-
Notifications
You must be signed in to change notification settings - Fork 64
/
forms.py
58 lines (47 loc) · 1.62 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.utils.translation import gettext_lazy as _
class UserCreateForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(UserCreateForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
class UserEditForm(forms.ModelForm):
username = forms.RegexField(
label=_("Username"),
max_length=30,
regex=r"^[\w.@+-]+$",
help_text=_(
"Required. 30 characters or fewer. Letters, digits and " "@/./+/-/_ only."
),
error_messages={
"invalid": _(
"This value may contain only letters, numbers and "
"@/./+/-/_ characters."
)
},
)
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email")
def __init__(self, *args, **kwargs):
super(UserEditForm, self).__init__(*args, **kwargs)
f = self.fields.get("user_permissions", None)
if f is not None:
f.queryset = f.queryset.select_related("content_type")
def save(self, commit=True):
user = super(UserEditForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
def clean_password(self):
return ""