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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Button, ButtonHolder, Field, Hidden, Layout, Submit
from django import forms
from django.core.exceptions import ValidationError
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.safestring import mark_safe
from ctrack.caf.models import CAF
from ctrack.organisations.models import Organisation, Person
from ctrack.register.models import (
CAFSingleDateEvent,
CAFTwinDateEvent,
EngagementEvent,
EngagementType,
SingleDateTimeEvent,
NoteEvent,
)
class CreateNoteEventForm(forms.ModelForm):
class Meta:
model = NoteEvent
fields = [
"short_description",
"organisation",
"comments",
"private",
"url",
"requested_response_date",
"response_received_date",
]
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
if kwargs.get("org_slug"):
self.org_slug = kwargs.pop("org_slug")
super().__init__(*args, **kwargs)
self.fields["organisation"].queryset = Organisation.objects.filter(slug=self.org_slug)
else:
super().__init__(*args, **kwargs)
self.fields["organisation"].queryset = Organisation.objects.all().order_by('name')
def save(self, commit=True, **kwargs):
new_note = super().save(commit=False)
new_note.user = self.user
new_note.save()
self.save_m2m()
return new_note
class CreateSimpleDateTimeEventForm(forms.ModelForm):
class Meta:
model = SingleDateTimeEvent
fields = [
"type_descriptor",
"private",
"short_description",
"date",
"participants",
"requested_response_date",
"response_received_date",
"url",
"location",
"comments",
]
widgets = {"participants": forms.CheckboxSelectMultiple()}
def __init__(self, *args, **kwargs):
self.event_type = None
self.user = kwargs.pop("user")
self.org_slug = kwargs.pop("org_slug")
try:
self.event_type = kwargs.pop("event_type")
except KeyError:
pass
super().__init__(*args, **kwargs)
if self.org_slug:
org = Organisation.objects.get(slug=self.org_slug)
self.fields["participants"].queryset = org.get_people()
self.fields["participants"].help_text = mark_safe(
f"Click to select participants from {org}. <strong>IMPORTANT:</strong>"
f"You must select at least one participant."
)
if self.event_type:
self.fields["type_descriptor"].initial = self.event_type
else:
self.fields["participants"].widget = forms.HiddenInput()
def clean(self):
cleaned_data = super().clean()
date = cleaned_data.get("date")
if not date:
return cleaned_data
# WOOO - walrus operator
if requested := cleaned_data.get("requested_response_date"):
if requested < date.date():
raise ValidationError("Requested response cannot be before date.")
return cleaned_data
def save(self, commit=True, **kwargs):
new_event = super().save(commit=False)
new_event.user = self.user
new_event.save()
self.save_m2m()
return new_event
class CAFSingleDateEventForm(forms.ModelForm):
class Meta:
model = CAFSingleDateEvent
fields = [
"type_descriptor",
"date",
"short_description",
"document_link",
"comments",
]
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
self.caf_id = kwargs.pop("caf_id")
super().__init__(*args, **kwargs)
def save(self, **kwargs):
form = super().save(commit=False)
form.user = self.user
form.related_caf = CAF.objects.get(id=self.caf_id)
form.save()
return form
class CAFTwinDateEventForm(forms.ModelForm):
# This constraint in the form prevents two such objects being created
# for the same CAF with the same start date, which does not make sense.
def clean_date(self):
data = self.cleaned_data["date"]
caf = self.cleaned_data["related_caf"]
existing_obj = (
CAFTwinDateEvent.objects.filter(date=data)
.filter(related_caf=caf)
.first()
)
if existing_obj:
raise ValidationError(
"You cannot have two CAF events starting on the same date."
)
return data
class Meta:
model = CAFTwinDateEvent
fields = [
"type_descriptor",
"related_caf",
"short_description",
"date",
"end_date",
"comments",
]
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
super().__init__(*args, **kwargs)
def save(self, **kwargs):
form = super().save(commit=False)
form.user = self.user
form.save()
return form
class EngagementEventCreateForm(forms.ModelForm):
def __init__(self, user, caf=None, org_slug=None, *args, **kwargs):
super().__init__(*args, **kwargs)
if caf:
org = CAF.objects.get(pk=caf).organisation
cancel_redirect = reverse("caf:detail", args=[caf])
self.fields["related_caf"].initial = caf
self.fields["participants"].queryset = Person.objects.filter(
organisation__pk=org.pk
)
self.fields["type"].queryset = EngagementType.objects.all().order_by(
"descriptor"
)
self.helper = FormHelper(self)
self.helper.layout = Layout(
Field("type"),
"short_description",
"participants",
"related_caf",
# "user",
Hidden("user", "none"),
"date",
"end_date",
"response_date_requested",
"response_received",
"document_link",
"comments",
ButtonHolder(
Submit("submit", "Submit", css_class="btn-primary"),
Button(
"cancel",
"Cancel",
onclick=f"location.href='{cancel_redirect}';",
css_class="btn-danger",
),
),
)
else:
org = get_object_or_404(Organisation, slug=org_slug)
cancel_redirect = reverse("organisations:detail", args=[org_slug])
selectable_people = Person.objects.filter(organisation__slug=org_slug)
self.fields["participants"].queryset = selectable_people
self.fields["participants"].initial = selectable_people.first()
self.fields["type"].queryset = EngagementType.objects.all().order_by(
"descriptor"
)
self.fields["related_caf"].queryset = org.caf_set.all()
self.fields["related_caf"].label = "Related CAFs"
self.helper = FormHelper(self)
self.helper.layout = Layout(
Field("type"),
"short_description",
"participants",
"related_caf",
# "user",
Hidden("user", "none"),
"date",
"end_date",
"response_date_requested",
"response_received",
"document_link",
"comments",
ButtonHolder(
Submit("submit", "Submit", css_class="btn-primary"),
Button(
"cancel",
"Cancel",
onclick=f"location.href='{cancel_redirect}';",
css_class="btn-danger",
),
),
)
def save(self, commit=True):
ee = super().save(commit=False)
if commit:
ee.save()
self.save_m2m() # so that we also save the peoples!
return ee
class Meta:
model = EngagementEvent
fields = "__all__"
exclude = ["user"]
widgets = {
"date": forms.DateTimeInput(attrs={"type": "date"}),
"response_date_requested": forms.DateTimeInput(attrs={"type": "date"}),
"response_received": forms.DateTimeInput(attrs={"type": "date"}),
"end_date": forms.DateTimeInput(attrs={"type": "date"}),
}
|