aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/register/views.py
blob: c8fe1c56326bc048556353a8dd45bcd0013d985e (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from django.contrib.auth import get_user_model
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.views.generic import DeleteView, FormView, UpdateView, CreateView

from ctrack.caf.models import CAF
from ctrack.organisations.models import Organisation
from ctrack.register.forms import (
    CreateSimpleDateTimeEventForm,
    EngagementEventCreateForm, CreateNoteEventForm, CAFSingleDateEventForm,
)
from ctrack.register.models import EngagementEvent, SingleDateTimeEvent, NoteEvent, CAFSingleDateEvent


class EngagementEventDelete(DeleteView):
    model = EngagementEvent

    def get_success_url(self):
        return reverse_lazy("organisations:detail", args=[self.kwargs["slug"]])


class EngagementEventCreate(FormView):
    fields = "__all__"
    form_class = EngagementEventCreateForm
    template_name = "register/engagementevent_form.html"

    # success_url = reverse_lazy("organisations:list")

    def form_valid(self, form):
        ee = form.save(commit=False)
        ee.user = self.request.user
        form.save()
        return HttpResponseRedirect(self.get_success_url())

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["org_slug"] = self.kwargs["slug"]
        kwargs["user"] = get_user_model()
        return kwargs

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context["org"] = Organisation.objects.get(slug=self.kwargs["slug"])
        return context

    def get_success_url(self):
        return reverse_lazy("organisations:detail", args=[self.kwargs["slug"]])


class EngagementEventCreateFromCaf(FormView):
    fields = "__all__"
    form_class = EngagementEventCreateForm
    template_name = "snippets/event_form_base.html"

    def form_valid(self, form):
        ee = form.save(commit=False)
        ee.user = self.request.user
        form.save()
        return HttpResponseRedirect(self.get_success_url())

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["org_slug"] = self.kwargs.get("slug")
        kwargs["user"] = get_user_model()
        kwargs["caf"] = self.kwargs.get("caf_id")
        return kwargs

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        return context

    def get_success_url(self):
        org_slug = CAF.objects.get(pk=self.kwargs["caf_id"]).organisation.slug
        return reverse_lazy("organisations:detail", args=[org_slug])


class CreateNoteEvent(CreateView):
    form_class = CreateNoteEventForm
    model = NoteEvent
    template_name = "register/create_note_event_form.html"

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["user"] = self.request.user
        return kwargs

    def form_valid(self, form):
        note = form.save(commit=False)
        note.user = self.request.user
        note.save()
        return super().form_valid(form)

    def get_success_url(self):
        return reverse_lazy("organisations:detail", args=[self.object.organisation.slug])


class SingleDateTimeEventUpdate(UpdateView):
    model = SingleDateTimeEvent
    fields = [
        "type_descriptor",
        "short_description",
        "datetime",
        "private",
        "document_link",
        "comments",
        "participants",
        "requested_response_date",
        "response_received_date",
    ]
    template_name_suffix = "_update_form"
    success_url = reverse_lazy("organisations:list")

    def get_success_url(self):
        # We might not have org_slug if we are not setting this from an org detail page
        try:
            self.success_url = reverse_lazy(
                "organisations:detail", args=[self.kwargs["org_slug"]]
            )
            return super().get_success_url()
        except KeyError:
            return super().get_success_url()


class SingleDateTimeEventCreate(FormView):
    template_name = "register/single_datetime_event_create.html"
    form_class = CreateSimpleDateTimeEventForm
    success_url = reverse_lazy("organisations:list")

    def get_success_url(self):
        # We might not have org_slug if we are not setting this from an org detail page
        try:
            self.success_url = reverse_lazy(
                "organisations:detail", args=[self.kwargs["org_slug"]]
            )
            return super().get_success_url()
        except KeyError:
            return super().get_success_url()

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        if self.kwargs.get("org_slug"):
            context["org"] = Organisation.objects.get(slug=self.kwargs["org_slug"])
            return context
        return context

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["user"] = self.request.user
        kwargs["org_slug"] = self.kwargs.get("org_slug")
        kwargs["event_type"] = self.kwargs.get("event_type")
        return kwargs

    def form_valid(self, form):
        form.save()
        return super().form_valid(form)


class CAFCreateSingleDateEventView(FormView):
    template_name = "register/caf_single_date_event_form.html"
    form_class = CAFSingleDateEventForm
    success_url = reverse_lazy("caf:detail")

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["user"] = self.request.user
        kwargs["caf_id"] = self.kwargs.get("caf_id")
        return kwargs

    def form_valid(self, form):
        form.save()
        return super().form_valid(form)

    def get_success_url(self):
        self.success_url = reverse_lazy("caf:detail", args=[self.kwargs.get("caf_id")])
        return super().get_success_url()

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context["caf"] = CAF.objects.get(id=self.kwargs.get("caf_id"))
        return context