Skip to content

Commit

Permalink
feat(vote): prepare api validate route to invalid and valid submitteds
Browse files Browse the repository at this point in the history
  • Loading branch information
igr-santos committed Aug 19, 2024
1 parent 9d41a90 commit ee5bcd3
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions app/org_eleicoes/votepeloclima/candidature/views/oauth.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -80,12 +84,59 @@ 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":
instance.validations = instance.validations or {}

if instance.status == "submitted" and validation.get("status") == "validating":
instance.validations.update({
validation.get("slug"): {
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"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().strftime("%Y-%m-%d %H:%M:%S"),
"content": validation.get("content"),
"name": validation.get("name"),
"status": CandidatureFlowStatus.invalid
}
})

instance.status = CandidatureFlowStatus.invalid
instance.save()

return JsonResponse({"message": "fail"}, status=200)
elif instance.status == "submitted" and validation.get("status") == CandidatureFlowStatus.is_valid:
instance.validations.update({
validation.get("slug"): {
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"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"):
Expand All @@ -110,11 +161,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)

0 comments on commit ee5bcd3

Please sign in to comment.