Blame view

src/user_manager/forms.py 2.96 KB
94082e77   haribo   Date: 03/06/2016
1
from django import forms
ddf59dd4   haribo   Remaniement :
2
from common.models import PyrosUser, Country, UserLevel, ScientificProgram
c5ae1cae   theopuhl   Add send mail + v...
3
from django.core.mail import send_mail
3df2d31a   haribo   #3430 : dates are...
4

94082e77   haribo   Date: 03/06/2016
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class PyrosUserCreationForm(forms.ModelForm):
    '''
        Form used at user creation.
        
        IMPORTANT : The user must not be able to choose his Country, Scientific Program(s), Quota, Priority, and User Level 
    '''

    email = forms.EmailField(label="Email", widget=forms.TextInput)

    password = forms.CharField(label='Password', widget=forms.PasswordInput)
    password_confirm = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

    first_name = forms.CharField(label='First name')
    last_name = forms.CharField(label='Last name')

    class Meta:
        model = PyrosUser
        fields = ('tel', 'laboratory', 'address')
94082e77   haribo   Date: 03/06/2016
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

    def __init__(self, *args, **kwargs):
        super(PyrosUserCreationForm, self).__init__(*args, **kwargs)
        self.fields.move_to_end('tel', True)
        self.fields.move_to_end('laboratory', True)
        self.fields.move_to_end('address', True)
        for field in self.fields.values():
            field.required = True
            field.widget.attrs['class'] = "form-control"

    def clean_password_confirm(self):
        '''
            Checks if password and confirmation are equal
        '''
        password = self.cleaned_data.get("password")
        password_confirmation = self.cleaned_data.get("password_confirm")
        if password and password_confirmation and password != password_confirmation:
            raise forms.ValidationError("Passwords don't match")
        return password_confirmation

    def clean_email(self):
        '''
            Checks if the email already exists in DB
        '''
        email = self.cleaned_data.get("email")
53787d30   Jeremy   Alert now inherit...
48
        if PyrosUser.objects.filter(email=email).exists():
94082e77   haribo   Date: 03/06/2016
49
50
51
52
53
54
55
            raise forms.ValidationError("Email address already taken")
        return email

    def save(self):
        '''
            Creates a User and a PyrosUser in DB
        '''
53787d30   Jeremy   Alert now inherit...
56
        pyros_user = PyrosUser.objects.create(username=self.cleaned_data['email'], email=self.cleaned_data['email'], country=Country.objects.all()[0],
29ecf1c6   Quentin Durand   fix unittest
57
                                              user_level=UserLevel.objects.all()[0],
94082e77   haribo   Date: 03/06/2016
58
59
                                              tel=self.cleaned_data['tel'], laboratory=self.cleaned_data['laboratory'],
                                              address=self.cleaned_data['address'])
53787d30   Jeremy   Alert now inherit...
60
61
62
63
64
        pyros_user.set_password(self.cleaned_data['password'])
        pyros_user.first_name = self.cleaned_data['first_name']
        pyros_user.last_name = self.cleaned_data['last_name']
        pyros_user.save()

c5ae1cae   theopuhl   Add send mail + v...
65
66
67
68
69
70
71
        send_mail(
            '[COLIBRI CC] Registration',
            'Hi,\n\nThanks for your registration. The PI will examinate your account as soon as possible.\n\nCordialy,\n\nColibri Control Center',
            '',
            [self.cleaned_data['email']],
            fail_silently=False,
        )
94082e77   haribo   Date: 03/06/2016
72
        return pyros_user