From: Guillaume Pellerin Date: Thu, 19 Oct 2017 14:57:31 +0000 (+0200) Subject: Add a command prototype to list all items from a playlist X-Git-Tag: 1.7.0~2^2~11 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=bb43a2e63f7f7c337b01ef855d426a84b73eeee5;p=telemeta.git Add a command prototype to list all items from a playlist --- 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 index 00000000..fee9519b --- /dev/null +++ b/telemeta/management/commands/telemeta-export-items-from-user-playlists.py @@ -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) +