aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/register/forms.py
blob: b09804e51f5b58a985f94a86e230ded25790499a (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
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Button, ButtonHolder, Layout, Submit, Hidden, Field
from django import forms
from django.shortcuts import get_object_or_404
from django.urls import reverse

from ctrack.caf.models import CAF
from ctrack.organisations.models import Person, Organisation
from ctrack.register.models import (
    EngagementEvent,
    EngagementType,
    CAFSingleDateEvent,
    SingleDateTimeEvent,
)


class AddMeetingForm(forms.ModelForm):
    class Meta:
        model = SingleDateTimeEvent
        fields = [
            "type_descriptor",
            "short_description",
            "datetime",
            "comments",
            "location",
        ]

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop("user")
        super().__init__(*args, **kwargs)

    def save(self, **kwargs):
        form = super().save(commit=False)
        form.user = self.user
        form.save()
        return form


class CAFSingleDateEventForm(forms.ModelForm):
    class Meta:
        model = CAFSingleDateEvent
        fields = [
            "type_descriptor",
            "related_caf",
            "short_description",
            "date",
            "comments",
        ]

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop("user")
        super().__init__(*args, **kwargs)

    def save(self, **kwargs):
        form = super().save(commit=False)
        form.user = self.user
        form.save()
        return form


class EngagementEventCreateForm(forms.ModelForm):
    def __init__(self, user, caf=None, org_slug=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if caf:
            org = CAF.objects.get(pk=caf).organisation
            cancel_redirect = reverse("caf:detail", args=[caf])
            self.fields["related_caf"].initial = caf
            self.fields["participants"].queryset = Person.objects.filter(
                organisation__pk=org.pk
            )
            self.fields["type"].queryset = EngagementType.objects.all().order_by(
                "descriptor"
            )
            self.helper = FormHelper(self)
            self.helper.layout = Layout(
                Field("type"),
                "short_description",
                "participants",
                "related_caf",
                # "user",
                Hidden("user", "none"),
                "date",
                "end_date",
                "response_date_requested",
                "response_received",
                "document_link",
                "comments",
                ButtonHolder(
                    Submit("submit", "Submit", css_class="btn-primary"),
                    Button(
                        "cancel",
                        "Cancel",
                        onclick=f"location.href='{cancel_redirect}';",
                        css_class="btn-danger",
                    ),
                ),
            )
        else:
            org = get_object_or_404(Organisation, slug=org_slug)
            cancel_redirect = reverse("organisations:detail", args=[org_slug])
            selectable_people = Person.objects.filter(organisation__slug=org_slug)
            self.fields["participants"].queryset = selectable_people
            self.fields["participants"].initial = selectable_people.first()
            self.fields["type"].queryset = EngagementType.objects.all().order_by(
                "descriptor"
            )
            self.fields["related_caf"].queryset = org.caf_set.all()
            self.fields["related_caf"].label = "Related CAFs"
            self.helper = FormHelper(self)
            self.helper.layout = Layout(
                Field("type"),
                "short_description",
                "participants",
                "related_caf",
                # "user",
                Hidden("user", "none"),
                "date",
                "end_date",
                "response_date_requested",
                "response_received",
                "document_link",
                "comments",
                ButtonHolder(
                    Submit("submit", "Submit", css_class="btn-primary"),
                    Button(
                        "cancel",
                        "Cancel",
                        onclick=f"location.href='{cancel_redirect}';",
                        css_class="btn-danger",
                    ),
                ),
            )

    def save(self, commit=True):
        ee = super().save(commit=False)
        if commit:
            ee.save()
            self.save_m2m()  # so that we also save the peoples!
        return ee

    class Meta:
        model = EngagementEvent
        fields = "__all__"
        exclude = ["user"]
        widgets = {
            "date": forms.DateTimeInput(attrs={"type": "date"}),
            "response_date_requested": forms.DateTimeInput(attrs={"type": "date"}),
            "response_received": forms.DateTimeInput(attrs={"type": "date"}),
            "end_date": forms.DateTimeInput(attrs={"type": "date"}),
        }