implement report feature

This commit is contained in:
Maximilian 2025-06-10 12:46:53 +02:00
parent 4218ee6b7d
commit d6d47f714a
31 changed files with 2472 additions and 8 deletions

View file

@ -0,0 +1 @@
# Empty file to make this directory a Python package

View file

@ -0,0 +1,62 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from django.db import models
from datetime import date, timedelta
from bird.models import Bird, FallenBird, BirdStatus
from sendemail.models import Emailadress
from reports.models import AutomaticReport
class Command(BaseCommand):
help = 'Create test data for the reports system'
def handle(self, *args, **options):
self.stdout.write('Creating test data for reports system...')
# Create test email addresses
email1, created = Emailadress.objects.get_or_create(
email_address='test1@example.com',
defaults={'user_id': 1}
)
if created:
self.stdout.write(f'✓ Created email: {email1.email_address}')
email2, created = Emailadress.objects.get_or_create(
email_address='test2@example.com',
defaults={'user_id': 1}
)
if created:
self.stdout.write(f'✓ Created email: {email2.email_address}')
# Create a test automatic report
admin_user = User.objects.filter(is_superuser=True).first()
if admin_user:
auto_report, created = AutomaticReport.objects.get_or_create(
name='Test Weekly Report',
defaults={
'description': 'Automatic weekly report for testing',
'frequency': 'weekly',
'include_naturschutzbehoerde': True,
'include_jagdbehoerde': False,
'is_active': True,
'created_by': admin_user
}
)
if created:
auto_report.email_addresses.add(email1, email2)
self.stdout.write(f'✓ Created automatic report: {auto_report.name}')
# Check existing bird data
bird_count = FallenBird.objects.count()
self.stdout.write(f'✓ Found {bird_count} existing birds in database')
# Check birds with notification settings
notification_birds = Bird.objects.filter(
models.Q(melden_an_naturschutzbehoerde=True) |
models.Q(melden_an_jagdbehoerde=True)
).count()
self.stdout.write(f'✓ Found {notification_birds} birds with notification settings')
self.stdout.write(
self.style.SUCCESS('✓ Test data creation completed successfully!')
)

View file

@ -0,0 +1,129 @@
from django.core.management.base import BaseCommand
from django.utils import timezone
from datetime import date, timedelta
from reports.services import ReportGenerator
from reports.models import AutomaticReport
class Command(BaseCommand):
help = 'Test the report generation system'
def add_arguments(self, parser):
parser.add_argument(
'--test-manual',
action='store_true',
help='Test manual report generation',
)
parser.add_argument(
'--test-email',
action='store_true',
help='Test email sending (requires SMTP configuration)',
)
def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Testing Report System'))
# Simple test first
self.stdout.write('Reports app is working!')
# Test basic report generation
if options.get('test_manual'):
self.test_manual_report()
if options.get('test_email'):
self.test_email_report()
self.test_basic_functionality()
def test_basic_functionality(self):
self.stdout.write('Testing basic report functionality...')
# Create test date range (last 30 days)
date_to = date.today()
date_from = date_to - timedelta(days=30)
generator = ReportGenerator(
date_from=date_from,
date_to=date_to,
include_naturschutzbehoerde=True,
include_jagdbehoerde=False
)
# Test CSV generation
try:
csv_content, bird_count = generator.generate_csv()
self.stdout.write(
self.style.SUCCESS(
f'✓ CSV generation successful: {bird_count} birds found'
)
)
# Test summary
summary = generator.get_summary()
self.stdout.write(
self.style.SUCCESS(
f'✓ Summary generation successful: {summary["total_birds"]} total birds'
)
)
except Exception as e:
self.stdout.write(
self.style.ERROR(f'✗ CSV generation failed: {e}')
)
def test_manual_report(self):
self.stdout.write('Testing manual report creation...')
date_to = date.today()
date_from = date_to - timedelta(days=7) # Last week
generator = ReportGenerator(
date_from=date_from,
date_to=date_to,
include_naturschutzbehoerde=True,
include_jagdbehoerde=True
)
try:
# Create download log
log = generator.create_download_log()
self.stdout.write(
self.style.SUCCESS(
f'✓ Manual report log created: {log.id} with {log.patient_count} patients'
)
)
except Exception as e:
self.stdout.write(
self.style.ERROR(f'✗ Manual report creation failed: {e}')
)
def test_email_report(self):
self.stdout.write('Testing email report (dry run)...')
# This would test email functionality if SMTP is configured
# For now, just test the email template rendering
from django.template.loader import render_to_string
context = {
'date_from': '01.01.2024',
'date_to': '31.01.2024',
'patient_count': 42,
'filter_naturschutzbehörde': True,
'filter_jagdbehörde': False,
'automatic_report': None,
'created_at': '01.02.2024',
}
try:
subject = render_to_string('reports/email/report_subject.txt', context).strip()
message = render_to_string('reports/email/report_message.txt', context)
self.stdout.write(
self.style.SUCCESS(f'✓ Email template rendering successful')
)
self.stdout.write(f'Subject: {subject}')
except Exception as e:
self.stdout.write(
self.style.ERROR(f'✗ Email template rendering failed: {e}')
)