Box sorting works
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
from adminsortable2.admin import SortableAdminMixin
|
||||
import json
|
||||
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.contrib.admin import SimpleListFilter
|
||||
from django.http import JsonResponse
|
||||
from django.urls import path
|
||||
from django.utils.html import format_html
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from .models import Box, BoxType, Facet, Tag, Thing, ThingFile, ThingLink
|
||||
|
||||
@@ -48,12 +52,37 @@ class BoxTypeAdmin(admin.ModelAdmin):
|
||||
|
||||
|
||||
@admin.register(Box)
|
||||
class BoxAdmin(SortableAdminMixin, admin.ModelAdmin):
|
||||
class BoxAdmin(admin.ModelAdmin):
|
||||
"""Admin configuration for Box model."""
|
||||
|
||||
list_display = ('id', 'box_type')
|
||||
ordering = ['sort_order']
|
||||
list_display = ('id', 'box_type', 'sort_order')
|
||||
list_filter = ('box_type',)
|
||||
search_fields = ('id',)
|
||||
change_list_template = 'admin/boxes/box/change_list.html'
|
||||
|
||||
def get_urls(self):
|
||||
urls = super().get_urls()
|
||||
custom_urls = [
|
||||
path('reorder/', self.admin_site.admin_view(self.reorder_view), name='boxes_box_reorder'),
|
||||
]
|
||||
return custom_urls + urls
|
||||
|
||||
def reorder_view(self, request):
|
||||
"""Handle AJAX reorder requests."""
|
||||
if request.method != 'POST':
|
||||
return JsonResponse({'error': 'POST required'}, status=405)
|
||||
|
||||
try:
|
||||
data = json.loads(request.body)
|
||||
order = data.get('order', [])
|
||||
|
||||
for index, pk in enumerate(order):
|
||||
Box.objects.filter(pk=pk).update(sort_order=index)
|
||||
|
||||
return JsonResponse({'status': 'ok'})
|
||||
except (json.JSONDecodeError, KeyError) as e:
|
||||
return JsonResponse({'error': str(e)}, status=400)
|
||||
|
||||
|
||||
class ThingFileInline(admin.TabularInline):
|
||||
|
||||
Reference in New Issue
Block a user