def validate_key(self, activation_key): """ Verify that the activation key is valid and within the permitted activation time window, returning the username if valid or raising ``ActivationError`` if not. """ try: obj = signing.loads( activation_key, salt=REGISTRATION_SALT, max_age=settings.ACCOUNT_ACTIVATION_DAYS * 86400 ) return obj['username'], obj['email'] except signing.SignatureExpired: raise ActivationError( self.EXPIRED_MESSAGE, code='expired' ) except signing.BadSignature: raise ActivationError( self.INVALID_KEY_MESSAGE, code='invalid_key', params={'activation_key': activation_key} )
def get_user(self, email): User = get_user_model() try: user = User.objects.get_by_natural_key(self.request.site, self.request.festival, email) if user.is_active: raise ActivationError(self.ALREADY_ACTIVATED_MESSAGE, code='already_activated') return user except User.DoesNotExist: raise ActivationError(self.BAD_USERNAME_MESSAGE, code='bad_username')
def get_user(self, username): User = get_user_model() try: user = User.objects.get(**{ User.USERNAME_FIELD: username, }) if user.is_active: raise ActivationError(self.ALREADY_ACTIVATED_MESSAGE, code='already_activated') return user except User.DoesNotExist: raise ActivationError(self.BAD_USERNAME_MESSAGE, code='bad_username')
def validate_key(self, activation_key): try: username = signing.loads(activation_key, salt=REGISTRATION_SALT, max_age=settings.ACCOUNT_ACTIVATION_DAYS * 86400) return username except signing.SignatureExpired: raise ActivationError(self.EXPIRED_MESSAGE, code='expired') messages.warning(self.request, f'seems like your activation code has expired!') except signing.BadSignature: raise ActivationError(self.INVALID_KEY_MESSAGE, code='invalid_key', params={'activation_key': activation_key})
def get_user(self, username): """ Given the verified username, look up and return the corresponding user account if it exists, or raising ``ActivationError`` if it doesn't. """ User = get_user_model() try: user = User.objects.get(**{User.USERNAME_FIELD: username}) if user.is_active: raise ActivationError(self.ALREADY_ACTIVATED_MESSAGE, code="already_activated") return user except User.DoesNotExist: raise ActivationError(self.BAD_USERNAME_MESSAGE, code="bad_username")
def activate(self, *args, **kwargs): username, email = self.validate_key(kwargs.get('activation_key')) user = self.get_user(username) try: UserEmail.objects.create(user=user, email=email, primary=True, verified=True) except IntegrityError as e: raise ActivationError( self.EMAIL_IN_USE_MESSAGE, code='email-in-use', ) user.email = email user.is_active = True user.save() return user
def activate(self, *args, **kwargs): activation_key = kwargs.get("activation_key") user_id = self.validate_key(activation_key) user = get_object_or_404(User, id=user_id) if self.request.user != user: raise ActivationError( message=_("Account does not match the activation code"), code="user_mismatch", params={"activation_key": activation_key, "user": self.request.user}, ) attendee, created = Attendee.objects.get_or_create(event=self.event, user=user) badge_data, created = BadgeData.objects.get_or_create(attendee=attendee) if created: badge_data.title = attendee.derive_title() badge_data.save() return attendee