mirror of
https://github.com/pinry/pinry.git
synced 2025-11-13 08:35:41 +01:00
Refactor apps to be in repo folder
This commit is contained in:
38
users/forms.py
Normal file
38
users/forms.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
class UserCreationForm(forms.ModelForm):
|
||||
"""
|
||||
A form that creates a user, with no privileges, from the given username,
|
||||
email, and password.
|
||||
"""
|
||||
error_messages = {
|
||||
'duplicate_username': _("A user with that username already exists."),
|
||||
}
|
||||
username = forms.RegexField(label=_("Username"), max_length=30,
|
||||
regex=r'^[\w-]+$')
|
||||
password = forms.CharField(label=_("Password"),
|
||||
widget=forms.PasswordInput)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ("username", "email")
|
||||
|
||||
def clean_username(self):
|
||||
# Since User.username is unique, this check is redundant,
|
||||
# but it sets a nicer error message than the ORM. See #13147.
|
||||
username = self.cleaned_data["username"]
|
||||
try:
|
||||
User._default_manager.get(username=username)
|
||||
except User.DoesNotExist:
|
||||
return username
|
||||
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
||||
|
||||
def save(self, commit=True):
|
||||
user = super(UserCreationForm, self).save(commit=False)
|
||||
user.set_password(self.cleaned_data["password"])
|
||||
if commit:
|
||||
user.save()
|
||||
return user
|
||||
Reference in New Issue
Block a user