med/sporz/admin.py

48 lines
1.4 KiB
Python
Raw Normal View History

2019-08-15 09:36:39 +00:00
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.contrib import admin
from med.admin import admin_site
from .models import GameSave, Player
class PlayerInline(admin.TabularInline):
model = Player
class GameSaveAdmin(admin.ModelAdmin):
inlines = [PlayerInline, ]
list_display = ('__str__', 'game_master', 'game_has_ended')
date_hierarchy = 'created_at'
def has_change_permission(self, request, obj=None):
"""
If user is game master then authorize edit
"""
if obj and obj.game_master == request.user:
return True
return super().has_change_permission(request, obj)
def has_delete_permission(self, request, obj=None):
"""
If user is game master then authorize deletion
"""
if obj and obj.game_master == request.user:
return True
return super().has_delete_permission(request, obj)
2019-08-16 12:34:16 +00:00
def add_view(self, request, form_url='', extra_context=None):
"""
Autoselect game master when creating a new game
"""
# Make GET data mutable
data = request.GET.copy()
data['game_master'] = request.user
request.GET = data
return super().add_view(request, form_url, extra_context)
2019-08-15 09:36:39 +00:00
admin_site.register(GameSave, GameSaveAdmin)