Browse Source

blog reordering action added

master
Mohsen Taba 2 months ago
parent
commit
2a5fb4d199
  1. 23
      apps/blog/views_admin.py

23
apps/blog/views_admin.py

@ -3,6 +3,8 @@ from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.authentication import TokenAuthentication
from rest_framework.parsers import MultiPartParser, FormParser, JSONParser
from rest_framework.decorators import action
from rest_framework.response import Response
from .models import Blog, BlogContent, BlogSeo
from .serializers_admin import (
@ -61,6 +63,27 @@ class AdminBlogContentViewSet(ModelViewSet):
return queryset.order_by('order', 'created_at')
@action(detail=False, methods=['post'])
def reorder(self, request):
"""
Reorders content blocks in bulk.
Format: {"content_ids": [id1, id2, ...]}
"""
content_ids = request.data.get('content_ids', [])
if not content_ids:
return Response({'error': 'content_ids list is required'}, status=400)
from django.db import transaction
with transaction.atomic():
# First pass: temporary shift to prevent unique constraint conflicts
for idx, c_id in enumerate(content_ids):
BlogContent.objects.filter(id=c_id).update(order=idx + 10000)
# Second pass: set the correct final order values
for idx, c_id in enumerate(content_ids):
BlogContent.objects.filter(id=c_id).update(order=idx + 1)
return Response({'status': 'success'})
class AdminBlogSeoViewSet(ModelViewSet):
"""
Admin CRUD ViewSet for managing SEO meta tags for a specific blog.

Loading…
Cancel
Save