summaryrefslogtreecommitdiff
path: root/src/apps/changepw/models.py
diff options
context:
space:
mode:
authorMarkus Krogh <markus@nordu.net>2017-06-02 13:19:30 +0200
committerMarkus Krogh <markus@nordu.net>2017-06-02 13:19:30 +0200
commit934702f61f1cbdbf001ebb598c22c75efa247645 (patch)
treeb0c6725a8c8a682b421aa35eea9662d7fff31bd6 /src/apps/changepw/models.py
parent41afbaae97384968df6312cbe570305208b2216e (diff)
Django 1.11 compatible and cleaned up
Diffstat (limited to 'src/apps/changepw/models.py')
-rw-r--r--src/apps/changepw/models.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/apps/changepw/models.py b/src/apps/changepw/models.py
new file mode 100644
index 0000000..24e0bec
--- /dev/null
+++ b/src/apps/changepw/models.py
@@ -0,0 +1,34 @@
+from django import forms
+import re
+
+class ChangePasswordForm(forms.Form):
+ new_password = forms.CharField(widget=forms.PasswordInput)
+ new_password_again = forms.CharField(widget=forms.PasswordInput)
+
+ def clean(self):
+ '''
+ Validate the password submitted.
+ '''
+ cleaned_data = self.cleaned_data
+ # The two submitted strings need to match.
+ new_password = cleaned_data.get('new_password')
+ new_password_again = cleaned_data.get('new_password_again')
+ if new_password != new_password_again:
+ raise forms.ValidationError('The typed passwords do not \
+match.')
+ # Check that the length is at least 10 characters.
+ if not len(new_password) >= 10:
+ raise forms.ValidationError('Your password needs to be at \
+least 10 characters long. Currently %d characters.' % len(new_password))
+ # The password needs to contain at least one upper and one lower case
+ # letter and three numbers or special characters.
+ if not re.search('[a-z]', new_password) or not re.search(
+ '[A-Z]', new_password):
+ raise forms.ValidationError('You need at least one upper \
+case letter and one lower case letter in your password.')
+ numbers = re.findall('\d', new_password)
+ specials = re.findall('[,.\[\]!@#$%^&*?_\(\)-]', new_password)
+ if (len(numbers)+len(specials)) < 3:
+ raise forms.ValidationError('You need at least three numbers or \
+special characters i.e. 1234567890,.][!@#$%^&*?_()-')
+ return cleaned_data