from rest_framework import viewsets, status from rest_framework.decorators import action from rest_framework.response import Response from django.db.models import Sum from .models import Crop, Variety, Plan from .serializers import CropSerializer, VarietySerializer, PlanSerializer class CropViewSet(viewsets.ModelViewSet): queryset = Crop.objects.all() serializer_class = CropSerializer class VarietyViewSet(viewsets.ModelViewSet): queryset = Variety.objects.all() serializer_class = VarietySerializer class PlanViewSet(viewsets.ModelViewSet): queryset = Plan.objects.all() serializer_class = PlanSerializer def get_queryset(self): queryset = Plan.objects.all() year = self.request.query_params.get('year') if year: queryset = queryset.filter(year=year) return queryset @action(detail=False, methods=['get']) def summary(self, request): year = request.query_params.get('year') if not year: return Response({'error': 'year parameter is required'}, status=status.HTTP_400_BAD_REQUEST) plans = Plan.objects.filter(year=year) total_area = plans.aggregate(total=Sum('field__area_tan'))['total'] or 0 by_crop = {} for plan in plans: crop_name = plan.crop.name if crop_name not in by_crop: by_crop[crop_name] = { 'crop': crop_name, 'count': 0, 'area': 0 } by_crop[crop_name]['count'] += 1 by_crop[crop_name]['area'] += float(plan.field.area_tan) return Response({ 'year': int(year), 'total_plans': plans.count(), 'total_area': float(total_area), 'by_crop': list(by_crop.values()) }) @action(detail=False, methods=['post']) def copy_from_previous_year(self, request): from_year = request.data.get('from_year') to_year = request.data.get('to_year') if not from_year or not to_year: return Response({'error': 'from_year and to_year are required'}, status=status.HTTP_400_BAD_REQUEST) previous_plans = Plan.objects.filter(year=from_year) new_plans = [] for plan in previous_plans: new_plans.append(Plan( field=plan.field, year=to_year, crop=plan.crop, variety=plan.variety, notes=plan.notes )) Plan.objects.bulk_create(new_plans, ignore_conflicts=True) return Response({'message': f'Copied {len(new_plans)} plans from {from_year} to {to_year}'}) @action(detail=False, methods=['get']) def get_crops_with_varieties(self, request): crops = Crop.objects.prefetch_related('varieties').all() return Response(CropSerializer(crops, many=True).data)