-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdocument_admin.py
More file actions
262 lines (222 loc) · 7.91 KB
/
document_admin.py
File metadata and controls
262 lines (222 loc) · 7.91 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from __future__ import absolute_import, unicode_literals
from django.contrib import admin
from django.db.models import Case, Exists, IntegerField, OuterRef, Value, When
from django.utils.translation import ugettext_lazy as _
from ..list_filter import (
ApprovedImageListFilter,
AssignedListFilter,
DocumentDisciplineListFilter,
DocumentTrainingSetListFilter,
)
from ..models import DocumentImage, Static
from .alternative_word_admin import AlternativeWordAdmin
from .document_image_admin import DocumentImageAdmin
SUPERUSER_ONLY_LIST_FILTERS = [ApprovedImageListFilter]
class DocumentAdmin(admin.ModelAdmin):
"""
Admin Interface to for the Document module.
Inheriting from `admin.ModelAdmin`.
"""
fields = (
"word_type",
"grammatical_gender",
"singular_article",
"word",
"plural_article",
"plural",
"audio",
"definition",
"additional_meaning_1",
"additional_meaning_2",
)
readonly_fields = ("created_by",)
search_fields = ["word", "alternatives__alt_word"]
inlines = [DocumentImageAdmin, AlternativeWordAdmin]
ordering = ["word", "creation_date"]
list_display = (
"word",
"word_type",
"singular_article_display",
"related_training_set",
"related_disciplines",
"has_audio",
"has_image",
"creator_group",
"creation_date",
)
list_filter = (
DocumentDisciplineListFilter,
DocumentTrainingSetListFilter,
AssignedListFilter,
ApprovedImageListFilter,
)
list_per_page = 25
def save_model(self, request, obj, form, change):
"""
Overwrite django built-in function to save
user group and admin status of model
:param request: current user request
:type request: django.http.request
:param obj: document object
:type obj: models.Document
:param form: employed model form
:type form: ModelForm
:param change: True if change on existing model
:type change: bool
:raises IndexError: Error when user is not superuser and doesn't belong to any group
"""
if not change:
if len(request.user.groups.all()) >= 1:
obj.created_by = request.user.groups.all()[0]
elif not request.user.is_superuser:
raise IndexError("No group assigned. Please add the user to a group")
obj.creator_is_admin = request.user.is_superuser
obj.save()
def get_action_choices(self, request, default_choices=""):
"""
Overwrite django built-in function to modify action choices. The first
option is dropped since it is a place holder.
:param request: current user request
:type request: django.http.request
:return: modified action choices
:rtype: dict
"""
choices = super().get_action_choices(request)
choices.pop(0)
return choices
def get_queryset(self, request):
"""
Overwrite django built-in function to modify queryset according to user.
Users that are not superusers only see documents of their groups.
:param request: current user request
:type request: django.http.request
:return: adjusted queryset
:rtype: QuerySet
"""
qs = (
super()
.get_queryset(request)
.annotate(
has_image=Exists(DocumentImage.objects.filter(document=OuterRef("pk"))),
has_confirmed_image=Exists(
DocumentImage.objects.filter(
document=OuterRef("pk"), confirmed=True
)
),
image_sort=Case(
When(has_confirmed_image=True, then=Value(2)),
When(has_image=True, then=Value(1)),
default=Value(0),
output_field=IntegerField(),
),
)
)
if request.user.is_superuser:
return qs.filter(creator_is_admin=True)
return qs.filter(created_by__in=request.user.groups.all())
def related_training_set(self, obj):
"""
Display related training sets in list display
:param obj: Document object
:type obj: models.Document
:return: comma seperated list of related training sets
:rtype: str
"""
return ", ".join([child.title for child in obj.training_sets.all()])
related_training_set.short_description = _("training set")
def related_disciplines(self, obj):
"""
Display related desciplines in list display
:param obj: Document object
:type obj: models.Document
:return: comma seperated list of related training sets
:rtype: str
"""
disciplines = []
for training_set in obj.training_sets.all():
disciplines += [
discipline.title for discipline in training_set.discipline.all()
]
return ", ".join(disciplines)
related_disciplines.short_description = _("disciplines")
def creator_group(self, obj):
"""
Include creator group of discipline in list display
:param obj: Document object
:type obj: models.Document
:return: Either static admin group or user group
:rtype: str
"""
if obj.creator_is_admin:
return Static.admin_group
if obj.created_by:
return obj.created_by
return None
creator_group.short_description = _("creator group")
creator_group.admin_order_field = "created_by"
def has_audio(self, obj):
"""
Include in list display whether a document has an audio file.
:param obj: Document object
:type obj: models.Document
:return: Either static admin group or user group
:rtype: str
"""
if obj.audio:
return True
return False
has_audio.boolean = True
has_audio.short_description = _("audio")
has_audio.admin_order_field = "audio"
def has_image(self, obj):
"""
Include in list display whether a document has an image file.
:param obj: Document object
:type obj: models.Document
:return: Whether the document has at least one confirmed image
:rtype: bool
"""
if obj.has_confirmed_image:
return True
if obj.has_image:
return None
return False
has_image.boolean = True
has_image.short_description = _("image")
has_image.admin_order_field = "image_sort"
def singular_article_display(self, obj):
"""
Include article of document in list display
:param obj: Document object
:type obj: models.Document
:return: Either static admin group or user group
:rtype: str
"""
return obj.get_singular_article_display()
singular_article_display.short_description = _("singular article")
def get_list_filter(self, request):
"""
Override djangos get_list_filter function
to remove specific list filters that are only relevant
for super users.
:param request: current request
:type request: django.http.request
:param obj: [description], defaults to None
:type obj: django.db.models, optional
:return: custom fields list
:rtype: list[str]
"""
if request.user.is_superuser:
return self.list_filter
# remove filters that are only relevant for super users
filters = [f for f in self.list_filter if f not in SUPERUSER_ONLY_LIST_FILTERS]
return tuple(filters)
class Media:
"""
Media class of admin interface for documents
"""
css = {"all": ("css/document_form.css",)}
js = (
"js/image_preview.js",
"js/toggle_plural_field.js",
)