1
|
|
|
from django import forms |
2
|
|
|
from django.forms import FileField, DateField, ChoiceField, Widget, BooleanField, CheckboxInput, PasswordInput |
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 PasswordWidget(PasswordInput): |
41
|
|
|
|
42
|
|
|
def render(self, name, value, attrs=None): |
43
|
|
|
return """<div> |
44
|
|
|
<label>Old password</label> |
45
|
|
|
<input id="id_old_password" type="password" name="old_password"/> |
46
|
|
|
</div> |
47
|
|
|
<div> |
48
|
|
|
<label>New password</label> |
49
|
|
|
<input id="id_password" type="password" name="password" minlength="3"/> |
50
|
|
|
</div> |
51
|
|
|
<div> |
52
|
|
|
<label>Confirm password</label> |
53
|
|
|
<input id="id_repeat_password" type="password" minlength="3"/> |
54
|
|
|
</div> """ |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class UserProfileForm(forms.ModelForm): |
58
|
|
|
# the widget gets rid of <a href= |
59
|
|
|
photo = FileField(widget=forms.FileInput) |
60
|
|
|
birthday = DateField(widget=DateWidget) # input_formats=settings.DATE_INPUT_FORMATS |
61
|
|
|
suggestions = BooleanField(widget=BooleanWidget) |
62
|
|
|
logs = BooleanField(widget=BooleanWidget) |
63
|
|
|
embedded_youtube =BooleanField(widget=BooleanWidget) |
64
|
|
|
send_logs = BooleanField(widget=BooleanWidget) |
65
|
|
|
highlight_code = BooleanField(widget=BooleanWidget, help_text="```console.log('Highlight code like this')```") |
66
|
|
|
incoming_file_call_sound = BooleanField(widget=BooleanWidget) |
67
|
|
|
message_sound = BooleanField(widget=BooleanWidget) |
68
|
|
|
online_change_sound = BooleanField(widget=BooleanWidget) |
69
|
|
|
password = forms.CharField(widget=PasswordWidget) |
70
|
|
|
THEME_CHOICES = ( |
71
|
|
|
('color-reg', 'Modern'), |
72
|
|
|
('color-lor', 'Simple'), |
73
|
|
|
('color-white', 'Light(Beta)'), |
74
|
|
|
) |
75
|
|
|
theme = ChoiceField(required=True, choices=THEME_CHOICES) |
76
|
|
|
GENDER_CHOICES = ( |
77
|
|
|
(1, 'Male'), |
78
|
|
|
(2, 'Female'), |
79
|
|
|
(0, 'Alien'), |
80
|
|
|
) |
81
|
|
|
# implement here to set required = remove ---- choice in favor of alien |
82
|
|
|
sex = ChoiceField(required=True, choices=GENDER_CHOICES) |
83
|
|
|
|
84
|
|
|
class Meta: # pylint: disable=C1001 |
85
|
|
|
model = UserProfile |
86
|
|
|
fields = ('username', 'name', 'city', 'surname', 'email', 'birthday', 'contacts', 'sex', 'photo', 'suggestions', 'embedded_youtube', 'highlight_code', 'message_sound', 'incoming_file_call_sound', 'online_change_sound', 'logs', 'send_logs', 'theme', 'password') |
87
|
|
|
|
88
|
|
|
def __init__(self, *args, **kwargs): |
89
|
|
|
""" |
90
|
|
|
Creates the entire form for changing UserProfile. |
91
|
|
|
""" |
92
|
|
|
super(UserProfileForm, self).__init__(*args, **kwargs) |
93
|
|
|
|
94
|
|
|
for key in self.fields: |
95
|
|
|
if key != 'username': |
96
|
|
|
self.fields[key].required = False |
97
|
|
|
|