-
Notifications
You must be signed in to change notification settings - Fork 1
/
matching.py
123 lines (101 loc) · 4.13 KB
/
matching.py
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
from __future__ import annotations
import uuid
from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db import transaction
from django.utils.translation import gettext as _
from django.views.generic import ListView
from django.views.generic.detail import BaseDetailView
from django_htmx.http import HttpResponseClientRedirect
from apps.buddy_system.models import BuddyRequest, BuddyRequestMatch, BuddySystemConfiguration
from apps.files.views import NamespacedFilesServeView
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.models.query import Q
class MatchingRequestsView(
EnsureInSectionSpaceViewMixin,
EnsureLocalUserViewMixin,
PermissionRequiredMixin,
PluginConfigurationViewMixin[BuddySystemConfiguration],
ListView,
):
template_name = "buddy_system/matching_requests.html"
model = BuddyRequest
def has_permission(self):
return self.configuration.matching_policy_instance.can_member_match
def get_queryset(self):
return self.configuration.matching_policy_instance.limit_requests(
qs=BuddyRequest.objects.get_queryset(),
membership=self.request.membership,
)
class TakeBuddyRequestView(
EnsureInSectionSpaceViewMixin,
EnsureLocalUserViewMixin,
PermissionRequiredMixin,
PluginConfigurationViewMixin[BuddySystemConfiguration],
BaseDetailView,
):
def has_permission(self):
return self.configuration.matching_policy_instance.can_member_match
def get_queryset(self):
return self.configuration.matching_policy_instance.limit_requests(
qs=BuddyRequest.objects.get_queryset(),
membership=self.request.membership,
)
@transaction.atomic
def post(self, request, pk: uuid.UUID):
br: BuddyRequest = self.get_object()
match = BuddyRequestMatch(
request=br,
matcher=self.request.user,
note=self.request.POST.get("note") or "",
)
# TODO: check matcher relation to responsible section
# TODO: reset any previous match for this BR
match.save()
br.match = match
br.state = BuddyRequest.State.MATCHED
br.save(update_fields=["state"])
messages.success(request, _("Request successfully matched!"))
# TODO: target URL?
return HttpResponseClientRedirect("/")
class IssuerPictureServeView(
PluginConfigurationViewMixin[BuddySystemConfiguration],
NamespacedFilesServeView,
):
def has_permission(self, request: HttpRequest, name: str) -> bool:
# is the file in requests, for whose is the related section responsible?
related_requests = request.membership.section.buddy_system_requests.filter(
issuer__profile__picture=name,
)
# does have the section enabled picture displaying?
return (related_requests.exists() and self.configuration and self.configuration.display_issuer_picture) or (
related_requests.filter(
state=BuddyRequest.State.MATCHED,
)
.filter(
Q(match__matcher=request.user) | Q(issuer=request.user),
)
.exists()
)
class MatcherPictureServeView(
PluginConfigurationViewMixin[BuddySystemConfiguration],
NamespacedFilesServeView,
):
def has_permission(self, request: HttpRequest, name: str) -> bool:
# is the file in requests, for whose is the related section responsible?
related_requests = request.membership.section.buddy_system_requests.filter(
match__matcher__profile__picture=name,
)
# does have the section enabled picture displaying?
return (
related_requests.filter(
state=BuddyRequest.State.MATCHED,
)
.filter(
Q(match__matcher=request.user) | Q(issuer=request.user),
)
.exists()
)