diff --git a/apps/library/views_admin.py b/apps/library/views_admin.py index e32e7b2..cc8ada4 100644 --- a/apps/library/views_admin.py +++ b/apps/library/views_admin.py @@ -135,6 +135,23 @@ class AdminAuthorViewSet(ModelViewSet): return queryset.order_by("full_name", "id") + @action(detail=True, methods=["post"], url_path="assign-books") + def assign_books(self, request, pk=None): + author = self.get_object() + add_ids = request.data.get("add_ids", []) or request.data.get("book_ids", []) + remove_ids = request.data.get("remove_ids", []) + + if add_ids: + Book.objects.filter(id__in=add_ids).update(author=author) + + if remove_ids: + Book.objects.filter(id__in=remove_ids, author=author).update(author=None) + + return Response({ + "status": "success", + "attached_count": author.books.count() + }) + class AdminBookViewSet(ModelViewSet): permission_classes = [IsAuthenticated, IsSuperAdmin]