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