-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.py
More file actions
118 lines (92 loc) · 4.04 KB
/
editor.py
File metadata and controls
118 lines (92 loc) · 4.04 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
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
from __future__ import annotations
import logging
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import UpdateView
from django_tables2 import TemplateColumn
from django_tables2.columns.base import LinkTransform
from django_tables2.utils import Accessor
from apps.buddy_system.forms import BuddyRequestEditorForm, QuickBuddyMatchForm
from apps.buddy_system.models import BuddyRequest, BuddyRequestMatch
from apps.fiestaforms.views.htmx import HtmxFormViewMixin
from apps.fiestarequests.tables.editor import BaseRequestsFilter, BaseRequestsTable
from apps.fiestarequests.views.editor import BaseQuickRequestMatchView, BaseUpdateRequestStateView
from apps.fiestatables.views.tables import FiestaTableView
from apps.sections.middleware.section_space import HttpRequest
from apps.sections.views.mixins.membership import EnsurePrivilegedUserViewMixin
from apps.utils.breadcrumbs import with_breadcrumb, with_object_breadcrumb, with_plugin_home_breadcrumb
from apps.utils.views import AjaxViewMixin
logger = logging.getLogger(__name__)
class BuddyRequestsTable(BaseRequestsTable):
match_request = TemplateColumn(
template_name="buddy_system/parts/requests_editor_match_btn.html",
exclude_from_export=True,
order_by="match",
)
class Meta(BaseRequestsTable.Meta):
model = BuddyRequest
fields = BaseRequestsTable.Meta.fields + ("match_request",)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if "issuer_name" in self.columns:
# sometimes excluded
self.columns["issuer_name"].link = LinkTransform(
attrs={"x-data": lambda: "modal($el.href)", "x-bind": "bind"},
reverse_args=("buddy_system:editor-detail", {"pk": Accessor("pk")}),
)
@with_plugin_home_breadcrumb
@with_breadcrumb(_("Requests"))
class BuddyRequestsEditorView(
EnsurePrivilegedUserViewMixin,
FiestaTableView,
):
request: HttpRequest
table_class = BuddyRequestsTable
filterset_class = BaseRequestsFilter
def get_queryset(self):
return self.request.in_space_of_section.buddy_system_requests.select_related(
"issuer__profile",
)
@with_plugin_home_breadcrumb
@with_object_breadcrumb()
class BuddyRequestEditorDetailView(
EnsurePrivilegedUserViewMixin,
SuccessMessageMixin,
HtmxFormViewMixin,
AjaxViewMixin,
UpdateView,
):
template_name = "fiestaforms/pages/card_page_for_ajax_form.html"
ajax_template_name = "fiestaforms/parts/ajax-form-container.html"
model = BuddyRequest
form_class = BuddyRequestEditorForm
success_url = reverse_lazy("buddy_system:requests")
success_message = _("Buddy request has been updated.")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form_url"] = reverse("pickup_system:editor-detail", kwargs={"pk": self.object.pk})
return context
class QuickBuddyMatchView(BaseQuickRequestMatchView):
model = BuddyRequest
form_class = QuickBuddyMatchForm
success_url = reverse_lazy("buddy_system:requests")
success_message = _("Buddy request has been matched.")
form_url = "buddy_system:quick-match"
match_model = BuddyRequestMatch
def after_match_created(self, match, fiesta_request) -> None:
from apps.notifications.services.match import notify_buddy_match
try:
notify_buddy_match(
match=match,
request=fiesta_request,
section=self.request.in_space_of_section,
)
except Exception:
logger.exception("Failed to send buddy match notification for match pk=%s", match.pk)
class UpdateBuddyRequestStateView(BaseUpdateRequestStateView):
model = BuddyRequest
object: BuddyRequest
success_url = reverse_lazy("buddy_system:requests")
def get_queryset(self):
return self.request.in_space_of_section.buddy_system_requests