-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
132 lines (117 loc) · 3.71 KB
/
Copy pathforms.py
File metadata and controls
132 lines (117 loc) · 3.71 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
from django import forms
from django.contrib.auth import (
models as auth_models,
forms as auth_forms
)
from django.utils.translation import ugettext as _
from . import conf
class CustomLogIn(auth_forms.AuthenticationForm):
username = auth_forms.UsernameField(
widget=forms.TextInput(attrs={
"autofocus": True,
"class": "form-control"
}))
password = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput(
attrs={
"class": "form-control"
}
),
)
class UserBaseForm(forms.ModelForm):
"""
Form wrapper for User in django.contril.auth
"""
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = auth_models.User
fields = [
"first_name",
"last_name",
"username",
"email",
"password"
]
labels = {
"first_name": _("First Name"),
"last_name": _("Last Name"),
"email": _("Email"),
"username": _("Username"),
"password": _("Password"),
}
error_messages = {
"username": {
"required": _("Username field is required"),
"invalid": _("Username field is invalid")
},
"password": {
"required": _("Password field is required"),
"invalid": _("Password field is invalid")
}, }
widgets = {
"first_name": forms.TextInput(attrs={"class": "validate", "required": "required"}),
"last_name": forms.TextInput(attrs={"class": "validate", "required": "required"}),
"email": forms.EmailInput(attrs={"class": "validate", "required": "required"}),
"username": forms.TextInput(attrs={"class": "validate", "required": "required"}),
"password": forms.TextInput(attrs={"class": "validate", "required": "required"}),
}
def clean_email(self):
"""
Verification for unique email
:return: Email or raise exception
"""
email = self.cleaned_data["email"]
print("Cleaning email {}".format(email))
if conf.AUTH_UNIQUE_EMAIL:
try:
auth_models.User.objects.get(email=email)
except auth_models.User.DoesNotExist:
return email
raise forms.ValidationError(conf.AUTH_DUPLICATED_EMAIL_ERROR_MESSAGE)
else:
return email
def save(self, commit=True):
"""
Custom save method for determine active and unactive users
:param commit:
:return:
"""
user = super(UserBaseForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
if conf.AUTH_VERIFY_EMAIL:
user.is_active = False
user.save()
return user
class EmailPasswordForm(UserBaseForm):
"""
Use just Email to login
"""
class Meta(UserBaseForm.Meta):
fields = (
"email",
"password"
)
def save(self, commit=True):
"""
Custom save method for determine active and unactive users
:param commit:
:return:
"""
user = super(UserBaseForm, self).save(commit=False)
user.username = self.cleaned_data["email"]
if commit:
if conf.AUTH_VERIFY_EMAIL:
user.is_active = False
user.save()
return user
class UsernameEmailPasswordForm(UserBaseForm):
class Meta(UserBaseForm.Meta):
fields = (
"username",
"email",
"password"
)