2013-12-16 20:15:47 +00:00
|
|
|
import logging
|
2013-11-28 03:14:52 +00:00
|
|
|
from django import forms
|
2017-09-22 20:54:16 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2018-07-26 17:59:58 +00:00
|
|
|
from django.contrib.auth.forms import PasswordResetForm
|
2013-11-28 03:14:52 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-07-30 23:00:58 +00:00
|
|
|
from registration.forms import RegistrationFormUniqueEmail
|
2013-12-16 20:15:47 +00:00
|
|
|
from .emailcheck import is_disposable
|
2013-11-28 03:14:52 +00:00
|
|
|
from .models import Library
|
2013-10-08 19:37:22 +00:00
|
|
|
|
2013-12-16 20:15:47 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-07-27 17:02:47 +00:00
|
|
|
class UserData(forms.Form):
|
|
|
|
username = forms.RegexField(
|
2018-07-26 17:59:58 +00:00
|
|
|
label=_("New Username"),
|
|
|
|
max_length=30,
|
2016-07-27 17:02:47 +00:00
|
|
|
regex=r'^[\w.@+-]+$',
|
2018-07-26 17:59:58 +00:00
|
|
|
help_text=_("30 characters or fewer."),
|
|
|
|
error_messages={
|
2016-07-27 17:02:47 +00:00
|
|
|
'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
oldusername = None
|
|
|
|
allow_same = False
|
|
|
|
|
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
username = self.data["username"]
|
|
|
|
if username != self.oldusername:
|
|
|
|
users = User.objects.filter(username__iexact=username)
|
|
|
|
for user in users:
|
|
|
|
raise forms.ValidationError(_("Another user with that username already exists."))
|
|
|
|
return username
|
|
|
|
if not self.allow_same:
|
|
|
|
raise forms.ValidationError(_("Your username is already "+username))
|
|
|
|
|
|
|
|
class UserNamePass(UserData):
|
2018-07-26 17:59:58 +00:00
|
|
|
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
|
|
|
|
password2 = forms.CharField(
|
|
|
|
label=_("Password confirmation"),
|
2016-07-27 17:02:47 +00:00
|
|
|
widget=forms.PasswordInput,
|
2018-07-26 17:59:58 +00:00
|
|
|
help_text=_("Enter the same password as above, for verification.")
|
|
|
|
)
|
2016-07-27 17:02:47 +00:00
|
|
|
allow_same = True
|
|
|
|
def clean_password2(self):
|
|
|
|
password1 = self.cleaned_data.get("password1", "")
|
|
|
|
password2 = self.cleaned_data["password2"]
|
|
|
|
if password1 != password2:
|
|
|
|
raise forms.ValidationError(_("The two passwords don't match."))
|
|
|
|
return password2
|
|
|
|
|
2018-07-30 23:00:58 +00:00
|
|
|
class RegistrationFormNoDisposableEmail(RegistrationFormUniqueEmail):
|
2013-12-16 20:15:47 +00:00
|
|
|
def clean_email(self):
|
|
|
|
"""
|
|
|
|
Check the supplied email address against a list of known disposable
|
|
|
|
webmail domains.
|
|
|
|
"""
|
2018-07-30 23:00:58 +00:00
|
|
|
cleaned_email = super(RegistrationFormNoDisposableEmail, self).clean_email()
|
2013-12-16 20:15:47 +00:00
|
|
|
logger.info('cleaning email')
|
2018-07-30 23:00:58 +00:00
|
|
|
if is_disposable(cleaned_email):
|
2013-12-16 20:15:47 +00:00
|
|
|
raise forms.ValidationError(_("Please supply a permanent email address."))
|
2018-07-30 23:00:58 +00:00
|
|
|
return cleaned_email
|
2013-12-16 20:15:47 +00:00
|
|
|
|
2017-09-22 20:54:16 +00:00
|
|
|
class SocialAwarePasswordResetForm(PasswordResetForm):
|
|
|
|
def get_users(self, email):
|
|
|
|
"""
|
|
|
|
Send the reset form even if the user password is not usable
|
|
|
|
"""
|
|
|
|
active_users = get_user_model()._default_manager.filter(
|
|
|
|
email__iexact=email, is_active=True)
|
|
|
|
return active_users
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
email = self.cleaned_data['email']
|
|
|
|
if not get_user_model().objects.filter(email__iexact=email, is_active=True).exists():
|
|
|
|
raise forms.ValidationError("There aren't ungluers with that email address!")
|
|
|
|
return email
|
2018-07-26 17:59:58 +00:00
|
|
|
|
2017-09-22 20:02:43 +00:00
|
|
|
|
2013-11-28 03:14:52 +00:00
|
|
|
class NewLibraryForm(forms.ModelForm):
|
|
|
|
username = forms.RegexField(
|
2018-07-26 17:59:58 +00:00
|
|
|
label=_("Library Username"),
|
|
|
|
max_length=30,
|
2013-11-28 03:14:52 +00:00
|
|
|
regex=r'^[\w.@+-]+$',
|
2018-07-26 17:59:58 +00:00
|
|
|
help_text=_("30 characters or fewer."),
|
|
|
|
error_messages={
|
2013-11-28 03:14:52 +00:00
|
|
|
'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")
|
|
|
|
},
|
2018-07-26 17:59:58 +00:00
|
|
|
initial='',
|
2013-11-28 03:14:52 +00:00
|
|
|
)
|
|
|
|
email = forms.EmailField(
|
2018-07-26 17:59:58 +00:00
|
|
|
label=_("notification email address for library"),
|
2013-11-28 03:14:52 +00:00
|
|
|
max_length=100,
|
|
|
|
error_messages={'required': 'Please enter an email address for the library.'},
|
|
|
|
)
|
2018-07-26 17:59:58 +00:00
|
|
|
|
2013-11-28 03:14:52 +00:00
|
|
|
def clean_username(self):
|
2018-07-26 17:59:58 +00:00
|
|
|
username = self.cleaned_data['username']
|
2013-11-28 03:14:52 +00:00
|
|
|
try:
|
|
|
|
user = User.objects.get(username=username)
|
2018-07-26 17:59:58 +00:00
|
|
|
raise forms.ValidationError(_(
|
|
|
|
"That username is already in use, please choose another."
|
|
|
|
))
|
2013-11-28 03:14:52 +00:00
|
|
|
except User.DoesNotExist:
|
|
|
|
self.instance.user = User(username=username)
|
|
|
|
return username
|
2018-07-26 17:59:58 +00:00
|
|
|
|
|
|
|
|
2013-11-28 03:14:52 +00:00
|
|
|
class Meta:
|
|
|
|
model = Library
|
|
|
|
fields = 'name', 'backend', 'email', 'username'
|
|
|
|
widgets = {'name':forms.TextInput(attrs={'size':'40'})}
|
|
|
|
|
2018-07-26 17:59:58 +00:00
|
|
|
class LibraryForm(forms.ModelForm):
|
2013-11-28 03:14:52 +00:00
|
|
|
class Meta:
|
|
|
|
model = Library
|
2018-07-26 17:59:58 +00:00
|
|
|
fields = 'name', 'backend',
|