summaryrefslogtreecommitdiff
path: root/models.py
blob: 7415b7fb579d0a43730cdd6fa5cfcfb3de4665e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 8 characters.
        if not len(new_password) >= 8:
            raise forms.ValidationError('Your password needs to be at \
least 8 characters long. Currently %d characters.' % len(new_password))
        # The password needs to contain at least one number, one upper
        # and one lower case letter and one special character.
        if not re.search('\d+', new_password):
            raise forms.ValidationError('You need at least one number \
in your password.')
        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.')
        if not re.search('[,.\[\]!@#$%^&*?_\(\)-]', new_password):
            raise forms.ValidationError('You need at least one special \
character i.e. ,.][!@#$%^&*?_()-')
        return cleaned_data


class ChangeOtherForm(forms.Form):
    new_attrib = forms.Textarea()