1
|
|
|
import datetime |
2
|
|
|
|
3
|
|
|
from django import forms |
4
|
|
|
from django.conf import settings |
5
|
|
|
# from django.utils.text import slugify |
6
|
|
|
from django.contrib.auth.models import User |
7
|
|
|
from django.core.exceptions import ValidationError |
8
|
|
|
|
9
|
|
|
from wye.base.constants import ( |
10
|
|
|
WorkshopRatings, |
11
|
|
|
WorkshopLevel, |
12
|
|
|
WorkshopStatus, |
13
|
|
|
FeedbackType) |
14
|
|
|
from wye.base.widgets import CalendarWidget |
15
|
|
|
from wye.organisations.models import Organisation |
16
|
|
|
from wye.profiles.models import Profile |
17
|
|
|
from wye.regions.models import RegionalLead, Location, State |
18
|
|
|
|
19
|
|
|
from .models import ( |
20
|
|
|
Workshop, |
21
|
|
|
WorkshopRatingValues, |
22
|
|
|
WorkshopFeedBack, |
23
|
|
|
WorkshopSections) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class WorkshopForm(forms.ModelForm): |
27
|
|
|
|
28
|
|
View Code Duplication |
def __init__(self, *args, **kwargs): |
|
|
|
|
29
|
|
|
user = kwargs.pop('user') |
30
|
|
|
super(WorkshopForm, self).__init__(*args, **kwargs) |
31
|
|
|
self.fields['expected_date'] = forms.DateField( |
32
|
|
|
widget=CalendarWidget, |
33
|
|
|
input_formats=settings.ALLOWED_DATE_FORMAT) |
34
|
|
|
self.fields['requester'].queryset = self.get_organisations(user) |
35
|
|
|
self.fields['location'].required = False |
36
|
|
|
self.fields['location'].widget = forms.HiddenInput() |
37
|
|
|
self.fields['workshop_section'].queryset = WorkshopSections.objects.filter(is_active=True) |
38
|
|
|
|
39
|
|
|
def clean_location(self): |
40
|
|
|
if "requester" not in self.cleaned_data: |
41
|
|
|
return "" |
42
|
|
|
|
43
|
|
|
organisation = self.cleaned_data['requester'] |
44
|
|
|
return organisation.location |
45
|
|
|
|
46
|
|
|
def get_organisations(self, user): |
47
|
|
|
if Profile.is_admin(user): |
48
|
|
|
return Organisation.objects.all() |
49
|
|
|
elif Profile.is_regional_lead(user): |
50
|
|
|
return Organisation.objects.filter(location=user.profile.location) |
51
|
|
|
else: |
52
|
|
|
return Organisation.list_user_organisations(user) |
53
|
|
|
|
54
|
|
|
def clean_expected_date(self): |
55
|
|
|
date = self.cleaned_data['expected_date'] |
56
|
|
|
if not (date > datetime.date.today() + datetime.timedelta(days=14)): |
57
|
|
|
raise ValidationError( |
58
|
|
|
'''Workshop request has to future date and |
59
|
|
|
atleast 2 weeks ahead of today''') |
60
|
|
|
else: |
61
|
|
|
return date |
62
|
|
|
|
63
|
|
|
class Meta: |
64
|
|
|
model = Workshop |
65
|
|
|
exclude = ( |
66
|
|
|
'presenter', 'created_at', 'modified_at', |
67
|
|
|
'number_of_volunteers', 'volunteer', |
68
|
|
|
'is_active', 'status',) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
class WorkshopEditForm(forms.ModelForm): |
72
|
|
|
requester = forms.CharField() |
73
|
|
|
|
74
|
|
View Code Duplication |
def __init__(self, request, *args, **kwargs): |
|
|
|
|
75
|
|
|
super(WorkshopEditForm, self).__init__(*args, **kwargs) |
76
|
|
|
self.fields['expected_date'] = forms.DateField( |
77
|
|
|
widget=CalendarWidget, |
78
|
|
|
input_formats=settings.ALLOWED_DATE_FORMAT) |
79
|
|
|
self.fields['requester'].widget = forms.TextInput() |
80
|
|
|
self.fields['requester'].widget.attrs['readonly'] = True |
81
|
|
|
if RegionalLead.is_regional_lead( |
82
|
|
|
request.user, self.instance.location): |
83
|
|
|
self.fields['presenter'].queryset = User.objects.filter( |
84
|
|
|
profile__usertype__slug="tutor" |
85
|
|
|
) |
86
|
|
|
else: |
87
|
|
|
del self.fields['presenter'] |
88
|
|
|
|
89
|
|
|
def clean_requester(self): |
90
|
|
|
return self.instance.requester |
91
|
|
|
|
92
|
|
|
class Meta: |
93
|
|
|
model = Workshop |
94
|
|
|
exclude = ( |
95
|
|
|
'created_at', 'modified_at', |
96
|
|
|
'number_of_volunteers', 'volunteer', |
97
|
|
|
'is_active', 'status', 'location') |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
class WorkshopFeedbackForm(forms.Form): |
101
|
|
|
""" |
102
|
|
|
Dynamically generates feedback form depending on questions available |
103
|
|
|
in model assigend to question_model. |
104
|
|
|
""" |
105
|
|
|
|
106
|
|
|
question_model = WorkshopRatingValues |
107
|
|
|
|
108
|
|
|
def __init__(self, user, id, *args, **kwargs): |
109
|
|
|
super(WorkshopFeedbackForm, self).__init__(*args, **kwargs) |
110
|
|
|
w = Workshop.objects.get(id=id) |
111
|
|
|
if user in w.presenter.all(): |
112
|
|
|
feedback_type = FeedbackType.PRESENTER |
113
|
|
|
elif user in w.requester.user.all(): |
114
|
|
|
feedback_type = FeedbackType.ORGANISATION |
115
|
|
|
else: |
116
|
|
|
feedback_type = None |
117
|
|
|
questions = self.question_model.get_questions(feedback_type) |
118
|
|
|
|
119
|
|
|
for question in questions: |
120
|
|
|
key = "{}".format(question) |
121
|
|
|
self.fields[key] = forms.ChoiceField( |
122
|
|
|
choices=WorkshopRatings.CHOICES, required=True, |
123
|
|
|
widget=forms.RadioSelect()) |
124
|
|
|
self.fields[key].label = question.name |
125
|
|
|
|
126
|
|
|
self.fields["comment"] = forms.CharField(widget=forms.Textarea) |
127
|
|
|
|
128
|
|
|
def save(self, user, workshop_id): |
129
|
|
|
print(dir(self)) |
130
|
|
|
data = {k: v for k, v in self.cleaned_data.items()} |
131
|
|
|
WorkshopFeedBack.save_feedback(user, workshop_id, **data) |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
class WorkshopListForm(forms.Form): |
135
|
|
|
""" |
136
|
|
|
Form to filter workshop list |
137
|
|
|
""" |
138
|
|
|
state = forms.ModelMultipleChoiceField( |
139
|
|
|
label="Workshop Location", |
140
|
|
|
required=False, |
141
|
|
|
queryset='') |
142
|
|
|
|
143
|
|
|
presenter = forms.ModelMultipleChoiceField( |
144
|
|
|
label="Presenter", |
145
|
|
|
required=False, |
146
|
|
|
queryset='') |
147
|
|
|
|
148
|
|
|
level = forms.MultipleChoiceField( |
149
|
|
|
label="Level", |
150
|
|
|
required=False, |
151
|
|
|
choices=WorkshopLevel.CHOICES) |
152
|
|
|
|
153
|
|
|
section = forms.ModelMultipleChoiceField( |
154
|
|
|
label="Section", |
155
|
|
|
required=False, |
156
|
|
|
queryset='') |
157
|
|
|
|
158
|
|
|
status = forms.MultipleChoiceField( |
159
|
|
|
label="Status", |
160
|
|
|
required=False, |
161
|
|
|
choices=WorkshopStatus.CHOICES) |
162
|
|
|
|
163
|
|
|
def __init__(self, *args, **kwargs): |
164
|
|
|
user = kwargs.pop('user') |
165
|
|
|
super(WorkshopListForm, self).__init__(*args, **kwargs) |
166
|
|
|
self.fields['state'].queryset = self.get_all_locations(user) |
167
|
|
|
if Profile.is_admin(user) or Profile.is_regional_lead(user): |
168
|
|
|
self.fields['presenter'].queryset = User.objects.filter( |
169
|
|
|
profile__usertype__slug="tutor" |
170
|
|
|
) |
171
|
|
|
elif 'poc' in user.profile.get_user_type: |
172
|
|
|
self.fields['presenter'].queryset = User.objects.filter( |
173
|
|
|
profile__usertype__slug="tutor", |
174
|
|
|
profile__location__state__in=self.get_all_states(user) |
175
|
|
|
) |
176
|
|
|
else: |
177
|
|
|
del self.fields['presenter'] |
178
|
|
|
self.fields['section'].queryset = WorkshopSections.objects.all() |
179
|
|
|
|
180
|
|
|
def get_all_locations(self, user): |
181
|
|
|
if Profile.is_admin(user): |
182
|
|
|
return Location.objects.all() |
183
|
|
|
else: |
184
|
|
|
return user.profile.interested_locations.all() |
185
|
|
|
|
186
|
|
|
def get_all_states(self, user): |
187
|
|
|
if Profile.is_admin(user): |
188
|
|
|
return State.objects.all() |
189
|
|
|
else: |
190
|
|
|
return user.profile.interested_states.all() |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
class WorkshopVolunteer(forms.Form): |
194
|
|
|
CHOICE_LIST = ((idx, idx) for idx in range(0, 6)) |
195
|
|
|
number_of_volunteers = forms.ChoiceField(choices=CHOICE_LIST) |
196
|
|
|
|