-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatching.py
More file actions
126 lines (100 loc) · 4.47 KB
/
matching.py
File metadata and controls
126 lines (100 loc) · 4.47 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
119
120
121
122
123
124
125
126
from __future__ import annotations
import logging
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.template.loader import render_to_string
from django.urls import reverse_lazy
from django.utils.functional import lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import ListView
from apps.buddy_system.forms import BuddyRequestMatchForm
from apps.buddy_system.models import BuddyRequest, BuddyRequestMatch, BuddySystemConfiguration
from apps.fiestarequests.matching_policy import BaseMatchingPolicy
from apps.fiestarequests.views.matching import BaseTakeRequestView
from apps.pickup_system.models.files import BaseIssuerPictureServeView, BaseMatcherPictureServeView
from apps.plugins.middleware.plugin import HttpRequest
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.breadcrumbs import with_breadcrumb, with_plugin_home_breadcrumb
logger = logging.getLogger(__name__)
@with_plugin_home_breadcrumb
@with_breadcrumb(_("Waiting Requests"))
class MatchingRequestsView(
EnsureInSectionSpaceViewMixin,
EnsureLocalUserViewMixin,
PermissionRequiredMixin,
PluginConfigurationViewMixin[BuddySystemConfiguration],
ListView,
):
template_name = "buddy_system/matching_requests.html"
model = BuddyRequest
_policy = None
def has_permission(self):
self._policy: BaseMatchingPolicy = self.configuration.matching_policy_instance
return self._policy.matching_done_by_members and self._policy.can_member_match(
membership=self.request.membership
)
def get_permission_denied_message(self):
# can be called even in the context of unauthorized user (so no policy available)
if self._policy and not self._policy.can_member_match(membership=self.request.membership):
return _("You have reached the limit of request matches in time window.")
return super().get_permission_denied_message()
def get_queryset(self):
return self.configuration.matching_policy_instance.limit_requests(
qs=BuddyRequest.objects.get_queryset().select_related(
# select all potentially necessary fields in the template afterward
"issuer__profile__user",
"issuer__profile__university",
"issuer__profile__faculty",
),
membership=self.request.membership,
)
class MatchBuddyRequestFormView(
PermissionRequiredMixin,
BaseTakeRequestView,
):
match_model = BuddyRequestMatch
form_class = BuddyRequestMatchForm
form_url = "buddy_system:match-buddy-request"
success_url = reverse_lazy("buddy_system:my-buddies")
buddy_request: BuddyRequest
_policy = None
def has_permission(self):
self._policy: BaseMatchingPolicy = self.configuration.matching_policy_instance
return self._policy.matching_done_by_members and self._policy.can_member_match(
membership=self.request.membership
)
def get_queryset(self):
return self.configuration.matching_policy_instance.limit_requests(
qs=BuddyRequest.objects.get_queryset(),
membership=self.request.membership,
)
def get_form(self, form_class=None):
form = super().get_form(form_class)
form.fields["note"].help_text = lazy(
lambda: render_to_string(
"buddy_system/parts/buddy_request_match_note_help.html",
context={"request": self.get_object()},
),
str,
)
return form
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 ServeFilesFromBuddiesMixin:
@classmethod
def get_request_queryset(cls, request: HttpRequest):
return request.membership.section.buddy_system_requests
class IssuerPictureServeView(ServeFilesFromBuddiesMixin, BaseIssuerPictureServeView): ...
class MatcherPictureServeView(
ServeFilesFromBuddiesMixin,
BaseMatcherPictureServeView,
): ...