/
language_tree_bulk_actions.py
151 lines (116 loc) · 4.39 KB
/
language_tree_bulk_actions.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
150
151
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from cacheops import invalidate_obj
from django.utils.translation import gettext_lazy as _
from ...models import (
EventTranslation,
LanguageTreeNode,
PageTranslation,
POITranslation,
)
from ..bulk_action_views import BulkUpdateBooleanFieldView
if TYPE_CHECKING:
from typing import Any
from django.http import HttpRequest, HttpResponseRedirect
logger = logging.getLogger(__name__)
class LanguageTreeBulkActionView(BulkUpdateBooleanFieldView):
"""
Bulk action view for language tree nodes which flushes
the cache for all content models after each operation
"""
#: The model of this :class:`~integreat_cms.cms.views.bulk_action_views.BulkActionView`
model = LanguageTreeNode
@property
def field_name(self) -> str:
"""
Called when the bulk action is performed and the ``field_name`` attribute was not overwritten
:raises NotImplementedError: If the ``field_name`` attribute is not implemented in the subclass
"""
raise NotImplementedError(
"Subclasses of LanguageTreeBulkActionView must provide a 'field_name' attribute"
)
@property
def action(self) -> str:
"""
Called when the bulk action is performed and the ``action`` attribute was not overwritten
:raises NotImplementedError: If the ``action`` attribute is not implemented in the subclass
"""
raise NotImplementedError(
"Subclasses of LanguageTreeBulkActionView must provide an 'action' attribute"
)
def post(
self, request: HttpRequest, *args: Any, **kwargs: Any
) -> HttpResponseRedirect:
r"""
Execute bulk action for language tree node and flush the cache
:param request: The current request
:param \*args: The supplied arguments
:param \**kwargs: The supplied keyword arguments
:return: The redirect
"""
# Execute bulk action
response = super().post(request, *args, **kwargs)
# Flush cache of content objects
for page in self.request.region.pages.all():
invalidate_obj(page)
for poi in self.request.region.pois.all():
invalidate_obj(poi)
for event in self.request.region.events.all():
invalidate_obj(event)
# Let the base view handle the redirect
return response
# pylint: disable=too-many-ancestors
class BulkMakeVisibleView(LanguageTreeBulkActionView):
"""
Bulk action for making multiple language tree nodes visible at once
"""
#: The name of the archived-field
field_name = "visible"
#: The name of the action
action = _("made visible")
# pylint: disable=too-many-ancestors
class BulkHideView(BulkMakeVisibleView):
"""
Bulk action for hiding multiple language tree nodes at once
"""
#: The value of the field
value = False
#: The name of the action
action = _("hidden")
# pylint: disable=too-many-ancestors
class BulkActivateView(LanguageTreeBulkActionView):
"""
Bulk action for activating multiple language tree nodes at once
"""
#: The name of the archived-field
field_name = "active"
#: The name of the action
action = _("activated")
def post(
self, request: HttpRequest, *args: Any, **kwargs: Any
) -> HttpResponseRedirect:
for language_tree_node in self.get_queryset():
models = [PageTranslation, EventTranslation, POITranslation]
for model in models:
filters = {
f"{model.foreign_field()}__region": request.region,
"language": language_tree_node.language,
}
distinct = [f"{model.foreign_field()}__pk", "language__pk"]
for translation in model.objects.filter(**filters).distinct(*distinct):
if self.value:
translation.save(update_timestamp=False)
else:
translation.links.all().delete()
# Execute bulk action
return super().post(request, *args, **kwargs)
# pylint: disable=too-many-ancestors
class BulkDisableView(BulkActivateView):
"""
Bulk action for disabling multiple language tree nodes at once
"""
#: The value of the field
value = False
#: The name of the action
action = _("disabled")