def perform_create(self, params): with transaction.atomic(): try: # double transaction sinon la transaction externe n'est plus utilisable après l'exception with transaction.atomic(): self.person = Person.objects.create_person(params["email"]) already_created = False except IntegrityError: self.person = Person.objects.get_by_natural_key(params["email"]) already_created = True save_subscription_information(self.person, self.type, params) if already_created and self.show_already_created_message: messages.add_message( self.request, messages.INFO, self.error_messages["already_created"] ) elif not already_created and "welcome" in SUBSCRIPTIONS_EMAILS[self.type]: from ..tasks import send_welcome_mail send_welcome_mail.delay(self.person.pk, type=self.type) hard_login(self.request, self.person) if self.person.coordinates_type is None: geocode_person.delay(self.person.pk)
def perform_create(self, params): try: self.person = Person.objects.create_person( is_insoumise=self.create_insoumise, **params ) except IntegrityError: self.person = Person.objects.get_by_natural_key(params["email"]) if self.show_already_created_message: messages.add_message( self.request, messages.INFO, self.error_messages["already_created"] ) else: if self.create_insoumise: send_welcome_mail.delay(self.person.pk) hard_login(self.request, self.person)
def get(self, request, **kwargs): pk_requester = request.GET.get("pk_requester") pk_merge = request.GET.get("pk_merge") token = request.GET.get("token") # Check part if not pk_requester or not pk_merge or not token: return self.error_page(key_error="invalid") try: pk_requester = UUID(pk_requester) pk_merge = UUID(pk_merge) except ValueError: return self.error_page(key_error="invalid") try: person_requester = Person.objects.get(pk=pk_requester) person_merge = Person.objects.get(pk=pk_merge) except Person.DoesNotExist: return self.error_page(key_error="invalid") if merge_account_token_generator.is_expired(token): return self.error_page(key_error="expired") params = {"pk_requester": str(pk_requester), "pk_merge": str(pk_merge)} if not merge_account_token_generator.check_token(token, **params): return self.error_page(key_error="invalid") try: merge_persons(person_requester, person_merge) except ValueError: return self.error_page(key_error="same_person") # success part hard_login(request, person_requester) messages.add_message( self.request, messages.SUCCESS, "Votre fusion de compte à été effectué avec succès", ) return HttpResponseRedirect(self.success_url)
def get(self, request, **kwargs): new_mail = request.GET.get("new_email") user_pk = request.GET.get("user") token = request.GET.get("token") # Check part if not new_mail or not user_pk or not token: return self.error_page(key_error="invalid") try: user_pk = str(UUID(user_pk)) except ValueError: return self.error_page(key_error="invalid") try: self.person = Person.objects.get(pk=user_pk) except Person.DoesNotExist: return self.error_page(key_error="invalid") if add_email_confirmation_token_generator.is_expired(token): return self.error_page(key_error="expired") params = {"new_email": new_mail, "user": user_pk} if not add_email_confirmation_token_generator.check_token( token, **params): return self.error_page(key_error="invalid") try: self.person.add_email(new_mail) except IntegrityError: return self.error_page(key_error="already_used", email=new_mail) # success part hard_login(request, self.person) self.person.set_primary_email(new_mail) messages.add_message( self.request, messages.SUCCESS, "Votre changement de email à été effectué avec succès", ) return HttpResponseRedirect(self.success_url)
def form_valid(self, form): p = form.save() hard_login(self.request, p) return TemplateResponse(self.request, "people/confirmation_subscription.html")