|
|
@ -535,3 +535,24 @@ class AdminHadisTransmitterViewSet(ModelViewSet): |
|
|
if hadis_id: |
|
|
if hadis_id: |
|
|
queryset = queryset.filter(hadis_id=hadis_id) |
|
|
queryset = queryset.filter(hadis_id=hadis_id) |
|
|
return queryset.order_by("order") |
|
|
return queryset.order_by("order") |
|
|
|
|
|
|
|
|
|
|
|
@action(detail=False, methods=["post"]) |
|
|
|
|
|
def reorder(self, request): |
|
|
|
|
|
from rest_framework.decorators import action |
|
|
|
|
|
from rest_framework.response import Response |
|
|
|
|
|
from django.db import transaction |
|
|
|
|
|
|
|
|
|
|
|
hadis_transmitter_ids = request.data.get("hadis_transmitter_ids", []) |
|
|
|
|
|
if not hadis_transmitter_ids: |
|
|
|
|
|
return Response({"error": "hadis_transmitter_ids list is required"}, status=400) |
|
|
|
|
|
|
|
|
|
|
|
with transaction.atomic(): |
|
|
|
|
|
# Pass 1: Set temporary positive offset order indices to avoid duplicate key violations (>= 0) |
|
|
|
|
|
for index, ht_id in enumerate(hadis_transmitter_ids): |
|
|
|
|
|
HadisTransmitter.objects.filter(id=ht_id).update(order=10000 + index) |
|
|
|
|
|
|
|
|
|
|
|
# Pass 2: Set final sequential order indices starting from 0 |
|
|
|
|
|
for index, ht_id in enumerate(hadis_transmitter_ids): |
|
|
|
|
|
HadisTransmitter.objects.filter(id=ht_id).update(order=index) |
|
|
|
|
|
|
|
|
|
|
|
return Response({"status": "success"}) |