]> git.parisson.com Git - telemeta.git/commitdiff
fix path
authorGuillaume Pellerin <yomguy@parisson.com>
Wed, 22 Jan 2014 13:28:54 +0000 (14:28 +0100)
committerGuillaume Pellerin <yomguy@parisson.com>
Wed, 22 Jan 2014 13:28:54 +0000 (14:28 +0100)
telemeta/management/commands/telemeta-import-items-without-copy.py
telemeta/management/commands/telemeta-media-import.py [new file with mode: 0644]

index 5b356ceb0321591ecb1e9e59f68814924b0614e5..d11c28e2916c451aa3e4eadd090fdb923815e26f 100644 (file)
@@ -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 (file)
index 0000000..c73d4f2
--- /dev/null
@@ -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 = "<media_file1 [media_file2 ...]>"
+    help = "Download and import a media item"
+    option_list = BaseCommand.option_list + (
+            make_option('--collection-code',
+                action='store',
+                dest='code',
+                default='default',
+                metavar = '<code>',
+                help='collection code'),
+            make_option('--collection-title',
+                action='store',
+                dest='title',
+                default='default',
+                metavar = '<title>',
+                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'