From: yomguy Date: Wed, 16 Nov 2011 16:37:50 +0000 (+0100) Subject: fix some cases in import scripts X-Git-Tag: 1.2.1~17 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=cd2bf2abbfe93ce47b0551aeda4a53fc833eb126;p=telemeta.git fix some cases in import scripts --- diff --git a/tools/scripts/telemeta-crem-import-new.py b/tools/scripts/telemeta-crem-import-new.py index 797a0fa4..536121a6 100755 --- a/tools/scripts/telemeta-crem-import-new.py +++ b/tools/scripts/telemeta-crem-import-new.py @@ -43,44 +43,54 @@ class TelemetaWavImport: self.collections = os.listdir(self.source_dir) self.pattern = pattern + def write_file(self, item, wav_file): + if os.path.exists(wav_file): + if not item.file : + f = open(wav_file, 'r') + file_content = ContentFile(f.read()) + item.file.save(filename, file_content) + f.close() + item.code = new_ref + item.save() + else: + msg = code + ' : fichier ' + wav_file + ' déjà ajouté !' + self.logger.write_error(collection, msg) + else: + msg = code + ' : fichier audio ' + wav_file + ' inexistant !' + self.logger.write_error(collection, msg) + def wav_import(self): - from telemeta.models import MediaItem + from telemeta.models import MediaItem, MediaCollection + for collection in self.collections: + collection_name = collection.split(os.sep)[-1] collection_dir = self.source_dir + os.sep + collection if not '/.' in collection_dir and self.pattern in collection_dir: - self.collection_name = collection.split(os.sep)[-1] msg = '************************ ' + collection + ' ******************************' self.logger.write_info(collection, msg[:70]) - + c = MediaCollection.objects.filter(code=collection_name) + if len(c) == 0: + msg = collection + ' collection NON présente dans la BDD, CREATION ' + self.logger.write_info(collection, msg[:70]) + c = MediaCollection(code=collection_name) + c.save() + else: + c = c[0] + collection_files = os.listdir(collection_dir) for filename in collection_files: wav_file = self.source_dir + os.sep + collection + os.sep + filename code = filename.split('.')[0] - print code items = MediaItem.objects.filter(code=code) if len(items) != 0: + msg = item.code+' : id = '+str(item.id)+" : title = "+item.title + self.logger.write_info(collection, msg) item = items[0] - print item.code + ' : id = ' + str(item.id) + " : title = " + item.title - if os.path.exists(wav_file): - if not item.file : - f = open(wav_file, 'r') - file_content = ContentFile(f.read()) - item.file.save(filename, file_content) - f.close() - item.code = new_ref - item.save() - else: - msg = code + ' : fichier ' + wav_file + ' déjà ajouté !' - print msg - self.logger.write_error(collection, msg) - else: - msg = code + ' : fichier audio ' + wav_file + ' inexistant !' - print msg - self.logger.write_error(collection, msg) else: - msg = code + ' : item inexistant dans la base de données !' - print msg - self.logger.write_error(collection, msg) + msg = item.code + ' : item NON présent dans la base de données, CREATION' + self.logger.write_info(collection, msg) + item = MediaItem(code=code, collection=c) + self.write_file(item, wav_file) def print_usage(tool_name): diff --git a/tools/scripts/telemeta-crem-import.py b/tools/scripts/telemeta-crem-import.py index 0e5d4967..941f27ea 100755 --- a/tools/scripts/telemeta-crem-import.py +++ b/tools/scripts/telemeta-crem-import.py @@ -46,14 +46,23 @@ class TelemetaWavImport: self.pattern = pattern def wav_import(self): - from telemeta.models import MediaItem + from telemeta.models import MediaItem, MediaCollection + for collection in self.collections: collection_dir = self.source_dir + os.sep + collection if not '/.' in collection_dir and self.pattern in collection_dir: - self.collection_name = collection.split(os.sep)[-1] + collection_name = collection.split(os.sep)[-1] + c = MediaCollection.objects.filter(code=collection_name) + if len(c) == 0: + sys.exit(msg = collection + ' collection NON présente dans la BDD, SORTIE ') + + for collection in self.collections: + collection_dir = self.source_dir + os.sep + collection + if not '/.' in collection_dir and self.pattern in collection_dir: + collection_name = collection.split(os.sep)[-1] msg = '************************ ' + collection + ' ******************************' self.logger.write_info(collection, msg[:70]) - + collection_files = os.listdir(collection_dir) if not collection + '.csv' in collection_files: msg = 'Le fichier CSV est mal nommé ou inexistant'