From 38570b6517eb4ae21f143e115d33625ff670fec5 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Wed, 22 Jan 2014 14:28:54 +0100 Subject: [PATCH] fix path --- .../telemeta-import-items-without-copy.py | 2 +- .../commands/telemeta-media-import.py | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 telemeta/management/commands/telemeta-media-import.py diff --git a/telemeta/management/commands/telemeta-import-items-without-copy.py b/telemeta/management/commands/telemeta-import-items-without-copy.py index 5b356ceb..d11c28e2 100644 --- a/telemeta/management/commands/telemeta-import-items-without-copy.py +++ b/telemeta/management/commands/telemeta-import-items-without-copy.py @@ -36,7 +36,7 @@ class Command(BaseCommand): if not items: item = MediaItem(collection=collection, code=name) item.title = name - item.file.path = path + item.file = path item.public_access = 'full' item.save() print 'item created: ' + item.code diff --git a/telemeta/management/commands/telemeta-media-import.py b/telemeta/management/commands/telemeta-media-import.py new file mode 100644 index 00000000..c73d4f2f --- /dev/null +++ b/telemeta/management/commands/telemeta-media-import.py @@ -0,0 +1,91 @@ +from optparse import make_option +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError +from django.core.files.base import ContentFile +from telemeta.models import * +from telemeta.util.unaccent import unaccent +from telemeta.cache import TelemetaCache +import urllib + +try: + from django.utils.text import slugify +except ImportError: + def slugify(string): + killed_chars = re.sub('[\(\),]', '', string) + return re.sub(' ', '_', killed_chars) + +def beautify(string): + return os.path.splitext(string)[0].replace('_',' ') + +class Command(BaseCommand): + args = "" + help = "Download and import a media item" + option_list = BaseCommand.option_list + ( + make_option('--collection-code', + action='store', + dest='code', + default='default', + metavar = '', + help='collection code'), + make_option('--collection-title', + action='store', + dest='title', + default='default', + metavar = '', + help='collection title'), + ) + + cache_data = TelemetaCache(settings.TELEMETA_DATA_CACHE_DIR) + cache_export = TelemetaCache(settings.TELEMETA_EXPORT_CACHE_DIR) + + urls = [] + + def handle(self, *args, **options): + if len(args) < 1: + return + if options['title']: + self.title = options['title'] + if options['code']: + self.code = options['code'] + for file in args: + self.urls.append('file://' + file) + + collections = MediaCollection.objects.filter(code=self.code) + if not collections: + # create a new collection + collection = MediaCollection(code=self.code, title=self.title) + collection.public_access = 'full' + collection.save() + else: + collection = collections[0] + + for url in self.urls: + basename = os.path.basename(url) + code = slugify(basename) + title = beautify(basename) + items = MediaItem.objects.filter(code=code) + if not items: + item = MediaItem(collection=collection, code=code, title=title) + item.save() + else: + print 'cleaning up', code + item = items[0] + self.cache_data.delete_item_data(code) + self.cache_export.delete_item_data(code) + flags = MediaItemTranscodingFlag.objects.filter(item=item) + analyses = MediaItemAnalysis.objects.filter(item=item) + for flag in flags: + flag.delete() + for analysis in analyses: + analysis.delete() + + print 'fetching: ' + url + file = urllib.urlopen(url) + file_content = ContentFile(file.read()) + item.title = title + item.file.save(code, file_content) + item.public_access = 'full' + item.save() + print 'item created: ', collection, code + + print 'done importing', len(self.urls), 'items' -- 2.39.5