/
push_notification_list_view.py
149 lines (125 loc) · 5.27 KB
/
push_notification_list_view.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
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
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from django.conf import settings
from django.contrib import messages
from django.core.paginator import Paginator
from django.shortcuts import redirect, render
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView
from ...decorators import permission_required
from ...forms import ObjectSearchForm
from ...models import PushNotificationTranslation
if TYPE_CHECKING:
from typing import Any
from django.http import HttpRequest, HttpResponse
logger = logging.getLogger(__name__)
@method_decorator(permission_required("cms.view_pushnotification"), name="dispatch")
class PushNotificationListView(TemplateView):
"""
Class that handles HTTP GET requests for listing push notifications
"""
#: If true, shows the template push notification list
templates = False
#: The template to render if templates is False
template = "push_notifications/push_notification_list.html"
#: The template to render if templates is True
template_templates = "push_notifications/push_notification_template_list.html"
#: The context dict passed to the template (see :class:`~django.views.generic.base.ContextMixin`)
extra_context = {"current_menu_item": "push_notifications"}
@property
def template_name(self) -> str:
"""
Select correct HTML template, depending on :attr:`~integreat_cms.cms.views.push_notifications.push_notification_list_view.PushNotificationListView.templates` flag
(see :class:`~django.views.generic.base.TemplateResponseMixin`)
:return: Path to HTML template
"""
return self.template_templates if self.templates else self.template
# pylint: disable=too-many-locals
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
r"""
Create a list that shows existing push notifications and translations
:param request: Object representing the user call
:param \*args: The supplied arguments
:param \**kwargs: The supplied keyword arguments
:return: The rendered template response
"""
# current region
region = request.region
if not settings.FCM_ENABLED:
messages.error(
request,
_("Push notifications are disabled."),
)
return redirect("dashboard", **{"region_slug": region.slug})
if not region.push_notifications_enabled:
messages.error(
request,
_("Push notifications are disabled in this region."),
)
return redirect("dashboard", **{"region_slug": region.slug})
# current language
if language_slug := kwargs.get("language_slug"):
language = region.get_language_or_404(language_slug, only_active=True)
elif region.default_language:
return redirect(
"push_notifications",
**{
"region_slug": region.slug,
"language_slug": region.default_language.slug,
},
)
else:
messages.error(
request,
_(
"Please create at least one language node before creating push notifications."
),
)
return redirect(
"languagetreenodes",
**{
"region_slug": region.slug,
},
)
push_notifications = region.push_notifications.filter(
is_template=self.templates
)
query = None
search_data = kwargs.get("search_data")
search_form = ObjectSearchForm(search_data)
if search_form.is_valid():
query = search_form.cleaned_data["query"]
push_notification_keys = PushNotificationTranslation.search(
region, language_slug, query
).values("push_notification__pk")
push_notifications = push_notifications.filter(
pk__in=push_notification_keys
)
chunk_size = int(request.GET.get("size", settings.PER_PAGE))
# for consistent pagination querysets should be ordered
paginator = Paginator(push_notifications, chunk_size)
chunk = request.GET.get("page")
push_notifications_chunk = paginator.get_page(chunk)
return render(
request,
self.template_name,
{
**self.get_context_data(**kwargs),
"push_notifications": push_notifications_chunk,
"language": language,
"languages": region.active_languages,
"region_slug": region.slug,
"search_query": query,
},
)
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
r"""
Apply the query and filter the rendered push notifications
:param request: The current request
:param \*args: The supplied arguments
:param \**kwargs: The supplied keyword arguments
:return: The rendered template response
"""
return self.get(request, *args, **kwargs, search_data=request.POST)