From 8569d3451f9591464ca974dc4f6581be22850da5 Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Mon, 19 Aug 2024 15:53:00 -0300 Subject: [PATCH] feat(vote): prepare api validate route to invalid and valid submitteds --- .../votepeloclima/candidature/views/oauth.py | 57 +++++++++++++++++-- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/app/org_eleicoes/votepeloclima/candidature/views/oauth.py b/app/org_eleicoes/votepeloclima/candidature/views/oauth.py index b331e556..af2b61a2 100644 --- a/app/org_eleicoes/votepeloclima/candidature/views/oauth.py +++ b/app/org_eleicoes/votepeloclima/candidature/views/oauth.py @@ -1,5 +1,9 @@ +import json + +from datetime import datetime from functools import reduce from collections import OrderedDict +# from typing import TypedDict from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import TemplateView, View @@ -80,12 +84,55 @@ def post(self, request, *args, **kwargs): return super().post(request, *args, **kwargs) +# Validation = TypedDict("Validation", { +# "status": str, +# "slug": str, +# "name": str, +# "content": str | None +# }) + + @method_decorator(csrf_exempt, name="dispatch") class UpdateCandidatureStatusView(JsonLoginRequiredMixin, View): def post(self, request, *args, **kwargs): + validation = json.loads(request.body) instance = CandidatureFlow.objects.get(user=request.user) - if instance.status == "submitted": + + if instance.status == "submitted" and validation.get("status") == "validating": + instance.validations.update({ + [validation.get("slug")]: { + "date": datetime.now(), + "content": validation.get("content"), + "name": validation.get("name"), + "status": CandidatureFlowStatus.is_valid + } + }) + instance.save() + elif instance.status == "submitted" and validation.get("status") == CandidatureFlowStatus.invalid: + instance.validations.update({ + [validation.get("slug")]: { + "date": datetime.now(), + "content": validation.get("content"), + "name": validation.get("name"), + "status": CandidatureFlowStatus.invalid + } + }) + instance.status = CandidatureFlowStatus.invalid + instance.save() + + return JsonResponse({"message": "fail"}, status=400) + elif instance.status == "submitted" and validation.get("status") == CandidatureFlowStatus.is_valid: + instance.validations.update({ + [validation.get("slug")]: { + "date": datetime.now(), + "content": validation.get("content"), + "name": validation.get("name"), + "status": CandidatureFlowStatus.is_valid + } + }) + instance.status = CandidatureFlowStatus.is_valid + values = {} for step, form_class in OrderedDict(register_form_list).items(): if step not in ("captcha", "checkout"): @@ -110,11 +157,9 @@ def post(self, request, *args, **kwargs): if instance.candidature: Candidature.objects.filter(id=instance.candidature.id).update(**values) - instance.status = CandidatureFlowStatus.is_valid - instance.save() else: instance.candidature = Candidature.objects.create(**values) - instance.status = CandidatureFlowStatus.is_valid - instance.save() + + instance.save() - return JsonResponse({"message": "success"}) + return JsonResponse({"message": "success"}, status=200)