from django import forms
from django.core.exceptions import ValidationError
from django.forms.widgets import HiddenInput
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"})
# 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