mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 12:11:13 -05:00
81 lines
3.6 KiB
Python
81 lines
3.6 KiB
Python
from django.contrib import admin
|
|
from django.contrib.admin import AdminSite
|
|
from django.utils.html import format_html
|
|
from django.urls import reverse
|
|
from .models import EditSubmission, PhotoSubmission
|
|
|
|
class ModerationAdminSite(AdminSite):
|
|
site_header = 'ThrillWiki Moderation'
|
|
site_title = 'ThrillWiki Moderation'
|
|
index_title = 'Moderation Dashboard'
|
|
|
|
def has_permission(self, request):
|
|
"""Only allow moderators and above to access this admin site"""
|
|
return request.user.is_authenticated and request.user.role in ['MODERATOR', 'ADMIN', 'SUPERUSER']
|
|
|
|
moderation_site = ModerationAdminSite(name='moderation')
|
|
|
|
class EditSubmissionAdmin(admin.ModelAdmin):
|
|
list_display = ['id', 'user_link', 'content_type', 'content_link', 'status', 'created_at', 'handled_by']
|
|
list_filter = ['status', 'content_type', 'created_at']
|
|
search_fields = ['user__username', 'reason', 'source', 'notes']
|
|
readonly_fields = ['user', 'content_type', 'object_id', 'changes', 'created_at']
|
|
|
|
def user_link(self, obj):
|
|
url = reverse('admin:accounts_user_change', args=[obj.user.id])
|
|
return format_html('<a href="{}">{}</a>', url, obj.user.username)
|
|
user_link.short_description = 'User'
|
|
|
|
def content_link(self, obj):
|
|
if hasattr(obj.content_object, 'get_absolute_url'):
|
|
url = obj.content_object.get_absolute_url()
|
|
return format_html('<a href="{}">{}</a>', url, str(obj.content_object))
|
|
return str(obj.content_object)
|
|
content_link.short_description = 'Content'
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
if 'status' in form.changed_data:
|
|
if obj.status == 'APPROVED':
|
|
obj.approve(request.user)
|
|
elif obj.status == 'REJECTED':
|
|
obj.reject(request.user)
|
|
elif obj.status == 'ESCALATED':
|
|
obj.escalate(request.user)
|
|
super().save_model(request, obj, form, change)
|
|
|
|
class PhotoSubmissionAdmin(admin.ModelAdmin):
|
|
list_display = ['id', 'user_link', 'content_type', 'content_link', 'photo_preview', 'status', 'created_at', 'handled_by']
|
|
list_filter = ['status', 'content_type', 'created_at']
|
|
search_fields = ['user__username', 'caption', 'notes']
|
|
readonly_fields = ['user', 'content_type', 'object_id', 'photo_preview', 'created_at']
|
|
|
|
def user_link(self, obj):
|
|
url = reverse('admin:accounts_user_change', args=[obj.user.id])
|
|
return format_html('<a href="{}">{}</a>', url, obj.user.username)
|
|
user_link.short_description = 'User'
|
|
|
|
def content_link(self, obj):
|
|
if hasattr(obj.content_object, 'get_absolute_url'):
|
|
url = obj.content_object.get_absolute_url()
|
|
return format_html('<a href="{}">{}</a>', url, str(obj.content_object))
|
|
return str(obj.content_object)
|
|
content_link.short_description = 'Content'
|
|
|
|
def photo_preview(self, obj):
|
|
if obj.photo:
|
|
return format_html('<img src="{}" style="max-height: 100px; max-width: 200px;" />', obj.photo.url)
|
|
return ''
|
|
photo_preview.short_description = 'Photo Preview'
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
if 'status' in form.changed_data:
|
|
if obj.status == 'APPROVED':
|
|
obj.approve(request.user, obj.notes)
|
|
elif obj.status == 'REJECTED':
|
|
obj.reject(request.user, obj.notes)
|
|
super().save_model(request, obj, form, change)
|
|
|
|
# Register with moderation site only
|
|
moderation_site.register(EditSubmission, EditSubmissionAdmin)
|
|
moderation_site.register(PhotoSubmission, PhotoSubmissionAdmin)
|