]> git.parisson.com Git - telemeta.git/commitdiff
Add a command prototype to list all items from a playlist
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Thu, 19 Oct 2017 14:57:31 +0000 (16:57 +0200)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Thu, 19 Oct 2017 14:57:31 +0000 (16:57 +0200)
telemeta/management/commands/telemeta-export-items-from-user-playlists.py [new file with mode: 0644]

diff --git a/telemeta/management/commands/telemeta-export-items-from-user-playlists.py b/telemeta/management/commands/telemeta-export-items-from-user-playlists.py
new file mode 100644 (file)
index 0000000..fee9519
--- /dev/null
@@ -0,0 +1,33 @@
+from optparse import make_option
+from django.conf import settings
+from django.core.management.base import BaseCommand, CommandError
+from django.contrib.auth.models import User
+from django.template.defaultfilters import slugify
+from django.utils import translation
+
+from telemeta.models import Playlist, MediaCollection, MediaItem
+
+
+class Command(BaseCommand):
+    help = "Export media files from playlists of a given user"
+    args = "username"
+
+    def handle(self, *args, **options):
+        username = args[0]
+        user = User.objects.get(username=username)
+        playlists = user.playlists.all()
+        items = []
+
+        for playlist in playlists:
+            resources = playlist.resources.all()
+            for resource in resources:
+                if resource.resource_type == 'collection':
+                    collection = MediaCollection.objects.get(id=resource.resource_id)
+                    for item in collection.items.all():
+                        items.append(item)
+                elif resource.resource_type == 'item':
+                    item = MediaItem.objects.get(id=resource.resource_id)
+                    items.append(item)
+
+        print(items)
+