This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
forms.py
96 lines (79 loc) · 3.34 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.utils.translation import ugettext_noop, ugettext_lazy as _
from .models import FruitUser, Message
class UserCreationForm(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.")},
)
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = FruitUser
fields = '__all__'
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class UserChangeForm(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.')},
)
password = ReadOnlyPasswordHashField(
label=_('Password'),
help_text=_('Raw passwords are not stored, so there is no way to see '
'this user\'s password, but you can change the password '
'using <a href="password/">this form</a>.'),
)
class Meta:
model = FruitUser
fields = '__all__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
f = self.fields.get('user_permissions', None)
if f is not None:
f.queryset = f.queryset.select_related('content_type')
def clean_password(self):
return self.initial["password"]
class MessageAdminForm(forms.ModelForm):
class Meta:
model = Message
fields = '__all__'
widgets = {
'text': forms.Textarea(attrs={'rows': 5, 'cols': 86, 'maxlength': 255}),
}
class UserSettingsForm(forms.ModelForm):
class Meta:
model = FruitUser
fields = ('avatar', 'motto')
widgets = {
'motto': forms.Textarea(attrs={'rows': 4, 'cols': 86, 'maxlength': 255}),
}
help_texts = {
'motto': _('Short text about you, that will appear in your profile.'),
'avatar': _('You can change your profile picture by uploading custom image '
'(up to 1 MB), or alternatively via '
'<a href="http://gravatar.com" rel="noopener noreferrer" '
'target="_blank">gravatar.com</a> service.')
}
override_wrong_default_translation = ugettext_noop('Username or e-mail')