1
|
|
|
from django import forms |
2
|
|
|
from django.forms import FileField, DateField, ChoiceField, Widget, BooleanField, CheckboxInput |
3
|
|
|
|
4
|
|
|
from chat.models import UserProfile |
5
|
|
|
from chat.settings import GENDERS |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class DateWidget(forms.widgets.DateInput): |
9
|
|
|
""" |
10
|
|
|
Replace input in favor of html5 datepicker |
11
|
|
|
""" |
12
|
|
|
input_type = 'date' |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class OnlyTextWidget(Widget): |
16
|
|
|
def render(self, name, value, attrs=None): |
17
|
|
|
if name == 'sex': |
18
|
|
|
return GENDERS[value] |
19
|
|
|
else: |
20
|
|
|
return value |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class UserProfileReadOnlyForm(forms.ModelForm): |
24
|
|
|
def __init__(self, *args, **kwargs): |
25
|
|
|
super(UserProfileReadOnlyForm, self).__init__(*args, **kwargs) |
26
|
|
|
for field in self: |
27
|
|
|
field.field.widget = OnlyTextWidget() |
28
|
|
|
|
29
|
|
|
class Meta: # pylint: disable=C1001 |
30
|
|
|
model = UserProfile |
31
|
|
|
fields = ('username', 'name', 'surname', 'city', 'email', 'birthday', 'contacts', 'sex') |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
class BooleanWidget(CheckboxInput): |
35
|
|
|
|
36
|
|
|
def render(self, name, value, attrs=None): |
37
|
|
|
return super(BooleanWidget, self).render(name, value, attrs) + '<label for="id_'+name+'"></label>' |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
class UserProfileForm(forms.ModelForm): |
41
|
|
|
# the widget gets rid of <a href= |
42
|
|
|
photo = FileField(widget=forms.FileInput) |
43
|
|
|
birthday = DateField(widget=DateWidget) # input_formats=settings.DATE_INPUT_FORMATS |
44
|
|
|
notifications = BooleanField(widget=BooleanWidget) |
45
|
|
|
suggestions = BooleanField(widget=BooleanWidget) |
46
|
|
|
logs = BooleanField(widget=BooleanWidget) |
47
|
|
|
|
48
|
|
|
GENDER_CHOICES = ( |
49
|
|
|
(1, 'Male'), |
50
|
|
|
(2, 'Female'), |
51
|
|
|
(0, 'Alien'), |
52
|
|
|
) |
53
|
|
|
# implement here to set required = remove ---- choice in favor of alien |
54
|
|
|
sex = ChoiceField(required=True, choices=GENDER_CHOICES) |
55
|
|
|
|
56
|
|
|
class Meta: # pylint: disable=C1001 |
57
|
|
|
model = UserProfile |
58
|
|
|
fields = ('username', 'name', 'city', 'surname', 'email', 'birthday', 'contacts', |
59
|
|
|
'sex', 'photo', 'notifications', 'suggestions', 'logs') |
60
|
|
|
|
61
|
|
|
def __init__(self, *args, **kwargs): |
62
|
|
|
""" |
63
|
|
|
Creates the entire form for changing UserProfile. |
64
|
|
|
""" |
65
|
|
|
super(UserProfileForm, self).__init__(*args, **kwargs) |
66
|
|
|
|
67
|
|
|
for key in self.fields: |
68
|
|
|
if key != 'username': |
69
|
|
|
self.fields[key].required = False |
70
|
|
|
|