forked from marcopoli/PugliaeventiRecommender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
135 lines (103 loc) · 3.91 KB
/
forms.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from django import forms
from django.forms.widgets import PasswordInput
from ajax_select.fields import AutoCompleteField
from django.utils import timezone
from recommender_webapp.common.utils import ChoiceEnum
from recommender_webapp.models import User, Profile, Comune, Companionship, Mood
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('location',)
location = AutoCompleteField('cities')
def clean(self, *args, **kwargs):
location = self.cleaned_data.get('location')
location_found = Comune.objects.filter(nome__iexact=location)
if not location_found.exists():
raise forms.ValidationError("Location not found")
class UserRegisterForm(forms.ModelForm):
email = forms.EmailField(label='Email address')
email2 = forms.EmailField(label='Confirm Email')
password = forms.CharField(widget=PasswordInput)
password2 = forms.CharField(widget=PasswordInput, label='Confirm Password')
class Meta:
model = User
fields = [
'email',
'email2',
'password',
'password2',
'first_name',
'last_name'
]
def clean(self, *args, **kwargs):
email = self.cleaned_data.get('email')
email2 = self.cleaned_data.get('email2')
password = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password2')
if email != email2:
raise forms.ValidationError("Emails must match")
if password != password2:
raise forms.ValidationError("Passwords must match")
email_qs = User.objects.filter(email=email)
if email_qs.exists():
raise forms.ValidationError("This email has already been registered")
return super(UserRegisterForm, self).clean(*args, **kwargs)
class SearchRecommendationDistanceRange(ChoiceEnum):
__order__ = 'unlimited km20 km40 km60'
unlimited = 0
km20 = 20
km40 = 40
km60 = 60
class SearchRecommendationForm(forms.Form):
mood = forms.ChoiceField(
required=True,
widget=forms.Select,
choices=[choice[::-1] for choice in Mood.choices()]
)
companionship = forms.ChoiceField(
required=True,
widget=forms.Select,
choices=[choice[::-1] for choice in Companionship.choices()]
)
km_range = forms.ChoiceField(
required=False,
widget=forms.Select,
choices=[choice[::-1] for choice in SearchRecommendationDistanceRange.choices()],
label='Distance'
)
any_events = forms.BooleanField(initial=False, required=False, label='Any events')
class AddRatingForm(forms.Form):
mood = forms.ChoiceField(
required=True,
widget=forms.Select,
choices=[choice[::-1] for choice in Mood.choices()]
)
companionship = forms.ChoiceField(
required=True,
widget=forms.Select,
choices=[choice[::-1] for choice in Companionship.choices()]
)
class SearchPlacesDistanceRange(ChoiceEnum):
__order__ = 'km5 km10'
km5 = 5
km10 = 10
class SearchNearPlacesForm(forms.Form):
km_range = forms.ChoiceField(
required=True,
widget=forms.Select,
choices=[choice[::-1] for choice in SearchPlacesDistanceRange.choices()]
)
def past_years(ago):
this_year = timezone.now().year
return list(range(this_year - ago - 1, this_year))
class FullProfileForm(forms.ModelForm):
birth_date = forms.DateField(widget=forms.SelectDateWidget(years=past_years(100)))
class Meta:
model = Profile
fields = ('location', 'profession', 'birth_date', 'bio')
location = AutoCompleteField('cities')
def clean(self, *args, **kwargs):
location = self.cleaned_data.get('location')
location_found = Comune.objects.filter(nome__iexact=location)
if not location_found.exists():
raise forms.ValidationError("Location not found")