def clean_username(self): if not alnum_re.search(self.cleaned_data["username"]): raise forms.ValidationError(_("Usernames can only contain letters, numbers and underscores.")) User = get_user_model() lookup_kwargs = get_user_lookup_kwargs({ "{username}__iexact": self.cleaned_data["username"] }) qs = User.objects.filter(**lookup_kwargs) if not qs.exists(): return self.cleaned_data["username"] raise forms.ValidationError(_("This username is already taken. Please choose another."))
def authenticate(self, **credentials): User = get_user_model() lookup_kwargs = get_user_lookup_kwargs({ "{username}__iexact": credentials["username"] }) try: user = User.objects.get(**lookup_kwargs) except (User.DoesNotExist, KeyError): return None else: try: if user.check_password(credentials["password"]): return user except KeyError: return None
def create_user(self, form, commit=True, **kwargs): user = get_user_model()(**kwargs) username = form.cleaned_data.get("username") if username is None: username = self.generate_username(form) user.username = username user.email = form.cleaned_data["email"].strip() password = form.cleaned_data.get("password") if password: user.set_password(password) else: user.set_unusable_password() if commit: user.save() return user
def send_email(self, email): User = get_user_model() protocol = getattr(settings, "DEFAULT_HTPROTOCOL", "http") current_site = get_current_site(self.request) email_qs = EmailAddress.objects.filter(email__iexact=email) for user in User.objects.filter(pk__in=email_qs.values("user")): uid = int_to_base36(user.id) token = self.make_token(user) password_reset_url = "{0}://{1}{2}".format( protocol, current_site.domain, reverse("account_password_reset_token", kwargs=dict(uidb36=uid, token=token))) ctx = { "user": user, "current_site": current_site, "password_reset_url": password_reset_url, } hookset.send_password_reset_email([email], ctx)
def get_user(self): try: uid_int = base36_to_int(self.kwargs["uidb36"]) except ValueError: raise Http404() return get_object_or_404(get_user_model(), id=uid_int)
raise ValidationError( 'Associated_with ID does not match with the user category') except Exception: raise ValidationError( 'Associated_with ID does not match with the user category') #user_associated_with = user_associated_with.school_name return u'%s' % (self.user) # # The call to get_user_model in global scope could lead to a circular import # when the app cache is not fully initialized in some cases. It is rare, but # it has happened. If you are debugging this problem and determine this line # of code as being problematic, contact the developers right away. # @receiver(post_save, sender=get_user_model()) def user_post_save(sender, **kwargs): """ After User.save is called we check to see if it was a created user. If so, we check if the User object wants account creation. If all passes we create an Account object. We only run on user creation to avoid having to check for existence on each call to User.save. """ user, created = kwargs["instance"], kwargs["created"] disabled = getattr(user, "_disable_account_creation", not settings.ACCOUNT_CREATE_ON_SAVE) if created and not disabled: Account.create(user=user)