feat: Mise en place des paiements en plusieurs fois - partie BACK [#25]

This commit is contained in:
N3WT DE COMPET
2025-02-09 19:20:43 +01:00
parent c269b89d3d
commit 274db249aa
4 changed files with 75 additions and 9 deletions

View File

@ -5,8 +5,8 @@ from rest_framework.parsers import JSONParser
from rest_framework.views import APIView
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from .models import Teacher, Speciality, SchoolClass, Planning, Discount, Fee
from .serializers import TeacherSerializer, SpecialitySerializer, SchoolClassSerializer, PlanningSerializer, DiscountSerializer, FeeSerializer
from .models import Teacher, Speciality, SchoolClass, Planning, Discount, Fee, PaymentPlan
from .serializers import TeacherSerializer, SpecialitySerializer, SchoolClassSerializer, PlanningSerializer, DiscountSerializer, FeeSerializer, PaymentPlanSerializer
from N3wtSchool import bdd
from N3wtSchool.bdd import delete_object, getAllObjects, getObject
@ -307,4 +307,52 @@ class FeeView(APIView):
return JsonResponse(fee_serializer.errors, safe=False, status=400)
def delete(self, request, _id):
return delete_object(Fee, _id)
return delete_object(Fee, _id)
@method_decorator(csrf_protect, name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class PaymentPlansView(APIView):
def get(self, request, _filter, *args, **kwargs):
if _filter not in ['registration', 'tuition']:
return JsonResponse({"error": "Invalid type parameter. Must be 'registration' or 'tuition'."}, safe=False, status=400)
type_value = 0 if _filter == 'registration' else 1
paymentPlans = PaymentPlan.objects.filter(type=type_value)
payment_plans_serializer = PaymentPlanSerializer(paymentPlans, many=True)
return JsonResponse(payment_plans_serializer.data, safe=False, status=200)
@method_decorator(csrf_protect, name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class PaymentPlanView(APIView):
def get(self, request, _id):
try:
payment_plan = PaymentPlan.objects.get(id=_id)
payment_plan_serializer = PaymentPlanSerializer(payment_plan)
return JsonResponse(payment_plan_serializer.data, safe=False)
except PaymentPlan.DoesNotExist:
return JsonResponse({'error': 'No object found'}, status=404)
def post(self, request):
payment_plan_data = JSONParser().parse(request)
payment_plan_serializer = PaymentPlanSerializer(data=payment_plan_data)
if payment_plan_serializer.is_valid():
payment_plan_serializer.save()
return JsonResponse(payment_plan_serializer.data, safe=False, status=201)
return JsonResponse(payment_plan_serializer.errors, safe=False, status=400)
def put(self, request, _id):
payment_plan_data = JSONParser().parse(request)
try:
payment_plan = PaymentPlan.objects.get(id=_id)
except PaymentPlan.DoesNotExist:
return JsonResponse({'error': 'No object found'}, status=404)
payment_plan_serializer = PaymentPlanSerializer(payment_plan, data=payment_plan_data, partial=True)
if payment_plan_serializer.is_valid():
payment_plan_serializer.save()
return JsonResponse(payment_plan_serializer.data, safe=False)
return JsonResponse(payment_plan_serializer.errors, safe=False, status=400)
def delete(self, request, _id):
return delete_object(PaymentPlan, _id)