-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatching.py
More file actions
77 lines (61 loc) · 2.86 KB
/
matching.py
File metadata and controls
77 lines (61 loc) · 2.86 KB
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
from __future__ import annotations
from django.contrib.messages.views import SuccessMessageMixin
from django.db import models, transaction
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.translation import gettext as _
from django.views.generic import CreateView
from apps.fiestaforms.views.htmx import HtmxFormViewMixin
from apps.fiestarequests.models import BaseRequestSystemConfiguration
from apps.fiestarequests.models.request import BaseRequestMatchProtocol, BaseRequestProtocol
from apps.plugins.views import PluginConfigurationViewMixin
from apps.sections.views.mixins.membership import EnsureLocalUserViewMixin
from apps.sections.views.mixins.section_space import EnsureInSectionSpaceViewMixin
from apps.utils.views import AjaxViewMixin
class BaseTakeRequestView(
PluginConfigurationViewMixin[BaseRequestSystemConfiguration],
EnsureInSectionSpaceViewMixin,
EnsureLocalUserViewMixin,
AjaxViewMixin,
HtmxFormViewMixin,
SuccessMessageMixin,
CreateView,
):
template_name = "fiestaforms/pages/card_page_for_ajax_form.html"
ajax_template_name = "fiestaforms/parts/ajax-form-container.html"
success_message = _("Request successfully matched!")
match_model: type[BaseRequestMatchProtocol] | type[models.Model]
form_url: str
fiesta_request: BaseRequestProtocol
def get_form(self, form_class=None):
form = super().get_form(form_class)
if self.configuration and self.configuration.enable_note_from_matcher:
form.fields["note"].required = True
else:
form.fields["note"].disabled = True
form.fields["note"].widget = form.fields["note"].hidden_widget()
return form
def dispatch(self, request, *args, **kwargs):
self.fiesta_request = get_object_or_404(
self.get_queryset(),
pk=kwargs.get("pk"),
)
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
data["form_url"] = reverse(self.form_url, kwargs={"pk": self.fiesta_request.pk})
return data
def after_match_created(self, match, fiesta_request) -> None:
"""Hook for subclasses to trigger notifications after a match is created."""
@transaction.atomic
def form_valid(self, form):
match: BaseRequestMatchProtocol = form.instance
match.request = self.fiesta_request
match.matcher = self.request.user
match.matcher_faculty = self.request.user.profile.faculty
response = super().form_valid(form)
self.fiesta_request.match = match
self.fiesta_request.state = BaseRequestProtocol.State.MATCHED
self.fiesta_request.save(update_fields=["state"])
transaction.on_commit(lambda: self.after_match_created(match, self.fiesta_request))
return response