Files
Tablequizwiki/content/admin.py

87 lines
2.6 KiB
Python

from django.contrib import admin
from django.utils.safestring import mark_safe
from content.models import Link, MediaFile, Question, QuestionVersion, Level, Label, SharedQuestion
from lib.mixins import PermissionsAdminMixin
from lib.utils import color_label
@admin.register(SharedQuestion)
class SharedQuestionAdmin(admin.ModelAdmin):
search_fields = ('user__username',)
@admin.register(Level)
class LevelAdmin(PermissionsAdminMixin, admin.ModelAdmin):
search_fields = ('name', 'value',)
list_display = ('name', 'value', 'color')
list_editable = ('color',)
@admin.register(Label)
class LabelAdmin(PermissionsAdminMixin, admin.ModelAdmin):
search_fields = ('name',)
list_display = ('name', 'color')
list_editable = ('color',)
@admin.register(Question)
class QuestionAdmin(PermissionsAdminMixin, admin.ModelAdmin):
autocomplete_fields = ('medias', 'links', 'labels', 'shares')
list_display = ('name', 'list_labels', 'list_level', 'author')
search_fields = ('name', 'question', 'awnser', 'description', 'label__name', 'level__value', 'level__name',)
def list_labels(self, instance):
if instance.labels:
return mark_safe(', '.join([color_label(l, value=l.name) for l in instance.labels.all()]))
return ''
def list_level(self, instance):
if instance.level:
return mark_safe(color_label(instance.level, value=str(instance.level)))
return ''
list_level.short_description = 'Level'
list_labels.short_description = 'Labels'
"""
fieldsets = [
('Basic', {'fields': [('name', 'author',)]}),
('Question', {'fields': ('question', 'awnser',), }),
('Meta', {'fields': ('level', 'labels', 'description'), }),
('Media and Links', {'fields': ('medias', 'links'), },
)
]
"""
@admin.register(MediaFile)
class MediaFileAdmin(PermissionsAdminMixin, admin.ModelAdmin):
search_fields = ('name', 'file__name')
list_display = ('name', 'file')
@admin.register(Link)
class LinkAdmin(PermissionsAdminMixin, admin.ModelAdmin):
search_fields = ('url',)
@admin.register(QuestionVersion)
class QuestionVersionAdmin(admin.ModelAdmin):
def has_add_permission(self, request):
"""If a group right is not set :)
Allways be able to create a version
"""
return True
def has_change_permission(self, request, obj=None):
"""Versions are immutable """
return False
def has_delete_permission(self, request, obj=None):
"""Versions are immutable """
return False
def has_view_permission(self, request, obj=None):
return True