-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadmin.py
More file actions
128 lines (108 loc) · 4.6 KB
/
Copy pathadmin.py
File metadata and controls
128 lines (108 loc) · 4.6 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
127
128
import importlib
import logging
from csp.constants import UNSAFE_INLINE as CSP_UNSAFE_INLINE
from csp.decorators import csp_update
from django.conf import settings
from django.contrib import admin, messages
from django.http import HttpResponseRedirect
from django.urls import path, reverse
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext_lazy
from django_ilmoitin.admin import NotificationTemplateAdmin
from django_ilmoitin.models import NotificationTemplate
from .notification_importer import (
AbstractNotificationImporter,
NotificationImporterException,
)
logger = logging.getLogger(__name__)
class NotificationTemplateAdminWithImporter(NotificationTemplateAdmin):
list_display = ("type", "get_sync_status")
actions = ("update_selected",)
ordering = ("type",)
change_list_template = "notification_change_list.html"
importer: AbstractNotificationImporter = None
def changelist_view(self, request, *args, **kwargs):
try:
self.importer = self.__load_importer_class_from_settings()
except NotificationImporterException as e:
self._send_error_message(request, e)
return super().changelist_view(request, *args, **kwargs)
def __load_importer_class_from_settings(self) -> AbstractNotificationImporter:
(
importer_module_name,
importer_class_name,
) = settings.NOTIFICATIONS_IMPORTER.rsplit(".", 1)
importer_module = importlib.import_module(importer_module_name)
importer_class = getattr(importer_module, importer_class_name)
return importer_class()
def get_sync_status(self, obj):
return self.importer.is_notification_in_sync(obj) if self.importer else None
get_sync_status.short_description = _("in sync with the importer source")
get_sync_status.boolean = True
def update_selected(self, request, queryset):
if not self.importer:
return
try:
num_of_updated = self.importer.update_notifications(queryset)
except NotificationImporterException as e:
self._send_error_message(request, e)
return
if num_of_updated:
message = ngettext_lazy(
f"Updated {num_of_updated} notification.",
f"Updated {num_of_updated} notifications.",
num_of_updated,
)
else:
message = _("The selected notifications were in sync already.")
self.message_user(request, message, messages.SUCCESS)
update_selected.short_description = _(
"Update selected notifications from the importer source"
)
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path(
"import-missing-notifications/",
self.admin_site.admin_view(self.import_missing_notifications),
name="import-missing-notifications",
),
]
return custom_urls + urls
def import_missing_notifications(self, request):
if self.importer:
try:
num_of_created = self.importer.create_missing_notifications()
except NotificationImporterException as e:
self._send_error_message(request, e)
else:
if num_of_created:
message = ngettext_lazy(
f"Imported {num_of_created} new notification.",
f"Imported {num_of_created} new notifications.",
num_of_created,
)
else:
message = _("No missing notifications.")
self.message_user(request, message, messages.SUCCESS)
return HttpResponseRedirect(
reverse("admin:django_ilmoitin_notificationtemplate_changelist")
)
def _send_error_message(self, request, message):
logger.error(message)
self.message_user(request, message, messages.ERROR)
# Acecpt CSP_UNSAFE_INLINE for the changeform view to allow
# the preview template to work correctly.
@csp_update(
{
"script-src": settings.CONTENT_SECURITY_POLICY["DIRECTIVES"]["script-src"]
+ [CSP_UNSAFE_INLINE],
}
)
def changeform_view(self, request, object_id=None, form_url="", extra_context=None):
return super().changeform_view(
request, object_id, form_url, extra_context=extra_context
)
if hasattr(settings, "NOTIFICATIONS_IMPORTER"):
admin.site.unregister(NotificationTemplate)
admin.site.register(NotificationTemplate, NotificationTemplateAdminWithImporter)