summaryrefslogblamecommitdiffstats
path: root/engagements/forms.py
blob: ae01916d9781e722c3f544728c923e9f64a7fc1d (plain) (tree)

















































































































































































































































                                                                                                                    
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Field, Fieldset, Layout, Submit
from django import forms
from django.forms.widgets import HiddenInput

from .models import Engagement, EngagementEffort

# TODO - need to handle errors correctly in this form and in the template


class EngagementEffortReportingCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                "This is a form",
                Field("is_planned"),
                Field("proposed_start_date", css_class="w3-input w3-border w3-round", type="date"),
                Field("proposed_end_date", css_class="w3-input w3-border w3-round"),
                "officers",
                "notes",
            ),
            Submit("submit", "Submit", css_class="w3-button w3-green"),
        )

    class Meta:
        model = EngagementEffort
        fields = [
            "is_planned",
            "proposed_start_date",
            "proposed_end_date",
            "officers",
            "notes",
        ]
        help_texts = {
            "is_planned": ("<br><small><em>To distinguish planned events from retrospective recording</em></small>")
        }
        widgets = {
            "proposed_start_date": forms.DateTimeInput(
                attrs={
                    "class": "w3-input w3-border w3-round",
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateTimeInput(
                attrs={
                    "class": "w3-input w3-border w3-round",
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
        }


class EngagementEffortRegulationCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                "This is a form",
                Field("is_planned"),
                Field("proposed_start_date", css_class="w3-input w3-border w3-round", type="date"),
                Field("proposed_end_date", css_class="w3-input w3-border w3-round"),
                "sub_instruments",
                "officers",
            ),
            Submit("submit", "Submit", css_class="w3-button w3-green"),
        )

    class Meta:
        model = EngagementEffort
        fields = [
            "is_planned",
            "proposed_start_date",
            "proposed_end_date",
            "officers",
            "sub_instruments",
            "notes",
        ]
        help_texts = {
            "is_planned": ("<br><small><em>To distinguish planned events from retrospective recording</em></small>")
        }
        widgets = {
            "proposed_start_date": forms.DateTimeInput(
                attrs={
                    "class": "w3-input w3-border w3-round",
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateTimeInput(
                attrs={
                    "class": "w3-input w3-border w3-round",
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
        }


class EngagementEffortPlanningCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                "This is a form",
                Field("is_planned"),
                Field("proposed_start_date", css_class="w3-input w3-border w3-round", type="date"),
                Field("proposed_end_date", css_class="w3-input w3-border w3-round"),
                "officers",
                "notes",
            ),
            Submit("submit", "Submit", css_class="w3-button w3-green"),
        )

    class Meta:
        model = EngagementEffort
        fields = [
            "is_planned",
            "proposed_start_date",
            "proposed_end_date",
            "officers",
            "notes",
        ]
        help_texts = {
            "is_planned": ("<br><small><em>To distinguish planned events from retrospective recording</em></small>")
        }
        widgets = {
            "proposed_start_date": forms.DateTimeInput(
                attrs={
                    "class": "w3-input w3-border w3-round",
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateTimeInput(
                attrs={
                    "class": "w3-input w3-border w3-round",
                    "type": "datetime-local",
                },
                format="j M y H:i",
            ),
        }


class EngagementEffortTravelCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                "This is a form",
                Field("is_planned"),
                Field("proposed_start_date", css_class="w3-input w3-border w3-round", type="date"),
                Field("proposed_end_date", css_class="w3-input w3-border w3-round"),
                "officers",
            ),
            Submit("submit", "Submit", css_class="w3-button w3-green"),
        )

    class Meta:
        model = EngagementEffort
        fields = ["is_planned", "proposed_start_date", "proposed_end_date", "officers"]
        widgets = {
            "proposed_start_date": forms.DateTimeInput(attrs={"type": "datetime-local"}),
            "proposed_end_date": forms.DateTimeInput(attrs={"type": "datetime-local"}),
        }


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()

    class Meta:
        model = EngagementEffort
        fields = "__all__"
        widgets = {
            "is_planned": forms.Select(
                choices=(
                    (True, "YES"),
                    (False, "NO"),
                ),
                attrs={"class": "w3-border"},
            ),
            "proposed_start_date": forms.DateTimeInput(
                attrs={"class": "w3-input w3-border w3-round", "type": "date"},
                format="j M y H:i",
            ),
            "proposed_end_date": forms.DateTimeInput(
                attrs={"class": "w3-input w3-border w3-round", "type": "date"},
                format="j M y H:i",
            ),
            "effort_type": forms.Select(attrs={"class": "w3-select w3-border w3-round"}),
            "officers": forms.SelectMultiple(attrs={"class": "w3-select w3-border w3-round"}),
            "sub_instruments": forms.SelectMultiple(attrs={"class": "w3-select w3-border w3-round"}),
        }
        help_texts = {
            "proposed_start_date": "<small><em>YYYY-MM-DD HH:MM</em> "
            "(<strong>Please include time here</strong>)</small>",
            "proposed_end_date": "<small><em>YYYY-MM-DD HH:MM</em> "
            "(<strong>Please include time here</strong>)</small>",
        }


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

    class Meta:
        model = Engagement
        fields = [
            "proposed_start_date",
            "proposed_end_date",
            "engagement_type",
            "officers",
        ]
        labels = {
            "officers": "Inspectors",
        }
        help_texts = {
            "proposed_start_date": "<small><em>YYYY-MM-DD</em></small>",
            "proposed_end_date": "<small><em>YYYY-MM-DD</em></small>",
        }
        widgets = {
            "proposed_start_date": forms.DateInput(attrs={"class": "w3-input w3-border w3-round", "type": "date"}),
            "proposed_end_date": forms.DateInput(attrs={"class": "w3-input w3-border w3-round", "type": "date"}),
            "engagement_type": forms.Select(attrs={"class": "w3-select w3-input w3-border w3-round"}),
            "officers": forms.SelectMultiple(attrs={"class": "w3-input w3-border w3-round"}),
        }