From a9727b3526a9ee148ab06c5cd0585497a9991522 Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Wed, 4 Sep 2024 19:45:44 -0300 Subject: [PATCH] fix(vote): catch IntegrityError when user email exists --- .../votepeloclima/candidature/views/base.py | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/app/org_eleicoes/votepeloclima/candidature/views/base.py b/app/org_eleicoes/votepeloclima/candidature/views/base.py index 2646bded..b8d23741 100644 --- a/app/org_eleicoes/votepeloclima/candidature/views/base.py +++ b/app/org_eleicoes/votepeloclima/candidature/views/base.py @@ -124,6 +124,7 @@ def process_step(self, form): "first_name": name.split(" ")[0], "last_name": " ".join(name.split(" ")[1:]), } + user, created = User.objects.get_or_create(**values) if created: @@ -225,16 +226,22 @@ def dispatch(self, request, *args, **kwargs): def post(self, *args, **kwargs): request = self.request - if "wizard_goto_last" in request.POST: - form = self.get_form(data=request.POST, files=request.FILES) - - if form.is_valid(): - self.storage.set_step_data(self.steps.current, self.process_step(form)) - self.storage.set_step_files( - self.steps.current, self.process_step_files(form) - ) - # Move to last step - self.storage.current_step = self.steps.all[-1] - return self.render(self.get_form()) + try: + if "wizard_goto_last" in request.POST: + form = self.get_form(data=request.POST, files=request.FILES) + + if form.is_valid(): + self.storage.set_step_data(self.steps.current, self.process_step(form)) + self.storage.set_step_files( + self.steps.current, self.process_step_files(form) + ) + # Move to last step + self.storage.current_step = self.steps.all[-1] + return self.render(self.get_form()) - return super().post(*args, **kwargs) + return super().post(*args, **kwargs) + except IntegrityError: + # Evita problemas com e-mails duplicados + form = self.get_form(data=request.POST, files=request.FILES) + form.add_error("email", "Esse e-mail já possui um cadastro em andamento, por favor, cadastre uma senha e/ou faça login.") + return self.render(form)