From c186e18cdd927b001eac6759abce74dfbc3e169e Mon Sep 17 00:00:00 2001
From: Ernesto Rico Schmidt <ernesto@helsinki.at>
Date: Mon, 4 Nov 2024 12:47:47 -0400
Subject: [PATCH] feat: add update method to PlaylistSerializer

---
 program/serializers.py | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/program/serializers.py b/program/serializers.py
index bf71dee7..9f5ad200 100644
--- a/program/serializers.py
+++ b/program/serializers.py
@@ -1527,3 +1527,33 @@ class PlaylistSerializer(serializers.ModelSerializer):
             PlaylistEntry.objects.create(playlist=playlist, **entry_data)
 
         return playlist
+
+    def update(self, instance, validated_data):
+        """Update an existing Playlist instance, given the validated data."""
+
+        user = self.context["request"].user
+        user_is_owner = user in instance.show.owners.all()
+
+        # having the update_playlist permission overrules the ownership
+        if not (user.has_perm("program.update_playlist") or user_is_owner):
+            raise exceptions.PermissionDenied(
+                detail="You are not allowed to update this playlist."
+            )
+
+        if "description" in validated_data:
+            instance.description = validated_data.pop("description")
+
+        if "playout_mode" in validated_data:
+            instance.playout_mode = validated_data.pop("playout_mode")
+
+        if "entries" in validated_data:
+            if instance.entries.count() > 0:
+                for entry in instance.entries.all():
+                    entry.delete(keep_parents=True)
+
+            for entry_data in validated_data.get("entries"):
+                PlaylistEntry.objects.create(playlist=instance, **entry_data)
+
+        instance.save()
+
+        return instance
-- 
GitLab