-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
53 lines (42 loc) · 2.4 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
# -*- coding: utf-8 -*-
import hashlib
from django import forms
from django.contrib.auth.models import User
class LoginForm(forms.Form):
login_username = forms.CharField(min_length=1, max_length=75,
label='Username',
widget=forms.TextInput(attrs={'size':'30'}))
login_password = forms.CharField(min_length=1, max_length=40,
label='Password',
widget=forms.PasswordInput(attrs={'size':'30','autocomplete':'off'}, render_value=False))
next = forms.CharField(required=False, widget=forms.HiddenInput())
def clean(self):
cleaned_data = self.cleaned_data
username = cleaned_data.get('login_username')
password = cleaned_data.get('login_password')
if username and password:
# In case the user accidently puts spaces before/after...
username = username.strip()
return cleaned_data
def is_valid(self):
return super(LoginForm, self).is_valid() and not (hasattr(self, 'error_msg') and self.error_msg)
class SignupForm(forms.Form):
signup_username = forms.CharField(min_length=1, max_length=75,
label='Username',
widget=forms.TextInput(attrs={'size':'30'}))
signup_password = forms.CharField(min_length=1, max_length=40,
label='Password',
widget=forms.PasswordInput(attrs={'size':'30','autocomplete':'off'}, render_value=False))
next = forms.CharField(required=False, widget=forms.HiddenInput())
def clean(self):
cleaned_data = self.cleaned_data
username = cleaned_data.get('signup_username')
password = cleaned_data.get('signup_password')
if username and password:
username = username.strip()
hashed_password = hashlib.md5(password).hexdigest()
User.objects.get_or_create(username=username, defaults={'password': hashed_password, 'is_active': True})
#User.objects.get_or_create(username=username, defaults={'password': password, 'is_active': True})
return cleaned_data
def is_valid(self):
return super(SignupForm, self).is_valid() and not (hasattr(self, 'error_msg') and self.error_msg)