Ejemplo n.º 1
0
    def confirm(self):
        if self.is_expired():
            raise EmailConfirmationExpired()
        else:
            if not self.is_verified:
                with transaction.atomic():
                    self.is_verified = True
                    self.confirmed_at = timezone.now()
                    update_fields(self, fields=('is_verified', 'confirmed_at'))

                    signals.post_email_confirm.send(
                        sender=self.__class__,
                        confirmation=self,
                    )

                    if self.is_primary and settings.EMAIL_CONFIRM_LA_SAVE_EMAIL_TO_INSTANCE:
                        self.save_email()

                    # Expire all other confirmations for the same email
                    reverse_expiry = timezone.now() - datetime.timedelta(seconds=settings.EMAIL_CONFIRM_LA_CONFIRM_EXPIRE_SEC)
                    EmailConfirmation.objects \
                        .filter(
                            content_type=self.content_type,
                            email_field_name=self.email_field_name,
                            email=self.email
                        ) \
                        .exclude(pk=self.pk) \
                        .update(send_at=reverse_expiry)

        return self
    def confirm(self, request):
        if not self.is_verified:
            if self.is_expired():
                raise EmailConfirmationExpired()
            with transaction.atomic():
                self.is_verified = True
                self.confirmed_at = timezone.now()
                # self.save(update_fields=['is_verified', 'confirmed_at'])
                update_fields(self, fields=('is_verified', 'confirmed_at'))

                signal_responses = signals.post_email_confirm.send(
                    sender=self.__class__,
                    confirmation=self,
                    request=request,
                )

                if self.is_primary and settings.EMAIL_CONFIRM_LA_SAVE_EMAIL_TO_INSTANCE:
                    self.save_email()

        else:
            # This email confirmation is already verified, but the user may
            # click the link twice (by accident?). The response should still
            # be helpful. Re-issue the signal.
            signal_responses = signals.post_email_confirm.send(
                sender=self.__class__,
                confirmation=self,
                request=request,
            )

        # signal.send() returns a list of tuples of the receiver and the
        # returned value from that receiver.
        for reciver, response in signal_responses:
            if response is not None:
                return response

        return None