|
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 |
|
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 |
|
14
|
|
|
|
|
15
|
|
|
from .models import Workshop, WorkshopRatingValues, WorkshopFeedBack |
|
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
|
|
|
|