|
|
@ -211,3 +211,39 @@ class AdminPodcastPlaylistViewSet(ModelViewSet): |
|
|
context={"request": request}, |
|
|
context={"request": request}, |
|
|
) |
|
|
) |
|
|
return Response(serializer.data) |
|
|
return Response(serializer.data) |
|
|
|
|
|
|
|
|
|
|
|
@action(detail=True, methods=["post"], url_path="assign-items") |
|
|
|
|
|
def assign_items(self, request, pk=None): |
|
|
|
|
|
playlist = self.get_object() |
|
|
|
|
|
add_ids = request.data.get("add_ids", []) or request.data.get("podcast_ids", []) or request.data.get("item_ids", []) |
|
|
|
|
|
remove_ids = request.data.get("remove_ids", []) |
|
|
|
|
|
|
|
|
|
|
|
with transaction.atomic(): |
|
|
|
|
|
if remove_ids: |
|
|
|
|
|
playlist.playlist_items.filter(id__in=remove_ids).delete() |
|
|
|
|
|
|
|
|
|
|
|
if add_ids: |
|
|
|
|
|
existing_podcast_ids = set(playlist.playlist_items.values_list("podcast_id", flat=True)) |
|
|
|
|
|
new_podcast_ids = [pid for pid in add_ids if pid not in existing_podcast_ids] |
|
|
|
|
|
current_count = playlist.playlist_items.count() |
|
|
|
|
|
new_items = [ |
|
|
|
|
|
PlaylistItem( |
|
|
|
|
|
playlist=playlist, |
|
|
|
|
|
podcast_id=pid, |
|
|
|
|
|
priority=current_count + idx + 1, |
|
|
|
|
|
) |
|
|
|
|
|
for idx, pid in enumerate(new_podcast_ids) |
|
|
|
|
|
] |
|
|
|
|
|
if new_items: |
|
|
|
|
|
PlaylistItem.objects.bulk_create(new_items) |
|
|
|
|
|
|
|
|
|
|
|
playlist.total_time = playlist.calculate_total_time() |
|
|
|
|
|
playlist.save(update_fields=["total_time"]) |
|
|
|
|
|
|
|
|
|
|
|
serializer = AdminPodcastPlaylistItemSerializer( |
|
|
|
|
|
playlist.playlist_items.select_related("podcast").order_by("priority", "id"), |
|
|
|
|
|
many=True, |
|
|
|
|
|
context={"request": request}, |
|
|
|
|
|
) |
|
|
|
|
|
return Response(serializer.data, status=status.HTTP_200_OK) |
|
|
|
|
|
|