Fixes IndexError on save with removed member forwarding email (#11)
* Fixed IndexError if a non existent field gets queried for a value. Using dict.get to either get a value or provide a reasonable default * Removed list construction around new_key value * Clean up source code with 80x25 terminal and vim (complies with PEP-8) * Added some docstrings
This commit is contained in:
parent
4f093c0899
commit
09af25761c
7 changed files with 409 additions and 194 deletions
147
account/forms.py
147
account/forms.py
|
|
@ -7,33 +7,41 @@ from django import forms
|
|||
from django.contrib.auth import authenticate
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
|
||||
class UsernameField(forms.CharField):
|
||||
"""
|
||||
The username field makes sure that usernames are always entered in lower-case.
|
||||
If we do not convert the username to lower-case, Django will create more than
|
||||
one user object in the database. If we then try to login again, the Django auth
|
||||
subsystem will do an query that looks like this: username__iexact="username". The
|
||||
result is an error, because iexact returns the objects for "username" and "Username".
|
||||
The username field makes sure that usernames are always entered in
|
||||
lower-case. If we do not convert the username to lower-case, Django will
|
||||
create more than one user object in the database. If we then try to login
|
||||
again, the Django auth subsystem will do an query that looks like this:
|
||||
username__iexact="username". The result is an error, because iexact returns
|
||||
the objects for "username" and "Username".
|
||||
"""
|
||||
|
||||
|
||||
def to_python(self, value):
|
||||
value = super(UsernameField, self).to_python(value)
|
||||
value = value.lower()
|
||||
return value
|
||||
|
||||
|
||||
|
||||
class LoginForm(forms.Form):
|
||||
username = UsernameField(max_length=255)
|
||||
password = forms.CharField(max_length=255, widget=forms.PasswordInput,
|
||||
password = forms.CharField(
|
||||
max_length=255, widget=forms.PasswordInput,
|
||||
help_text=_('Cookies must be enabled.'))
|
||||
|
||||
|
||||
def clean(self):
|
||||
username = self.cleaned_data.get('username')
|
||||
password = self.cleaned_data.get('password')
|
||||
user = authenticate(username=username, password=password)
|
||||
if not user or not user.is_active:
|
||||
raise forms.ValidationError(_('Sorry, that login was invalid. '
|
||||
'Please try again.'), code='invalid_login')
|
||||
raise forms.ValidationError(
|
||||
_(
|
||||
'Sorry, that login was invalid. '
|
||||
'Please try again.'
|
||||
),
|
||||
code='invalid_login'
|
||||
)
|
||||
return self.cleaned_data
|
||||
|
||||
def login(self, request):
|
||||
|
|
@ -45,6 +53,7 @@ class LoginForm(forms.Form):
|
|||
|
||||
class GastroPinField(forms.CharField):
|
||||
widget = forms.PasswordInput
|
||||
|
||||
def validate(self, value):
|
||||
"""
|
||||
Check if the value is all numeric and 4 - 8 chars long.
|
||||
|
|
@ -56,7 +65,8 @@ class GastroPinField(forms.CharField):
|
|||
|
||||
class GastroPinForm(forms.Form):
|
||||
gastropin1 = GastroPinField(label=_('New Gastro-PIN'))
|
||||
gastropin2 = GastroPinField(label=_('Repeat Gastro-PIN'),
|
||||
gastropin2 = GastroPinField(
|
||||
label=_('Repeat Gastro-PIN'),
|
||||
help_text=_('Numerical only, 4 to 8 digits'))
|
||||
|
||||
def clean(self):
|
||||
|
|
@ -67,24 +77,33 @@ class GastroPinForm(forms.Form):
|
|||
if gastropin1 != gastropin2:
|
||||
raise forms.ValidationError(
|
||||
_('The PINs entered were not identical.'),
|
||||
code='not_identical')
|
||||
code='not_identical'
|
||||
)
|
||||
return cleaned_data
|
||||
|
||||
|
||||
class WlanPresenceForm(forms.Form):
|
||||
# Boolean fields must never be required.
|
||||
presence = forms.BooleanField(required=False,
|
||||
label=_('Enable WiFi presence'))
|
||||
presence = forms.BooleanField(
|
||||
required=False,
|
||||
label=_('Enable WiFi presence')
|
||||
)
|
||||
|
||||
|
||||
class PasswordForm(forms.Form):
|
||||
old_password = forms.CharField(max_length=255, widget=forms.PasswordInput,
|
||||
old_password = forms.CharField(
|
||||
max_length=255, widget=forms.PasswordInput,
|
||||
label=_('Old password'),
|
||||
help_text=_('Enter your current password here.'))
|
||||
password1 = forms.CharField(max_length=255, widget=forms.PasswordInput,
|
||||
label=_('New password'))
|
||||
password2 = forms.CharField(max_length=255, widget=forms.PasswordInput,
|
||||
label=_('Repeat password'))
|
||||
help_text=_(
|
||||
'Enter your current password here.'))
|
||||
password1 = forms.CharField(
|
||||
max_length=255, widget=forms.PasswordInput,
|
||||
label=_('New password')
|
||||
)
|
||||
password2 = forms.CharField(
|
||||
max_length=255, widget=forms.PasswordInput,
|
||||
label=_('Repeat password')
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._request = kwargs.pop('request', None)
|
||||
|
|
@ -97,8 +116,12 @@ class PasswordForm(forms.Form):
|
|||
user = authenticate(username=username, password=old_password)
|
||||
|
||||
if not user or not user.is_active:
|
||||
raise forms.ValidationError(_('The old password was incorrect.'),
|
||||
code='old_password_wrong')
|
||||
raise forms.ValidationError(
|
||||
_(
|
||||
'The old password was incorrect.'
|
||||
),
|
||||
code='old_password_wrong'
|
||||
)
|
||||
|
||||
password1 = cleaned_data.get('password1')
|
||||
password2 = cleaned_data.get('password2')
|
||||
|
|
@ -115,9 +138,14 @@ class PasswordForm(forms.Form):
|
|||
|
||||
|
||||
class RFIDForm(forms.Form):
|
||||
rfid = forms.CharField(max_length=255, label=_('Your RFID'),
|
||||
help_text=_('Find out your RFID by holding your RFID tag to the '
|
||||
'reader in the airlock.'))
|
||||
rfid = forms.CharField(
|
||||
max_length=255,
|
||||
label=_('Your RFID'),
|
||||
help_text=_(
|
||||
'Find out your RFID by holding your RFID tag to the '
|
||||
'reader in the airlock.'
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class SIPPinForm(forms.Form):
|
||||
|
|
@ -135,30 +163,44 @@ class SIPPinForm(forms.Form):
|
|||
|
||||
|
||||
class NRF24Form(forms.Form):
|
||||
nrf24 = forms.CharField(max_length=255,
|
||||
label = _('NRF24-ID'),
|
||||
help_text=_("Your r0ket's NRF24 identification"))
|
||||
nrf24 = forms.CharField(
|
||||
max_length=255,
|
||||
label=_('NRF24-ID'),
|
||||
help_text=_("Your r0ket's NRF24 identification")
|
||||
)
|
||||
|
||||
|
||||
class CLabPinForm(forms.Form):
|
||||
c_lab_pin1 = GastroPinField(label=_('New indoor PIN'))
|
||||
c_lab_pin2 = GastroPinField(label=_('Repeat indoor PIN'),
|
||||
help_text=_('Numerical only, 4 to 8 digits'))
|
||||
c_lab_pin2 = GastroPinField(
|
||||
label=_('Repeat indoor PIN'),
|
||||
help_text=_('Numerical only, 4 to 8 digits')
|
||||
)
|
||||
|
||||
|
||||
class PreferredEmailForm(forms.Form):
|
||||
preferred_email = forms.EmailField(max_length=255, required=False,
|
||||
label = _('Preferred e-mail'),
|
||||
help_text=_("Forward my mail to this address. Leave empty to use the c-base IMAP and SMTP servers."))
|
||||
preferred_email = forms.EmailField(
|
||||
max_length=255, required=False,
|
||||
label=_('Preferred e-mail'),
|
||||
help_text=_(
|
||||
"Forward my mail to this address. Leave empty to use the c-base "
|
||||
"IMAP and SMTP servers."
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class AdminForm(forms.Form):
|
||||
username = forms.ChoiceField(choices=[])
|
||||
password1 = forms.CharField(max_length=255, widget=forms.PasswordInput,
|
||||
label=_('New password'))
|
||||
password2 = forms.CharField(max_length=255, widget=forms.PasswordInput,
|
||||
label=_('Repeat password'))
|
||||
|
||||
password1 = forms.CharField(
|
||||
max_length=255,
|
||||
widget=forms.PasswordInput,
|
||||
label=_('New password')
|
||||
)
|
||||
password2 = forms.CharField(
|
||||
max_length=255,
|
||||
widget=forms.PasswordInput,
|
||||
label=_('Repeat password')
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._request = kwargs.pop('request', None)
|
||||
|
|
@ -166,10 +208,23 @@ class AdminForm(forms.Form):
|
|||
choices = [x for x in self._users]
|
||||
choices.insert(0, ('', 'Select username ...'))
|
||||
super(AdminForm, self).__init__(*args, **kwargs)
|
||||
#self.fields.insert(0, 'username', forms.ChoiceField(choices=choices,
|
||||
#help_text=_('Select the username for whom you want to reset the password.')))
|
||||
self.fields['username'] = forms.ChoiceField(choices=choices,
|
||||
help_text=_('Select the username for whom you want to reset the password.'))
|
||||
# self.fields.insert(
|
||||
# 0,
|
||||
# 'username',
|
||||
# forms.ChoiceField(
|
||||
# choices=choices,
|
||||
# help_text=_(
|
||||
# 'Select the username for whom you want '
|
||||
# 'to reset the password.'
|
||||
# )
|
||||
# )
|
||||
# )
|
||||
self.fields['username'] = forms.ChoiceField(
|
||||
choices=choices,
|
||||
help_text=_(
|
||||
'Select the username for whom you want to reset the password.'
|
||||
)
|
||||
)
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(AdminForm, self).clean()
|
||||
|
|
@ -179,11 +234,13 @@ class AdminForm(forms.Form):
|
|||
if password1 != password2:
|
||||
raise forms.ValidationError(
|
||||
_('The new passwords were not identical.'),
|
||||
code='not_identical')
|
||||
code='not_identical'
|
||||
)
|
||||
if len(password1) < 6:
|
||||
raise forms.ValidationError(
|
||||
_('Password must be at least 6 characters long'),
|
||||
code='to_short')
|
||||
code='to_short'
|
||||
)
|
||||
|
||||
return cleaned_data
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue