2013-10-24 21:27:15 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
2013-10-19 00:47:24 +02:00
|
|
|
from django import forms
|
2013-10-25 19:35:32 +02:00
|
|
|
from django.contrib.auth import authenticate
|
2013-10-24 21:27:15 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
2020-12-05 23:23:19 +01:00
|
|
|
|
2014-02-13 23:20:20 +01:00
|
|
|
class UsernameField(forms.CharField):
|
|
|
|
|
"""
|
2020-12-05 23:23:19 +01:00
|
|
|
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".
|
2014-02-13 23:20:20 +01:00
|
|
|
"""
|
2020-12-05 23:23:19 +01:00
|
|
|
|
2014-02-13 23:20:20 +01:00
|
|
|
def to_python(self, value):
|
|
|
|
|
value = super(UsernameField, self).to_python(value)
|
|
|
|
|
value = value.lower()
|
|
|
|
|
return value
|
2020-12-05 23:23:19 +01:00
|
|
|
|
2013-10-19 00:47:24 +02:00
|
|
|
|
|
|
|
|
class LoginForm(forms.Form):
|
2014-02-13 23:20:20 +01:00
|
|
|
username = UsernameField(max_length=255)
|
2020-12-05 23:23:19 +01:00
|
|
|
password = forms.CharField(
|
|
|
|
|
max_length=255, widget=forms.PasswordInput,
|
2013-10-27 21:13:41 +01:00
|
|
|
help_text=_('Cookies must be enabled.'))
|
2020-12-05 23:23:19 +01:00
|
|
|
|
2013-10-25 19:35:32 +02:00
|
|
|
def clean(self):
|
2014-02-13 23:20:20 +01:00
|
|
|
username = self.cleaned_data.get('username')
|
2013-10-25 19:35:32 +02:00
|
|
|
password = self.cleaned_data.get('password')
|
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
|
if not user or not user.is_active:
|
2020-12-05 23:23:19 +01:00
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_(
|
|
|
|
|
'Sorry, that login was invalid. '
|
|
|
|
|
'Please try again.'
|
|
|
|
|
),
|
|
|
|
|
code='invalid_login'
|
|
|
|
|
)
|
2013-10-25 19:35:32 +02:00
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
|
|
def login(self, request):
|
2014-02-13 23:20:20 +01:00
|
|
|
username = self.cleaned_data.get('username')
|
2013-10-25 19:35:32 +02:00
|
|
|
password = self.cleaned_data.get('password')
|
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
|
return user
|
|
|
|
|
|
2013-10-23 00:27:31 +02:00
|
|
|
|
2013-10-24 21:27:15 +02:00
|
|
|
class GastroPinField(forms.CharField):
|
2013-10-25 19:35:32 +02:00
|
|
|
widget = forms.PasswordInput
|
2020-12-05 23:23:19 +01:00
|
|
|
|
2013-10-24 21:27:15 +02:00
|
|
|
def validate(self, value):
|
|
|
|
|
"""
|
2013-11-12 20:10:30 +01:00
|
|
|
Check if the value is all numeric and 4 - 8 chars long.
|
2013-10-24 21:27:15 +02:00
|
|
|
"""
|
2013-10-29 22:28:30 +01:00
|
|
|
match = re.match(r'^\d{4,8}$', value)
|
2013-10-24 21:27:15 +02:00
|
|
|
if not match:
|
2013-10-29 22:28:30 +01:00
|
|
|
raise forms.ValidationError(_('PIN must be 4 to 8 digits.'))
|
2013-10-24 21:27:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class GastroPinForm(forms.Form):
|
2013-10-25 19:35:32 +02:00
|
|
|
gastropin1 = GastroPinField(label=_('New Gastro-PIN'))
|
2020-12-05 23:23:19 +01:00
|
|
|
gastropin2 = GastroPinField(
|
|
|
|
|
label=_('Repeat Gastro-PIN'),
|
2013-11-12 20:10:30 +01:00
|
|
|
help_text=_('Numerical only, 4 to 8 digits'))
|
2013-10-25 19:35:32 +02:00
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
cleaned_data = super(GastroPinForm, self).clean()
|
|
|
|
|
gastropin1 = cleaned_data.get("gastropin1")
|
2013-11-12 20:10:30 +01:00
|
|
|
gastropin2 = cleaned_data.get("gastropin2")
|
|
|
|
|
|
2013-10-25 19:35:32 +02:00
|
|
|
if gastropin1 != gastropin2:
|
|
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_('The PINs entered were not identical.'),
|
2020-12-05 23:23:19 +01:00
|
|
|
code='not_identical'
|
|
|
|
|
)
|
2013-10-25 23:15:30 +02:00
|
|
|
return cleaned_data
|
2013-10-24 21:27:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class WlanPresenceForm(forms.Form):
|
|
|
|
|
# Boolean fields must never be required.
|
2020-12-05 23:23:19 +01:00
|
|
|
presence = forms.BooleanField(
|
|
|
|
|
required=False,
|
|
|
|
|
label=_('Enable WiFi presence')
|
|
|
|
|
)
|
2013-10-24 21:27:15 +02:00
|
|
|
|
|
|
|
|
|
2013-10-25 01:03:16 +02:00
|
|
|
class PasswordForm(forms.Form):
|
2020-12-05 23:23:19 +01:00
|
|
|
old_password = forms.CharField(
|
|
|
|
|
max_length=255, widget=forms.PasswordInput,
|
2013-10-26 20:06:42 +02:00
|
|
|
label=_('Old password'),
|
2020-12-05 23:23:19 +01:00
|
|
|
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')
|
|
|
|
|
)
|
2013-10-24 21:27:15 +02:00
|
|
|
|
2013-10-26 20:06:42 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
self._request = kwargs.pop('request', None)
|
|
|
|
|
super(PasswordForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
cleaned_data = super(PasswordForm, self).clean()
|
|
|
|
|
old_password = cleaned_data.get('old_password')
|
2014-02-08 20:39:08 +01:00
|
|
|
username = self._request.user.username.lower()
|
2013-10-26 20:06:42 +02:00
|
|
|
user = authenticate(username=username, password=old_password)
|
|
|
|
|
|
|
|
|
|
if not user or not user.is_active:
|
2020-12-05 23:23:19 +01:00
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_(
|
|
|
|
|
'The old password was incorrect.'
|
|
|
|
|
),
|
|
|
|
|
code='old_password_wrong'
|
|
|
|
|
)
|
2013-10-26 20:06:42 +02:00
|
|
|
|
|
|
|
|
password1 = cleaned_data.get('password1')
|
|
|
|
|
password2 = cleaned_data.get('password2')
|
|
|
|
|
if password1 != password2:
|
|
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_('The new passwords were not identical.'),
|
|
|
|
|
code='not_identical')
|
2013-11-24 18:10:42 +01:00
|
|
|
if len(password1) < 6:
|
|
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_('Password must be at least 6 characters long'),
|
|
|
|
|
code='to_short')
|
|
|
|
|
|
2013-10-26 20:06:42 +02:00
|
|
|
return cleaned_data
|
|
|
|
|
|
2013-10-24 21:27:15 +02:00
|
|
|
|
|
|
|
|
class RFIDForm(forms.Form):
|
2020-12-05 23:23:19 +01:00
|
|
|
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.'
|
|
|
|
|
)
|
|
|
|
|
)
|
2013-10-24 21:27:15 +02:00
|
|
|
|
2013-10-25 04:24:26 +02:00
|
|
|
|
2013-10-25 01:03:16 +02:00
|
|
|
class SIPPinForm(forms.Form):
|
2013-10-25 19:35:32 +02:00
|
|
|
sippin1 = GastroPinField(label=_('Your SIP PIN'))
|
|
|
|
|
sippin2 = GastroPinField(label=_('Repeat SIP PIN'))
|
|
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
cleaned_data = super(SIPPinForm, self).clean()
|
|
|
|
|
sippin1 = cleaned_data.get("sippin1")
|
|
|
|
|
sippin2 = cleaned_data.get("sippin2")
|
|
|
|
|
if sippin1 != sippin2:
|
|
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_('The PINs entered were not identical.'),
|
|
|
|
|
code='not_identical')
|
2013-10-25 01:03:16 +02:00
|
|
|
|
2013-10-24 21:27:15 +02:00
|
|
|
|
2013-10-24 21:44:53 +02:00
|
|
|
class NRF24Form(forms.Form):
|
2020-12-05 23:23:19 +01:00
|
|
|
nrf24 = forms.CharField(
|
|
|
|
|
max_length=255,
|
|
|
|
|
label=_('NRF24-ID'),
|
|
|
|
|
help_text=_("Your r0ket's NRF24 identification")
|
|
|
|
|
)
|
2013-10-25 04:24:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CLabPinForm(forms.Form):
|
2015-09-24 21:40:54 +02:00
|
|
|
c_lab_pin1 = GastroPinField(label=_('New indoor PIN'))
|
2020-12-05 23:23:19 +01:00
|
|
|
c_lab_pin2 = GastroPinField(
|
|
|
|
|
label=_('Repeat indoor PIN'),
|
|
|
|
|
help_text=_('Numerical only, 4 to 8 digits')
|
|
|
|
|
)
|
2013-10-26 22:38:57 +02:00
|
|
|
|
|
|
|
|
|
2015-12-01 00:32:51 +01:00
|
|
|
class PreferredEmailForm(forms.Form):
|
2020-12-05 23:23:19 +01:00
|
|
|
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."
|
|
|
|
|
)
|
|
|
|
|
)
|
2015-12-01 00:32:51 +01:00
|
|
|
|
|
|
|
|
|
2013-10-26 22:38:57 +02:00
|
|
|
class AdminForm(forms.Form):
|
2015-09-30 09:23:19 +02:00
|
|
|
username = forms.ChoiceField(choices=[])
|
2020-12-05 23:23:19 +01:00
|
|
|
password1 = forms.CharField(
|
|
|
|
|
max_length=255,
|
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
|
label=_('New password')
|
|
|
|
|
)
|
|
|
|
|
password2 = forms.CharField(
|
|
|
|
|
max_length=255,
|
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
|
label=_('Repeat password')
|
|
|
|
|
)
|
2013-10-26 22:38:57 +02:00
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
self._request = kwargs.pop('request', None)
|
|
|
|
|
self._users = kwargs.pop('users', [])
|
2013-11-12 21:28:00 +01:00
|
|
|
choices = [x for x in self._users]
|
2013-10-26 22:56:47 +02:00
|
|
|
choices.insert(0, ('', 'Select username ...'))
|
2013-10-26 22:38:57 +02:00
|
|
|
super(AdminForm, self).__init__(*args, **kwargs)
|
2020-12-05 23:23:19 +01:00
|
|
|
# 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.'
|
|
|
|
|
)
|
|
|
|
|
)
|
2013-10-26 22:38:57 +02:00
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
cleaned_data = super(AdminForm, self).clean()
|
|
|
|
|
|
|
|
|
|
password1 = cleaned_data.get('password1')
|
|
|
|
|
password2 = cleaned_data.get('password2')
|
|
|
|
|
if password1 != password2:
|
|
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_('The new passwords were not identical.'),
|
2020-12-05 23:23:19 +01:00
|
|
|
code='not_identical'
|
|
|
|
|
)
|
2013-11-24 18:10:42 +01:00
|
|
|
if len(password1) < 6:
|
|
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_('Password must be at least 6 characters long'),
|
2020-12-05 23:23:19 +01:00
|
|
|
code='to_short'
|
|
|
|
|
)
|
2013-10-26 22:38:57 +02:00
|
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
|
|
def get_member_choices(self):
|
2013-11-24 18:10:42 +01:00
|
|
|
return [(x, x) for x in self._users]
|