mirror of
https://github.com/bodyrep/bodyrep-sandpit.git
synced 2026-02-01 09:41:44 +00:00
Django import
Django import
This commit is contained in:
91
django/app/forms.py
Normal file
91
django/app/forms.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import datetime
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth import authenticate
|
||||
from django.db.models import Q
|
||||
from app.models import Members
|
||||
|
||||
class LoginForm(forms.Form):
|
||||
username = forms.CharField(label=_("Username"), max_length=30,
|
||||
widget=forms.TextInput(attrs={'tabindex': 1, 'class': 'NB-input'}),
|
||||
error_messages={'required': 'Please enter a username.'})
|
||||
password = forms.CharField(label=_("Password"),
|
||||
widget=forms.PasswordInput(attrs={'tabindex': 2, 'class': 'NB-input'}),
|
||||
required=False)
|
||||
# error_messages={'required': 'Please enter a password.'})
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.user_cache = None
|
||||
super(LoginForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def clean(self):
|
||||
username = self.cleaned_data.get('username', '').lower()
|
||||
password = self.cleaned_data.get('password', '')
|
||||
|
||||
user = User.objects.filter(Q(username__iexact=username) | Q(email=username))
|
||||
if username and user:
|
||||
self.user_cache = authenticate(username=user[0].username, password=password)
|
||||
if self.user_cache is None:
|
||||
self.user_cache = authenticate(username=user[0].username, password="")
|
||||
if self.user_cache is None:
|
||||
email_username = User.objects.filter(email=username)
|
||||
if email_username:
|
||||
self.user_cache = authenticate(username=email_username[0].username, password=password)
|
||||
if self.user_cache is None:
|
||||
self.user_cache = authenticate(username=email_username[0].username, password="")
|
||||
if self.user_cache is None:
|
||||
# logging.info(" ***> [%s] Bad Login: TRYING JK-LESS PASSWORD" % username)
|
||||
jkless_password = password.replace('j', '').replace('k', '')
|
||||
self.user_cache = authenticate(username=username, password=jkless_password)
|
||||
if self.user_cache is None:
|
||||
#logging.info(" ***> [%s] Bad Login" % username)
|
||||
raise forms.ValidationError(_("Whoopsy-daisy. Try again."))
|
||||
else:
|
||||
# Supreme fuck-up. Accidentally removed the letters J and K from
|
||||
# all user passwords. Re-save with correct password.
|
||||
#logging.info(" ***> [%s] FIXING JK-LESS PASSWORD" % username)
|
||||
self.user_cache.set_password(password)
|
||||
self.user_cache.save()
|
||||
if not self.user_cache.is_active:
|
||||
raise forms.ValidationError(_("This account is inactive."))
|
||||
elif username and not user:
|
||||
raise forms.ValidationError(_("That username is not registered. Create an account with it instead."))
|
||||
|
||||
return self.cleaned_data
|
||||
|
||||
def get_user_id(self):
|
||||
if self.user_cache:
|
||||
return self.user_cache.id
|
||||
return None
|
||||
|
||||
def get_user(self):
|
||||
return self.user_cache
|
||||
|
||||
class EditProfileForm(forms.Form):
|
||||
firstname = forms.CharField(label=_("First Name"), max_length=30,
|
||||
widget=forms.TextInput(attrs={'tabindex': 1, 'class': 'NB-input'}),
|
||||
error_messages={'required': 'Please enter a first name'})
|
||||
lastname = forms.CharField(label=_("LastName"),
|
||||
widget=forms.TextInput(attrs={'tabindex': 2, 'class': 'NB-input'}),
|
||||
error_messages={'required': 'Please enter a last name.'})
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(EditProfileForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def clean(self):
|
||||
firstname = self.cleaned_data.get('firstname', '')
|
||||
lastname = self.cleaned_data.get('lastname', '')
|
||||
|
||||
return self.cleaned_data
|
||||
|
||||
def save(self, username):
|
||||
firstname = self.cleaned_data['firstname']
|
||||
lastname = self.cleaned_data['lastname']
|
||||
|
||||
member = Members.objects.get(username=username)
|
||||
member.firstname = firstname
|
||||
member.lastname = lastname
|
||||
member.save()
|
||||
|
||||
return member
|
||||
Reference in New Issue
Block a user