2024-07-03 21:41:03 +02:00
|
|
|
from django.contrib import admin
|
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
|
2024-07-12 17:22:17 +02:00
|
|
|
from content.models import Link, MediaFile, Question, QuestionVersion, Level, Label, SharedQuestion
|
2024-07-04 14:05:55 +02:00
|
|
|
from lib.mixins import PermissionsAdminMixin
|
2024-07-12 17:22:17 +02:00
|
|
|
from lib.utils import color_label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(SharedQuestion)
|
|
|
|
|
class SharedQuestionAdmin(admin.ModelAdmin):
|
|
|
|
|
search_fields = ('user__username',)
|
2024-07-03 21:41:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Level)
|
2024-07-04 14:05:55 +02:00
|
|
|
class LevelAdmin(PermissionsAdminMixin, admin.ModelAdmin):
|
2024-07-03 21:41:03 +02:00
|
|
|
search_fields = ('name', 'value',)
|
|
|
|
|
list_display = ('name', 'value', 'color')
|
|
|
|
|
list_editable = ('color',)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Label)
|
2024-07-04 14:05:55 +02:00
|
|
|
class LabelAdmin(PermissionsAdminMixin, admin.ModelAdmin):
|
2024-07-03 21:41:03 +02:00
|
|
|
search_fields = ('name',)
|
|
|
|
|
list_display = ('name', 'color')
|
|
|
|
|
list_editable = ('color',)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Question)
|
2024-07-04 14:05:55 +02:00
|
|
|
class QuestionAdmin(PermissionsAdminMixin, admin.ModelAdmin):
|
2024-07-12 17:22:17 +02:00
|
|
|
autocomplete_fields = ('medias', 'links', 'labels', 'shares')
|
2024-07-03 21:41:03 +02:00
|
|
|
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):
|
2024-07-12 17:22:17 +02:00
|
|
|
if instance.labels:
|
|
|
|
|
return mark_safe(', '.join([color_label(l, value=l.name) for l in instance.labels.all()]))
|
|
|
|
|
return ''
|
2024-07-03 21:41:03 +02:00
|
|
|
|
|
|
|
|
def list_level(self, instance):
|
2024-07-12 17:22:17 +02:00
|
|
|
if instance.level:
|
|
|
|
|
return mark_safe(color_label(instance.level, value=str(instance.level)))
|
|
|
|
|
return ''
|
2024-07-03 21:41:03 +02:00
|
|
|
|
|
|
|
|
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)
|
2024-07-04 14:05:55 +02:00
|
|
|
class MediaFileAdmin(PermissionsAdminMixin, admin.ModelAdmin):
|
2024-07-03 21:41:03 +02:00
|
|
|
search_fields = ('name', 'file__name')
|
|
|
|
|
list_display = ('name', 'file')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Link)
|
2024-07-04 14:05:55 +02:00
|
|
|
class LinkAdmin(PermissionsAdminMixin, admin.ModelAdmin):
|
2024-07-03 21:41:03 +02:00
|
|
|
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
|
2024-07-04 14:05:55 +02:00
|
|
|
|
|
|
|
|
def has_view_permission(self, request, obj=None):
|
|
|
|
|
return True
|