This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
553 lines (457 loc) · 22 KB
/
admin.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
from copy import deepcopy
from base64 import b64decode, b64encode
from django import forms
from django.conf import settings
from django.contrib import admin, messages
from django.contrib.admin.utils import unquote
from django.contrib.admin.widgets import AutocompleteSelect
from django.core.exceptions import PermissionDenied, ValidationError
from django.db import models, transaction
from django.db.models import Q
from django.forms.widgets import RadioSelect
from django.shortcuts import render
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from django.urls import path, reverse
from reversion.admin import VersionAdmin
from reversion.models import Version
from .models import (Team, Match, Player, Scoresheet,
Timer, TimerProfile, TimerStage, TIMERSTATES,)
class RadioRow(RadioSelect):
template_name = "fllfms/radiorow.html"
option_template_name = "fllfms/radiorow_option.html"
# There are some remanants from the RadioSelect context, we ignore them.
# self.attrs seems to be applied to both <ul> and <input> elements.
# This is also true for RadioSelect, and doesn't seem to break anything.
def get_context(self, name, value, attrs):
# Work with long choices (automatic class if not declared).
if not self.attrs.get('class', None):
# If any option label is more than 5 chars, use vertical.
# Make sure to cast to str before len()!
long = any(len(str(i['label'])) > 5
for i in self.options(None, []))
self.attrs['class'] = 'radiocolumn' if long else 'radiorow'
context = super().get_context(name, value, attrs)
context['widget']['options'] = self.options(
name, context['widget']['value'], attrs)
return context
class Media:
css = {'all': ("fllfms/radiorow.css",)}
class SignatureWidget(forms.Widget):
template_name = 'fllfms/signature_widget.html'
def format_value(self, value):
value = value or ""
if isinstance(value, str):
# Happens if we don't use to_python(), e.g. validation failure.
return value
return str(b64encode(value or b""), 'ascii')
class Media:
js = (
"fllfms/vendor/signature_pad/signature_pad.umd.min.js",
"fllfms/signature_widget.js",
)
class SignatureField(forms.Field):
initial = None
widget = SignatureWidget
def to_python(self, value):
if not value:
return None
try:
return b64decode(value)
except (TypeError, ValueError):
raise ValidationError(_("Invalid data format."),
code='invalid')
def clean(self, value):
value = self.to_python(value)
if value is None:
raise ValidationError(_("A signature is required."),
code='required')
return value
class PlayerAdmin(admin.TabularInline):
model = Player
fields = ('match', 'station', 'team', 'surrogate', 'scoresheet_link',)
readonly_fields = ('scoresheet_link',)
# We want to disallow editing the team once set, else scores would move to
# the new team, so require a new Player instead. Blocked by Django #15602.
# In the meantime: clean() checks on model.
# readonly_fields = tuple()
ordering = ('station',)
def scoresheet_link(self, obj):
url_name_data = (self.admin_site.name, Scoresheet._meta.app_label,
Scoresheet._meta.model_name)
if getattr(obj, 'scoresheet', None) is not None:
text = _(
"Edit scoresheet... (Score: {})").format(obj.scoresheet.score)
url = reverse("{}:{}_{}_change".format(*url_name_data),
args=[obj.scoresheet.pk])
elif obj.station is not None and obj.match.actual is not None:
# Only allow adding for existing players on a completed match.
text = _("Click to add...")
# Kinda hacky to prepopulate via querystring, but it works, so...
url = (reverse("{}:{}_{}_add".format(*url_name_data)) +
"?player={}".format(obj.pk))
else:
return "-" # No link for not-yet-created players.
return format_html('<a href="{}">{}</a>', url, text)
scoresheet_link.short_description = _("scoresheet")
# autocomplete_fields = ('team',) # Overriden below.
# Standard autocomplete field won't allow us to clear it once selected. We
# fix that by subclassing the AutocompleteSelect widget, and using that.
def formfield_for_foreignkey(self, db_field, request, **kwargs):
class ClearableAutocompleteSelect(AutocompleteSelect):
def build_attrs(self, base_attrs, extra_attrs=None):
attrs = super().build_attrs(base_attrs, extra_attrs)
attrs.update({'data-allow-clear': 'true'})
return attrs
db = kwargs.get('using')
if db_field.name == 'team':
kwargs['widget'] = ClearableAutocompleteSelect(
db_field.remote_field, self.admin_site, using=db)
return super().formfield_for_foreignkey(db_field, request, **kwargs)
# This is all blocked by Django bug #15602 (wrong obj value).
# We will also need to add Player.Meta.default_permissions = tuple().
# def has_view_permission(self, request, obj=None):
# if not obj:
# obj = Player(match=Match())
# return MatchAdmin(
# Match, self.admin_site).has_view_permission(request, obj.match)
#
# def has_write_permission(self, request, obj=None):
# if not obj:
# obj = Player(match=Match())
# return MatchAdmin(
# Match, self.admin_site).has_change_permission(request, obj.match)
#
# has_create_permission = has_write_permission
# has_change_permission = has_write_permission
# has_delete_permission = has_write_permission
@admin.register(Team)
class TeamAdmin(VersionAdmin, admin.ModelAdmin):
class EligibilityFilter(admin.SimpleListFilter):
title = _("eligibility")
parameter_name = 'dq'
def lookups(self, request, model_admin):
return (
# Values are negated to filter on dq, eligible=yes -> dq=False.
(0, _("Eligible")),
(1, _("Disqualified")),
)
def queryset(self, request, queryset):
# self.value() is taken from self.lookups() (and is a string).
if self.value() in ('0', '1'):
return queryset.filter(dq__exact=self.value())
# Eligibility column so ticks represent eligible teams (not disqualified).
def eligible(self, team):
return not team.dq
eligible.short_description = _("eligible")
eligible.boolean = True
eligible.admin_order_field = 'dq'
list_display = ('number', 'name', 'eligible')
list_display_links = list_display[:-1] # Cut the dq column.
list_filter = (EligibilityFilter,)
fields = list_display[:-1] + ('dq', )
readonly_fields = tuple()
ordering = ('number',)
search_fields = ('number', 'name',)
def get_readonly_fields(self, request, obj=None):
if obj:
# Disallow editing the team number once set.
return self.readonly_fields + ('number',)
return self.readonly_fields
@admin.register(Match)
class MatchAdmin(VersionAdmin, admin.ModelAdmin):
class MatchCompleteFilter(admin.SimpleListFilter):
title = _("completion state")
parameter_name = 'complete'
def lookups(self, request, model_admin):
return (
(0, _("Played")),
(1, _("Yet To Play")),
)
def queryset(self, request, queryset):
if self.value() in ('0', '1'):
# A match is complete if (and only if):
# - Scores are present (when implemented - TODO), or
# - Actual start time is NOT null
# (Str always casts True, so cast self.value() to int first.)
action = queryset.filter # For '0', filter on played matches.
if self.value() == '1':
action = queryset.exclude # Exclude played (Yet To Play).
# Distinct to prevent duplicates.
return action(Q(actual__isnull=False)
| Q(players__scoresheet__isnull=False)
).distinct()
class StationCountFilter(admin.SimpleListFilter):
title = _("missing teams")
parameter_name = 'stationcount'
def lookups(self, request, model_admin):
# See Django #1873 for feature request to filter multiple options.
# (Method listed doesn't seem to work with SimpleListFilter.)
return (
(0, _("Missing All Teams")),
(1, _("Missing Some Teams")),
(2, _("All Teams Present")),
)
def queryset(self, request, queryset):
queryset = queryset.annotate(models.Count('players'))
# self.value() is taken from self.lookups() (and is a string)
if self.value() == '0':
return queryset.filter(players__count=0)
if self.value() == '1':
return queryset.filter(
players__count__gt=0,
players__count__lt=len(settings.FLLFMS['TOURNAMENTS']))
if self.value() == '2':
return queryset.filter(
players__count=len(settings.FLLFMS['TOURNAMENTS']))
@staticmethod
def action_reset(self, request, queryset):
# NOTE: 'self' is the modeladmin object, which is provided.
# The staticmethod decorator ensures that it's not provided twice.
# Reset the match to an unplayed state.
# Completed matches only (actual is not None or scoresheets exist).
# Note that distinct() is needed else the second Q causes duplicates.
queryset = queryset.prefetch_related('players__scoresheet').filter(
Q(actual__isnull=False) | Q(players__scoresheet__isnull=False)
).distinct()
scoring = self.admin_site._registry.get(Scoresheet)
for match in queryset:
# We seek permission to clear actual and delete scoresheets,
# but not modify the player, since we don't do that.
if not self.has_change_permission(request, match):
break
for player in match.players.all():
if (getattr(player, 'scoresheet', None) is not None
and not scoring.has_delete_permission(
request, player.scoresheet)):
break
else:
# Valid, the loop did not terminate.
with transaction.atomic():
count = queryset.count() # Get count before updating.
Scoresheet.objects.filter(player__match__in=queryset).delete()
for match in queryset:
if match.actual is not None:
match.actual = None
match.save() # Trigger a reversion change.
self.message_user(
request, _(
"{} matches were reset. Any other selected matches were "
"already in an unplayed state.").format(count),
level=messages.SUCCESS)
return # Skip error below.
self.message_user(
request, _("You do not have sufficient privileges to perform the "
"requested action."),
level=messages.ERROR)
@staticmethod
def action_empty(self, request, queryset):
# NOTE: 'self' is the modeladmin object, which is provided.
# The staticmethod decorator ensures that it's not provided twice.
# Remove players from stations (only possible if match is unplayed).
if queryset.filter(
Q(actual__isnull=False) | Q(players__scoresheet__isnull=False)
).exists():
self.message_user(
request, _("Selection includes matches that have already been "
"played (cannot clear players). Reset those "
"matches first."),
level=messages.ERROR)
return
playeradmin = PlayerAdmin(parent_model=self.model,
admin_site=self.admin_site)
playerset = Player.objects.filter(match__in=queryset)
valid = all((
playeradmin.has_delete_permission(request, obj)
for obj in playerset
))
if valid:
num = playerset.delete()[0]
for match in queryset:
match.save() # Trigger reversion change.
# TODO the ngettext translation thing.
self.message_user(
request, _("{} players were deleted.").format(num),
level=messages.SUCCESS)
else:
self.message_user(
request, _("You do not have sufficient privileges to perform "
"the requested action."),
level=messages.ERROR)
list_display = (
'tournament', 'number', 'round', 'field', 'schedule', 'actual',)
list_display_links = list_display[:2]
list_editable = list_display[2:]
list_filter = ('tournament', 'round', 'field',
MatchCompleteFilter, StationCountFilter,)
ordering = ('-tournament', 'number',)
fields = list_display
search_fields = ('number',)
def get_actions(self, request):
actions = super().get_actions(request)
# TODO check scoresheet deletion permissions.
if self.has_change_permission(request) and True:
actions['reset'] = (self.action_reset, 'reset', _(
"Reset selected matches to unplayed state (time/scores)"))
if PlayerAdmin(parent_model=self.model, admin_site=self.admin_site
).has_delete_permission(request):
actions['empty'] = (self.action_empty, 'empty', _(
"Delete all players from selected matches"))
return actions
inlines = [
PlayerAdmin,
]
@admin.register(Scoresheet)
class ScoresheetAdmin(VersionAdmin, admin.ModelAdmin):
# Deepcopy but cast list to allow modifiction without altering original.
_mission_fieldsets = list(deepcopy(Scoresheet.missions))
for missionset in _mission_fieldsets:
# This modifies by reference, taking only first entry from each tuple.
# Each 'field' is a tuple (name, {**config}), we want a list of names.
missionset[1]['fields'] = [
mission[0] for mission in missionset[1]['fields']]
_mission_fields = sum(
(section[1]['fields'] for section in _mission_fieldsets), [])
def imgsignature(self, obj):
return format_html('<img src="data:image/png;base64,{}">',
str(b64encode(obj.signature), 'ascii'))
imgsignature.short_description = _("team initials")
def get_fieldsets(self, request, obj=None):
# Replace signature with imgsignature if it's going to be readonly.
# Kinda cheaty, but admin forms can only output text/booleans.
# We'd have to subclass at least 3 classes to do it "properly".
# I copied the "readonly form" check from ModelAdmin._changeform_view.
signature = 'signature'
if obj is not None and not self.has_change_permission(request, obj):
signature = 'imgsignature'
return (
(_("Player details"), {
'fields': ['player', 'referee']
}),
*self._mission_fieldsets,
(_("Sign off"), {
'fields': [signature]
}),
)
def formfield_for_dbfield(self, db_field, request, **kwargs):
db = kwargs.get('using')
if db_field.name == 'player':
if 'queryset' not in kwargs:
# Specify limiting and especially ordering here, not model.
mgr = db_field.remote_field.model._default_manager.using(db)
filter = Q(scoresheet__isnull=True,
match__actual__isnull=False)
# If we're editing existing, show current selection too.
obj = request.resolver_match.kwargs.get('object_id')
if obj is None:
# django-reversion uses args
# The last item will always be the Version pk.
# Slice, then add [None] in case empty, then get element.
obj = (request.resolver_match.args[-1:] + (None,))[0]
if obj is not None:
obj = Version.objects.get(pk=obj) # Get prev version.
# When editing a revision, or recovering a deleted
# object, a fake version is created to restore from. If
# editing, this fake version overwrites the current
# value, "freeing" that Player and allowing it to be
# selected.
# In both cases, the fake object uses the old Player in
# the fake object, blocking it from appearing as
# selectable (indeed, if the old Player is in use
# elsewhere, the view fails since the fake cannot be
# created). If the Player is unchanged between old and
# current, the view succeeds, but still blocks the
# value from selection.
# We must whitelist the old Player for selection, since
# this is a history view, so we want it visible, even
# if it could not be selected (which won't happen since
# the fake object creation would fail instead).
# Without the fake, this gets the old value.
# prev = obj.field_dict.get(db_field.column)
# if prev is not None:
# filter |= Q(pk=prev)
# Without the fake, this gets the current value.
# For now, it's actually the old value from the fake.
obj = obj.field_dict.get(Scoresheet._meta.pk.column)
if obj is not None:
filter |= Q(scoresheet__pk=obj)
kwargs['queryset'] = mgr.filter(filter).order_by(
'-match__actual', 'match', 'station')
# Skip the RelatedFieldWidgetWrapper.
return db_field.formfield(**kwargs)
if db_field.name == 'referee':
# Skip the RelatedFieldWidgetWrapper.
return db_field.formfield(**kwargs)
if db_field.name == 'signature':
return db_field.formfield(form_class=SignatureField, **kwargs)
if db_field.name in self._mission_fields:
if 'widget' not in kwargs:
kwargs['widget'] = RadioRow()
if 'choices' not in kwargs:
kwargs['choices'] = db_field.get_choices(include_blank=False)
return db_field.formfield(**kwargs)
# Later: filter on referee role for referee field.
return super().formfield_for_dbfield(db_field, request, **kwargs)
class TimerStageAdmin(admin.TabularInline):
model = TimerStage
fields = ('name', 'trigger', 'css', 'display', 'sound',)
ordering = ('trigger', 'name', 'pk',)
@admin.register(Timer)
class TimerAdmin(VersionAdmin, admin.ModelAdmin):
list_display = ('id', 'name', 'match', 'statestring', 'profile',)
list_display_links = list_display[:2]
fields = ('id', 'name', 'profile', 'statestring', 'match',)
autocomplete_fields = ('match',)
ordering = ('name', 'pk',)
def statestring(self, timer):
states = {
TIMERSTATES.PRESTART: _('Pre-Start'),
TIMERSTATES.START: _('Running'),
TIMERSTATES.END: _('Stopped'),
TIMERSTATES.ABORT: _('Failed'),
}
return states.get(timer.state)
statestring.short_description = _("timer state")
def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
return [
path('<path:object_id>/control/',
self.admin_site.admin_view(self.control_view),
name="{}_{}_control".format(*info)),
*super().get_urls(),
]
def get_readonly_fields(self, request, obj=None):
fields = ['id', 'statestring', ]
if obj is not None and obj.state == TIMERSTATES.START:
fields.extend(['profile', 'match', ])
return fields
def control_view(self, request, object_id):
# Check if exists (404) first, then permissions (403).
obj = self.get_object(request, unquote(object_id))
if obj is None:
# Later: when moving this function to views.py, add 404/403 args.
return self._get_obj_does_not_exist_redirect(
request, self.model._meta, object_id)
# NOTE: Keep synchronised with permissions checks in consumers.py.
profile_admin = self.admin_site._registry.get(TimerProfile)
if not (self.has_change_permission(request, obj)
and profile_admin.has_view_permission(request, obj.profile)):
raise PermissionDenied
return render(request, 'fllfms/timer_control.html', context={
'object_id': object_id, # Already quoted as passed argument.
})
@admin.register(TimerProfile)
class TimerProfileAdmin(VersionAdmin, admin.ModelAdmin):
list_display = ('name', 'duration', 'format',)
# fields = list_display
ordering = ('name', 'duration', 'pk',)
fieldsets = (
(_("General"), {'fields': ('name', 'duration', 'format',)}),
(_("Pre-Start"), {'fields': ('prestartcss',)}),
(_("Start"), {'fields': ('startcss', 'startdisplay', 'startsound',)}),
(_("End"), {'fields': ('endcss', 'endsound',)}),
(_("Abort"), {'fields': ('abortsound',)}),
)
inlines = [
TimerStageAdmin,
]