diff options
author | Yulqen <246857+yulqen@users.noreply.github.com> | 2024-09-10 15:58:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-10 15:58:55 +0100 |
commit | dcfe6d87098bbe064d2db268f0972865c3d412bf (patch) | |
tree | 2b82df07a5290f80f25e9780596cbce0f128454f /engagements/forms.py | |
parent | 7ddc22b821f405546786977225e7b194a19b2b77 (diff) | |
parent | 15b2dc965bfe2271d73476fcf9ff636c60113908 (diff) |
Merge pull request #106 from defencedigital/postgres-migration
Adds new Engagement Strategy stuff
Diffstat (limited to '')
-rw-r--r-- | engagements/forms.py | 115 |
1 files changed, 73 insertions, 42 deletions
diff --git a/engagements/forms.py b/engagements/forms.py index 2532892..cccaf3b 100644 --- a/engagements/forms.py +++ b/engagements/forms.py @@ -1,16 +1,14 @@ from django import forms +from django.core.exceptions import ValidationError from django.forms.widgets import HiddenInput -from .models import Engagement, EngagementEffort - - -# TODO - need to handle errors correctly in this form and in the template +from .models import Engagement, EngagementEffort, EngagementStrategy class EngagementEffortReportingCreateForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.fields['is_planned'].widget.attrs.update({"class": "select-lg"}) + 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' @@ -18,14 +16,14 @@ class EngagementEffortReportingCreateForm(forms.ModelForm): 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.") + "officers": ("Include yourself here but you can also add effort for your colleagues."), } fields = [ - 'is_planned', - 'proposed_start_date', - 'proposed_end_date', - 'officers', - 'notes', + "is_planned", + "proposed_start_date", + "proposed_end_date", + "officers", + "notes", ] widgets = { # 'is_planned': forms.Select( @@ -35,27 +33,35 @@ class EngagementEffortReportingCreateForm(forms.ModelForm): # ), # attrs={'class': 'select-lg w-full max-w-xs'} # ), - 'proposed_start_date': forms.DateTimeInput( + "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' + "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', + format="j M y H:i", ), - 'proposed_end_date': forms.DateTimeInput( + "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' + "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', + 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, + } ), - '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 = [ @@ -66,9 +72,7 @@ class EngagementEffortRegulationCreateForm(forms.ModelForm): "sub_instruments", "notes", ] - help_texts = { - "is_planned": ("To distinguish planned events from retrospective recording.") - } + help_texts = {"is_planned": ("To distinguish planned events from retrospective recording.")} widgets = { "proposed_start_date": forms.DateTimeInput( attrs={ @@ -86,7 +90,6 @@ class EngagementEffortRegulationCreateForm(forms.ModelForm): class EngagementEffortPlanningCreateForm(forms.ModelForm): - class Meta: model = EngagementEffort fields = [ @@ -96,9 +99,7 @@ class EngagementEffortPlanningCreateForm(forms.ModelForm): "officers", "notes", ] - help_texts = { - "is_planned": ("To distinguish planned events from retrospective recording.") - } + help_texts = {"is_planned": ("To distinguish planned events from retrospective recording.")} widgets = { "proposed_start_date": forms.DateTimeInput( attrs={ @@ -118,28 +119,30 @@ class EngagementEffortPlanningCreateForm(forms.ModelForm): class EngagementEffortTravelCreateForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.fields['is_planned'].widget.attrs.update({"class": "select-lg"}) + 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' + 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"}), + "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.") - } + 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() + 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() @@ -162,20 +165,32 @@ class EngagementEffortCreateForm(forms.ModelForm): "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" + "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" + "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'}), + "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" + } + ), } @@ -187,6 +202,16 @@ class EngagementCreateForm(forms.ModelForm): 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_start_date + class Meta: model = Engagement fields = [ @@ -209,3 +234,9 @@ class EngagementCreateForm(forms.ModelForm): "engagement_type": forms.Select(), "officers": forms.SelectMultiple(), } + + +class EngagementStrategyCreateForm(forms.ModelForm): + class Meta: + fields = "__all__" + model = EngagementStrategy |