From 653d2237a5b53178d32b3df7a66830dc1c50e6b1 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Thu, 7 May 2015 10:42:57 +0200 Subject: [PATCH] fix corpus import again --- .../telemeta-import-corpus-from-dir.py | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/telemeta/management/commands/telemeta-import-corpus-from-dir.py b/telemeta/management/commands/telemeta-import-corpus-from-dir.py index f38037a3..04105ba1 100644 --- a/telemeta/management/commands/telemeta-import-corpus-from-dir.py +++ b/telemeta/management/commands/telemeta-import-corpus-from-dir.py @@ -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('.') -- 2.39.5