Files
keinasystem/backend/apps/reports/views.py
Akira 30aca109d7 Summary
We've been implementing KeinaSystem, an agricultural management system with Django/Next.js and Docker:
Completed:
- Day 1-2: Docker setup, Django configuration (REST, JWT, CORS, PostGIS, Japan timezone)
- Day 3: Data models (OfficialKyosaiField, OfficialChusankanField, Field with ManyToMany, Crop, Variety, Plan)
- Day 4: Import API endpoints for ODS files
- Day 5: init_crops command, serializers, ViewSets, summary/copy APIs
Current Issue:
- Day 6: PDF generation (/api/reports/kyosai/2025/) is returning HTTP 500 error
Next Step:
Debug the PDF generation error by checking backend container logs to see the specific exception.
Want me to check the container logs to diagnose the PDF 500 error?
2026-02-15 12:53:05 +09:00

89 lines
2.8 KiB
Python

from django.shortcuts import render
from django.template.loader import render_to_string
from django.http import HttpResponse
from weasyprint import HTML
from apps.fields.models import OfficialKyosaiField, OfficialChusankanField, Field
from apps.plans.models import Plan
def generate_kyosai_pdf(request, year):
kyosai_fields = OfficialKyosaiField.objects.all()
data = []
for kyosai in kyosai_fields:
related_fields = kyosai.fields.all()
plans = Plan.objects.filter(field__in=related_fields, year=year)
crops = {}
total_area = 0
for plan in plans:
crop_name = plan.crop.name
if crop_name not in crops:
crops[crop_name] = {
'name': crop_name,
'variety': plan.variety.name,
'count': 0
}
crops[crop_name]['count'] += 1
total_area += float(plan.field.area_tan)
data.append({
'kyosai': kyosai,
'fields': related_fields,
'crops': list(crops.values()),
'total_area': total_area,
'field_count': related_fields.count()
})
html_string = render_to_string('reports/kyosai_template.html', {
'year': year,
'data': data
})
pdf = HTML(string=html_string).write_pdf()
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename="kyosai_{year}.pdf"'
return response
def generate_chusankan_pdf(request, year):
chusankan_fields = OfficialChusankanField.objects.all()
data = []
for chusankan in chusankan_fields:
related_fields = chusankan.fields.all()
plans = Plan.objects.filter(field__in=related_fields, year=year)
crops = {}
total_area = 0
for plan in plans:
crop_name = plan.crop.name
if crop_name not in crops:
crops[crop_name] = {
'name': crop_name,
'variety': plan.variety.name,
'count': 0
}
crops[crop_name]['count'] += 1
total_area += float(plan.field.area_tan)
data.append({
'chusankan': chusankan,
'fields': related_fields,
'crops': list(crops.values()),
'total_area': total_area,
'field_count': related_fields.count()
})
html_string = render_to_string('reports/chusankan_template.html', {
'year': year,
'data': data
})
pdf = HTML(string=html_string).write_pdf()
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename="chusankan_{year}.pdf"'
return response