2018-11-08 13:18:24 +00:00
|
|
|
from django.contrib.auth.password_validation import validate_password
|
2018-02-04 00:36:39 +00:00
|
|
|
from django.contrib.auth.forms import UserCreationForm
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-11-08 13:18:24 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.contrib import messages
|
|
|
|
from django import forms
|
2018-02-04 00:36:39 +00:00
|
|
|
from .models import NelUser
|
|
|
|
|
|
|
|
|
|
|
|
class RegistrationForm(UserCreationForm):
|
|
|
|
class Meta:
|
|
|
|
model = NelUser
|
|
|
|
fields = (NelUser.EMAIL_FIELD,)
|
2018-11-08 13:18:24 +00:00
|
|
|
|
2018-11-08 19:11:14 +00:00
|
|
|
|
2018-11-08 13:18:24 +00:00
|
|
|
class ChangePasswordForm(forms.Form):
|
2018-11-08 19:11:14 +00:00
|
|
|
current_password = forms.CharField(
|
2019-07-24 17:07:41 +00:00
|
|
|
label=_("current_password"), widget=forms.PasswordInput
|
2018-11-08 19:11:14 +00:00
|
|
|
)
|
2019-07-24 17:07:41 +00:00
|
|
|
new_password = forms.CharField(label=_("new_password"), widget=forms.PasswordInput)
|
2018-11-08 19:11:14 +00:00
|
|
|
new_password_confirm = forms.CharField(
|
2019-07-24 17:07:41 +00:00
|
|
|
label=_("new_password_confirm"), widget=forms.PasswordInput
|
2018-11-08 19:11:14 +00:00
|
|
|
)
|
2018-11-08 13:18:24 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-07-24 17:07:41 +00:00
|
|
|
self.request = kwargs.pop("request")
|
2018-11-08 13:18:24 +00:00
|
|
|
return super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
2019-07-24 17:07:41 +00:00
|
|
|
old_pass = cleaned_data.get("current_password")
|
|
|
|
new_pass = cleaned_data.get("new_password")
|
|
|
|
new_pass_confirm = cleaned_data.get("new_password_confirm")
|
2018-11-08 13:18:24 +00:00
|
|
|
user = self.request.user
|
|
|
|
if new_pass != new_pass_confirm:
|
2019-07-24 17:07:41 +00:00
|
|
|
msg = _("The new password does not match its confirmation.")
|
2018-11-08 19:11:14 +00:00
|
|
|
raise forms.ValidationError(msg)
|
2018-11-08 13:18:24 +00:00
|
|
|
try:
|
|
|
|
validate_password(new_pass, user=user)
|
|
|
|
except ValidationError as error:
|
|
|
|
raise forms.ValidationError(error)
|
|
|
|
if not user.check_password(old_pass):
|
2019-07-24 17:07:41 +00:00
|
|
|
msg = _("The current password is incorrect.")
|
2018-11-08 19:11:14 +00:00
|
|
|
raise forms.ValidationError(msg)
|
2018-11-08 14:29:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteAccountForm(forms.Form):
|
2018-11-08 19:11:14 +00:00
|
|
|
current_password = forms.CharField(
|
2019-07-24 17:07:41 +00:00
|
|
|
label=_("current_password"), widget=forms.PasswordInput
|
2018-11-08 19:11:14 +00:00
|
|
|
)
|
2018-11-08 14:29:59 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-07-24 17:07:41 +00:00
|
|
|
self.request = kwargs.pop("request")
|
2018-11-08 14:29:59 +00:00
|
|
|
return super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
2019-07-24 17:07:41 +00:00
|
|
|
password = cleaned_data.get("current_password")
|
2018-11-08 14:29:59 +00:00
|
|
|
user = self.request.user
|
|
|
|
if not user.check_password(password):
|
2019-07-24 17:07:41 +00:00
|
|
|
msg = _("The current password is incorrect.")
|
2018-11-08 19:11:14 +00:00
|
|
|
raise forms.ValidationError(msg)
|