aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/organisations/forms.py
blob: d98e9f01e413b1cc49b9a839ce595115f3974f50 (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
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit
from django import forms
from django.forms import inlineformset_factory

from ctrack.organisations.models import Address, IncidentReport, Organisation


class OrganisationCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["name"].widget.attrs["class"] = "form-control form-control-lg"
        self.fields["submode"].widget.attrs["class"] = "form-control"
        self.fields["oes"].widget.attrs["class"] = "form-check-input"
        self.fields["oes"].widget.attrs["type"] = "checkbox"
        self.fields["designation_type"].widget.attrs["class"] = "form-control"
        self.fields["registered_company_name"].widget.attrs["class"] = "form-control"
        self.fields["registered_company_number"].widget.attrs["class"] = "form-control"
        self.fields["comments"].widget.attrs["class"] = "form-control"

    class Meta:
        model = Organisation
        fields = [
            "name",
            "submode",
            "oes",
            "designation_type",
            "registered_company_name",
            "registered_company_number",
            "comments",
            "active",
        ]
        labels = {"oes": "OES"}
        help_texts = {
            "name": "Name of the organisation",
            "submode": "e.g. Rail Maintenance, TOC, etc...",
            "active": "Is this company an active participant in the NIS compliance regime?",
            "designation_type": "This is probably defined in the Regulation",
        }


class AddressCreateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["type"].widget.attrs["class"] = "form-control"
        self.fields["line1"].widget.attrs["class"] = "form-control"
        self.fields["line2"].widget.attrs["class"] = "form-control"
        self.fields["line3"].widget.attrs["class"] = "form-control"
        self.fields["city"].widget.attrs["class"] = "form-control"
        self.fields["county"].widget.attrs["class"] = "form-control"
        self.fields["postcode"].widget.attrs["class"] = "form-control"
        self.fields["country"].widget.attrs["class"] = "form-control"
        self.fields["other_details"].widget.attrs["class"] = "form-control"

    class Meta:
        model = Address
        fields = (
            "type",
            "line1",
            "line2",
            "line3",
            "city",
            "county",
            "postcode",
            "country",
            "other_details",
        )


# https://dev.to/zxenia/django-inline-formsets-with-class-based-views-and-crispy-forms-14o6
# good advice on setting up the inlineformset - with crispy forms too
AddressInlineFormSet = inlineformset_factory(
    Organisation,
    Address,
    fields=(
        "type",
        "line1",
        "line2",
        "line3",
        "city",
        "county",
        "postcode",
        "country",
        "other_details",
    ),
    form=AddressCreateForm,
    extra=2,
)


class IncidentReportForm(forms.Form):
    class Meta:
        model = IncidentReport