Completed
Push — master ( 6ca66a...a573ac )
by Andrew
01:29
created

OnlyTextWidget   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A render() 0 5 2
1
from django import forms
2
from django.forms import FileField, DateField, ChoiceField, Widget
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 UserProfileForm(forms.ModelForm):
35
	# the widget gets rid of <a href=
36
	photo = FileField(widget=forms.FileInput)
37
	birthday = DateField(widget=DateWidget)  # input_formats=settings.DATE_INPUT_FORMATS
38
39
	GENDER_CHOICES = (
40
		(1, 'Male'),
41
		(2, 'Female'),
42
		(0, 'Alien'),
43
	)
44
	# implement here to set required = remove ---- choice in favor of alien
45
	sex = ChoiceField(required=True, choices=GENDER_CHOICES)
46
47
	class Meta:  # pylint: disable=C1001
48
		model = UserProfile
49
		fields = ('username', 'name', 'city', 'surname', 'email', 'birthday', 'contacts', 'sex', 'photo')
50
51
	def __init__(self, *args, **kwargs):
52
		"""
53
		Creates the entire form for changing UserProfile.
54
		"""
55
		super(UserProfileForm, self).__init__(*args, **kwargs)
56
57
		for key in self.fields:
58
			if key != 'username':
59
				self.fields[key].required = False
60