forms.py
2.21 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
from django import forms
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm as UserCreationForm_
from django.utils.translation import ugettext_lazy as _
from .models import MailingList
from .validators import UniqueValidator
# XXX: I know that this code does not look nice AT ALL.
# probably it should be implemented using formsets instead of
# the hack below. Feel free to improve it! :)
# User fields
username_field = UserCreationForm_().fields.get(u'username')
first_name_field = forms.CharField(max_length=30, label=_(u'Name'))
last_name_field = forms.CharField(max_length=30, label=_(u'Last name'))
email_field = forms.EmailField(validators=[UniqueValidator(User, 'email')])
# UserProfile fields
institution_field = forms.CharField(max_length=120, label=_(u'Institution'),
required=False)
role_field = forms.CharField(max_length=60, label=_(u'Function'), required=False)
twitter_field = forms.URLField(label=_(u'Twitter'), required=False)
facebook_field = forms.URLField(label=_(u'Facebook'), required=False)
google_talk_field = forms.EmailField(label=_(u'Google Talk'), required=False)
webpage_field = forms.URLField(label=_(u'Personal Website/Blog'), required=False)
all_lists = MailingList.objects.all()
lists_names = []
for list_ in all_lists:
choice = (list_.name, list_.name)
lists_names.append(choice)
lists_field = forms.MultipleChoiceField(
label=u'Listas',
required=False,
widget=forms.CheckboxSelectMultiple,
choices=lists_names
)
class UserCreationForm(UserCreationForm_):
first_name = first_name_field
last_name = last_name_field
email = email_field
institution = institution_field
role = role_field
twitter = twitter_field
facebook = facebook_field
google_talk = google_talk_field
webpage = webpage_field
lists = lists_field
class UserUpdateForm(forms.Form):
username = username_field
username.required = False
institution = institution_field
role = role_field
twitter = twitter_field
facebook = facebook_field
google_talk = google_talk_field
webpage = webpage_field