summaryrefslogtreecommitdiffstats
path: root/engagements/forms.py
blob: ddd7aa0c3fbe755c0a646c915711bd0e5430d1e4 (plain) (blame)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from django import forms
from django.core.exceptions import ValidationError
from django.forms.widgets import HiddenInput

from .models import Engagement, EngagementEffort, EngagementStrategy, EngagementType, Organisation


class EngagementEffortReportingCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["is_planned"].widget.attrs.update({"class": "select-lg"})
        # for field in self.fields.values():
        #     field.widget.attrs['class'] = 'mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50'

    class Meta:
        model = EngagementEffort
        help_texts = {
            "is_planned": ("To distinguish planned events from retrospective recording."),
            "officers": ("Include yourself here but you can also add effort for your colleagues."),
        }
        fields = [
            "is_planned",
            "proposed_start_date",
            "proposed_end_date",
            "officers",
            "notes",
        ]
        widgets = {
            # 'is_planned': forms.Select(
            #     choices=(
            #         (True, "YES"),
            #         (False, "NO"),
            #     ),
            #     attrs={'class': 'select-lg w-full max-w-xs'}
            # ),
            "proposed_start_date": forms.DateTimeInput(
                attrs={
                    "type": "datetime-local",
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50",
                },
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateTimeInput(
                attrs={
                    "type": "datetime-local",
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50",
                },
                format="j M y H:i",
            ),
            "officers": forms.SelectMultiple(
                attrs={
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
                }
            ),
            "notes": forms.Textarea(
                attrs={
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50",
                    "rows": 3,
                }
            ),
        }


class EngagementEffortRegulationCreateForm(forms.ModelForm):
    class Meta:
        model = EngagementEffort
        fields = [
            "is_planned",
            "proposed_start_date",
            "proposed_end_date",
            "officers",
            "sub_instruments",
            "notes",
        ]
        help_texts = {"is_planned": ("To distinguish planned events from retrospective recording.")}
        widgets = {
            "proposed_start_date": forms.DateTimeInput(
                attrs={
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateTimeInput(
                attrs={
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
        }


class EngagementEffortPlanningCreateForm(forms.ModelForm):
    class Meta:
        model = EngagementEffort
        fields = [
            "is_planned",
            "proposed_start_date",
            "proposed_end_date",
            "officers",
            "notes",
        ]
        help_texts = {"is_planned": ("To distinguish planned events from retrospective recording.")}
        widgets = {
            "proposed_start_date": forms.DateTimeInput(
                attrs={
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateTimeInput(
                attrs={
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
        }


class EngagementEffortTravelCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["is_planned"].widget.attrs.update({"class": "select-lg"})
        for field in self.fields.values():
            field.widget.attrs["class"] = (
                "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
            )

    class Meta:
        model = EngagementEffort
        fields = ["is_planned", "proposed_start_date", "proposed_end_date", "officers"]
        widgets = {
            "is_planned": forms.Select(
                choices=((True, "YES"), (False, "NO")), attrs={"class": "select select-bordered w-full max-w-xs"}
            ),
            "proposed_start_date": forms.DateTimeInput(attrs={"type": "datetime-local"}),
            "proposed_end_date": forms.DateTimeInput(attrs={"type": "datetime-local"}),
        }
        help_texts = {"is_planned": ("To distinguish planned events from retrospective recording.")}


class EngagementEffortCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["engagement"].widget = HiddenInput()
        self.fields["effort_type"].widget = HiddenInput()
        if kwargs.get("initial"):
            if not kwargs["initial"]["effort_type"] == "REGULATION":
                self.fields["sub_instruments"].widget = HiddenInput()

        # Apply Tailwind classes to all visible fields
        # for field in self.visible_fields():
        #     field.widget.attrs['class'] = 'mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50'

    class Meta:
        model = EngagementEffort
        fields = "__all__"
        widgets = {
            # "is_planned": forms.Select(
            #     choices=(
            #         (True, "YES"),
            #         (False, "NO"),
            #     ),
            #     attrs={'class': 'mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50'}
            # ),
            "proposed_start_date": forms.DateTimeInput(
                attrs={
                    "type": "datetime-local",
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50",
                },
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateTimeInput(
                attrs={
                    "type": "datetime-local",
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50",
                },
                format="j M y H:i",
            ),
            "effort_type": forms.Select(
                attrs={
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
                }
            ),
            "officers": forms.SelectMultiple(
                attrs={
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
                }
            ),
            "sub_instruments": forms.SelectMultiple(
                attrs={
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
                }
            ),
        }


class EngagementCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        try:
            self.fields["engagement_type"].queryset = kwargs["initial"]["engagement_type"]
        except KeyError:
            pass

    def clean_proposed_end_date(self):
        proposed_start_date = self.cleaned_data["proposed_start_date"]
        proposed_end_date = self.cleaned_data["proposed_end_date"]

        if proposed_start_date and proposed_end_date:
            if proposed_start_date > proposed_end_date:
                raise ValidationError("The proposed start date must be before the proposed end date.")

        return proposed_end_date

    class Meta:
        model = Engagement
        fields = [
            "proposed_start_date",
            "proposed_end_date",
            "engagement_type",
            "external_party",
            "officers",
        ]
        labels = {
            "officers": "Inspectors",
        }
        help_texts = {
            "external_party": "These are the people you are dealing with.",
            "proposed_start_date": "YYYY-MM-DD",
            "proposed_end_date": "YYYY-MM-DD",
            "officers": "Use CTRL to select multiple if required.",
        }
        widgets = {
            "proposed_start_date": forms.DateInput(attrs={"type": "date"}),
            "proposed_end_date": forms.DateInput(attrs={"type": "date"}),
            "engagement_type": forms.Select(),
            "officers": forms.SelectMultiple(),
        }


class EngagementStrategyCreateForm(forms.ModelForm):
    class Meta:
        fields = "__all__"
        model = EngagementStrategy


class AssessmentCreateForm(forms.ModelForm):
    class Meta:
        model = Engagement
        fields = ["proposed_start_date", "proposed_end_date", "officers", "engagement_type", "external_party"]
        widgets = {
            "proposed_start_date": forms.DateInput(
                attrs={
                    "type": "date",
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50",
                },
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateInput(
                attrs={
                    "type": "date",
                    "class": "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50",
                },
                format="j M y H:i",
            ),
        }

    def __init__(self, *args, org, **kwargs):
        super().__init__(*args, **kwargs)
        entity = Organisation.objects.get(slug=org)
        et = EngagementType.objects.get(name="ASSESSMENT")
        self.initial["engagement_type"] = et.pk
        self.initial["external_party"] = entity.pk
        self.fields["engagement_type"].widget= forms.HiddenInput()
        self.fields["engagement_type"].label = ""
        self.fields["external_party"].widget= forms.HiddenInput()
        self.fields["external_party"].label = ""