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