]> git.parisson.com Git - telemeta.git/commitdiff
fix corpus import again
authorGuillaume Pellerin <yomguy@parisson.com>
Thu, 7 May 2015 08:42:57 +0000 (10:42 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Thu, 7 May 2015 08:42:57 +0000 (10:42 +0200)
telemeta/management/commands/telemeta-import-corpus-from-dir.py

index f38037a34695300a353dee952f32a154c7946da0..04105ba15afe391b685786f2c64ebf93c32f3119 100644 (file)
@@ -48,6 +48,27 @@ class Command(BaseCommand):
     image_formats = ['png', 'jpg']
     text_formats = ['txt']
 
+    def write_file(self, item, media):
+        filename = media.split(os.sep)[-1]
+        if os.path.exists(media):
+            if not item.file or self.force:
+                if not self.media_root in self.source_dir:
+                    print "file not in MEDIA_ROOT, copying..."
+                    f = open(media, 'r')
+                    if not self.dry_run:
+                        file_content = ContentFile(f.read())
+                        item.file.save(filename, file_content)
+                        item.save()
+                    f.close()
+                else:
+                    print "file in MEDIA_ROOT, linking..."
+                    path = media.replace(self.media_root, '')
+                    if not self.dry_run:
+                        item.file = path
+                        item.save()
+                if self.user:
+                    item.set_revision(self.user)
+
     def handle(self, *args, **options):
         # NOT4PROD!!
         # reset()
@@ -113,18 +134,21 @@ class Command(BaseCommand):
 
                         collection_title = collection_name.replace('_', ' ') + ' : ' + chapter_title
                         print collection_title
-                        collection, c = MediaCollection.objects.get_or_create(code=collection_id)
-                        if c:
+                        cc = MediaCollection.objects.filter(code=collection_id, title=collection_title)
+                        if cc:
+                            collection = cc[0]
+                        else:
+                            collection = MediaCollection(code=collection_id)
                             collection.title = collection_title
                             collection.save()
+
                         if not collection in corpus.children.all():
                             corpus.children.add(collection)
 
                         item, c = MediaItem.objects.get_or_create(collection=collection, code=item_id)
                         if c:
                             item.old_code = item_name
-                            # item.track = item_name
-                            item.file = media_path
+                            self.write_file(item, media_path)
                             item.save()
 
                             title = data[0].split('.')