2010-07-05 17:52:52 -04:00
|
|
|
import datetime
|
2010-04-22 21:17:00 -04:00
|
|
|
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
|
2010-06-12 21:20:06 -04:00
|
|
|
from apps.reader.models import Feature
|
2010-08-23 18:37:59 -04:00
|
|
|
from utils import log as logging
|
2010-04-22 21:17:00 -04:00
|
|
|
|
|
|
|
class LoginForm(forms.Form):
|
2010-09-12 13:55:04 -04:00
|
|
|
username = forms.CharField(label=_("Username or Email"), max_length=30,
|
2010-04-22 21:17:00 -04:00
|
|
|
error_messages={'required': 'Please enter a username.'})
|
|
|
|
password = forms.CharField(label=_("Password"), widget=forms.PasswordInput,
|
2010-07-05 16:05:54 -04:00
|
|
|
required=False)
|
|
|
|
# error_messages={'required': 'Please enter a password.'})
|
2010-04-22 21:17:00 -04:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user_cache = None
|
|
|
|
super(LoginForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
username = self.cleaned_data.get('username')
|
2010-08-02 09:57:53 -04:00
|
|
|
password = self.cleaned_data.get('password', '')
|
2010-04-22 21:17:00 -04:00
|
|
|
|
2010-07-05 16:05:54 -04:00
|
|
|
if username:
|
2010-04-22 21:17:00 -04:00
|
|
|
self.user_cache = authenticate(username=username, password=password)
|
|
|
|
if self.user_cache is None:
|
2010-09-12 13:55:04 -04:00
|
|
|
email_username = User.objects.filter(email=username)
|
|
|
|
if email_username:
|
|
|
|
self.user_cache = authenticate(username=email_username[0].username, password=password)
|
2010-08-23 18:37:59 -04:00
|
|
|
if self.user_cache is None:
|
2010-09-12 13:55:04 -04:00
|
|
|
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()
|
|
|
|
elif not self.user_cache.is_active:
|
|
|
|
raise forms.ValidationError(_("This account is inactive."))
|
2010-04-22 21:17:00 -04:00
|
|
|
|
|
|
|
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 SignupForm(forms.Form):
|
2010-04-25 15:43:09 -04:00
|
|
|
signup_username = forms.RegexField(regex=r'^\w+$',
|
|
|
|
max_length=30,
|
|
|
|
widget=forms.TextInput(),
|
|
|
|
label=_(u'username'),
|
|
|
|
error_messages={'required': 'Please enter a username.'})
|
2010-04-22 21:17:00 -04:00
|
|
|
email = forms.EmailField(widget=forms.TextInput(attrs=dict(maxlength=75)),
|
|
|
|
label=_(u'email address'),
|
2010-07-05 16:05:54 -04:00
|
|
|
required=False)
|
|
|
|
# error_messages={'required': 'Please enter your email.'})
|
2010-08-02 09:57:53 -04:00
|
|
|
signup_password = forms.CharField(widget=forms.PasswordInput(),
|
2010-04-25 15:43:09 -04:00
|
|
|
label=_(u'password'),
|
2010-07-05 16:05:54 -04:00
|
|
|
required=False)
|
|
|
|
# error_messages={'required': 'Please enter a password.'})
|
2010-04-22 21:17:00 -04:00
|
|
|
|
2010-04-25 15:43:09 -04:00
|
|
|
def clean_signup_username(self):
|
2010-04-22 21:17:00 -04:00
|
|
|
try:
|
2010-07-05 16:05:54 -04:00
|
|
|
User.objects.get(username__iexact=self.cleaned_data['signup_username'])
|
2010-04-22 21:17:00 -04:00
|
|
|
except User.DoesNotExist:
|
2010-04-25 15:43:09 -04:00
|
|
|
return self.cleaned_data['signup_username']
|
2010-07-05 22:02:08 -04:00
|
|
|
raise forms.ValidationError(_(u'Someone is already using that username.'))
|
2010-04-25 15:43:09 -04:00
|
|
|
return self.cleaned_data['signup_username']
|
2010-04-22 21:17:00 -04:00
|
|
|
|
2010-07-05 16:05:54 -04:00
|
|
|
def clean_signup_password(self):
|
|
|
|
if not self.cleaned_data['signup_password']:
|
|
|
|
return ""
|
2010-08-02 09:57:53 -04:00
|
|
|
return self.cleaned_data['signup_password']
|
2010-07-05 16:05:54 -04:00
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
if not self.cleaned_data['email']:
|
|
|
|
return ""
|
2010-08-02 09:57:53 -04:00
|
|
|
return self.cleaned_data['email']
|
2010-04-22 21:17:00 -04:00
|
|
|
|
|
|
|
def save(self, profile_callback=None):
|
2010-07-05 16:05:54 -04:00
|
|
|
new_user = User(username=self.cleaned_data['signup_username'])
|
2010-04-25 15:43:09 -04:00
|
|
|
new_user.set_password(self.cleaned_data['signup_password'])
|
2010-04-22 21:17:00 -04:00
|
|
|
new_user.is_active = True
|
2010-07-05 16:05:54 -04:00
|
|
|
new_user.email = self.cleaned_data['email']
|
2010-04-22 21:17:00 -04:00
|
|
|
new_user.save()
|
2010-07-05 16:05:54 -04:00
|
|
|
new_user = authenticate(username=self.cleaned_data['signup_username'],
|
|
|
|
password=self.cleaned_data['signup_password'])
|
2010-04-22 21:17:00 -04:00
|
|
|
|
|
|
|
return new_user
|
2010-06-12 21:20:06 -04:00
|
|
|
|
|
|
|
class FeatureForm(forms.Form):
|
|
|
|
description = forms.CharField(required=True)
|
|
|
|
|
|
|
|
def save(self):
|
2010-07-05 17:52:52 -04:00
|
|
|
feature = Feature(description=self.cleaned_data['description'],
|
|
|
|
date=datetime.datetime.now() + datetime.timedelta(minutes=1))
|
2010-06-12 21:20:06 -04:00
|
|
|
feature.save()
|
|
|
|
return feature
|