116 lines
3.4 KiB
Python
116 lines
3.4 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, SubmittedQuestion
|
|
from lib.mixins import PermissionsAdminMixin
|
|
from lib.utils import color_label
|
|
|
|
|
|
class ActiveFilter(admin.SimpleListFilter):
|
|
title = "Aktiv/Inaktiv"
|
|
parameter_name = 'released'
|
|
|
|
def lookups(self, request, model_admin):
|
|
return [
|
|
(1, 'Inaktiv'),
|
|
(0, 'Aktiv')
|
|
]
|
|
|
|
def queryset(self, request, queryset):
|
|
v = int(self.value() or -1)
|
|
if v == 1:
|
|
return queryset.filter(**{self.parameter_name: True})
|
|
elif v == 0:
|
|
return queryset.filter(**{self.parameter_name: False})
|
|
else:
|
|
return queryset.filter(**{self.parameter_name: False})
|
|
# return queryset
|
|
|
|
|
|
@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(SubmittedQuestion)
|
|
class SubmittedQuestionAdmin(admin.ModelAdmin):
|
|
list_filter = [ActiveFilter,]
|
|
list_display = ['__str__', 'release']
|
|
list_editable = ['release',]
|
|
|
|
|
|
@admin.register(Question)
|
|
class QuestionAdmin(PermissionsAdminMixin, admin.ModelAdmin):
|
|
autocomplete_fields = ('medias', 'links', 'labels', 'shares')
|
|
list_display = ('name', 'list_labels', 'list_level', 'author', 'is_published')
|
|
search_fields = ('name', 'question', 'awnser', 'description', 'label__name', 'level__value', 'level__name',)
|
|
list_editable = ('is_published',)
|
|
|
|
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
|