forms.py 3.32 KB
Newer Older
1
2
from __future__ import unicode_literals

3
from django import forms
4
5
from django.contrib.auth.forms import UserChangeForm as BaseUserChangeForm
from django.contrib.auth.forms import UserCreationForm as BaseUserCreationForm
6
from django.contrib.auth.models import User
7
from django.utils.translation import ugettext_lazy as _
8

9
from .models import Profile
10
from members import emails
11
12


13
class ProfileForm(forms.ModelForm):
14
15
16
17
    class Meta:
        fields = ['address_street', 'address_street2',
                  'address_postal_code', 'address_city', 'phone_number',
                  'emergency_contact', 'emergency_contact_phone_number',
18
                  'show_birthday', 'website',
19
20
21
                  'profile_description', 'nickname',
                  'display_name_preference', 'photo', 'language',
                  'receive_optin', 'receive_newsletter']
22
        model = Profile
23

24
    def clean(self):
25
26
        super().clean()
        errors = {}
27
28
29
        direct_debit_authorized = self.cleaned_data\
            .get('direct_debit_authorized')
        bank_account = self.cleaned_data.get('bank_account')
30
31
32
33
        if direct_debit_authorized and not bank_account:
            errors.update({'bank_account': _('Please enter a bank account')})

        raise forms.ValidationError(errors)
34

35

36
class UserCreationForm(BaseUserCreationForm):
37
38
39
    # Don't forget to edit the formset in admin.py!
    # This is a stupid quirk of the user admin.

40
41
42
43
44
    # shadow the password fields to prevent validation errors,
    #   since we generate the passwords dynamically.
    password1 = None
    password2 = None

45
46
47
48
49
50
51
52
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in ('email', 'first_name', 'last_name'):
            self.fields[field].required = True

    send_welcome_email = forms.BooleanField(
        label=_('Send welcome email'),
        help_text=_('This email will include the generated password'),
53
        required=False,
54
55
        initial=True)

56
    def clean(self):
57
58
59
        if 'username' in self.cleaned_data:
            self.cleaned_data['username'] = (self.cleaned_data['username']
                                             .lower())
60
61
        super().clean()

62
63
    def save(self, commit=True):
        password = User.objects.make_random_password(length=15)
64
65
        # pass the password on as if it was filled in, so that save() works
        self.cleaned_data['password1'] = password
66
67
68
69
70
        user = super().save(commit=False)
        user.set_password(password)
        if commit:
            user.save()
        if self.cleaned_data['send_welcome_email']:
71
            # Ugly way to get the language since member isn't available
72
            language = str(self.data.get('profile-0-language', 'en'))
73
74
            if language not in ('nl', 'en'):
                language = 'en'
75
            emails.send_welcome_message(user, password, language)
76
77
78
79
80
81
82
        return user

    class Meta:
        fields = ('username',
                  'first_name',
                  'last_name',
                  'send_welcome_email')
83
84


85
class UserChangeForm(BaseUserChangeForm):
86
87
88
89
90

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def clean(self):
91
92
93
        if 'username' in self.cleaned_data:
            self.cleaned_data['username'] = (self.cleaned_data['username']
                                             .lower())
94
        super().clean()