From 8bbeb5fe6d9cabfa3c7b1e3a0d454ed5d092e3c0 Mon Sep 17 00:00:00 2001 From: bkfox Date: Wed, 4 Jan 2017 18:12:58 +0100 Subject: [PATCH] fix error in playlist import + more verbose command --- aircox/management/commands/import_playlist.py | 44 ++++++++++++------- aircox/models.py | 5 ++- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/aircox/management/commands/import_playlist.py b/aircox/management/commands/import_playlist.py index d3a2316..4cf7f03 100755 --- a/aircox/management/commands/import_playlist.py +++ b/aircox/management/commands/import_playlist.py @@ -26,6 +26,7 @@ logger = logging.getLogger('aircox.tools') class Importer: + path = None data = None tracks = None @@ -43,6 +44,8 @@ class Importer: if not os.path.exists(path): return True with open(path, 'r') as file: + logger.info('start reading csv ' + path) + self.path = path self.data = list(csv.DictReader( (row for row in file if not row.startswith('#')), fieldnames = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS, @@ -58,26 +61,35 @@ class Importer: maps = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS tracks = [] + logger.info('parse csv file ' + self.path) in_seconds = ('minutes' or 'seconds') in maps for index, line in enumerate(self.data): - position = \ - int(line.get('minute') or 0) * 60 + \ - int(line.get('seconds') or 0) \ - if in_seconds else index + if ('title' or 'artist') not in line: + return - track, created = Track.objects.get_or_create( - related_type = ContentType.objects.get_for_model(related), - related_id = related.pk, - title = line.get('title'), - artist = line.get('artist'), - position = position, - ) + try: + position = \ + int(line.get('minute') or 0) * 60 + \ + int(line.get('seconds') or 0) \ + if in_seconds else index - track.in_seconds = in_seconds - track.info = line.get('info') - tags = line.get('tags') - if tags: - track.tags.add(*tags.split(',')) + track, created = Track.objects.get_or_create( + related_type = ContentType.objects.get_for_model(related), + related_id = related.pk, + title = line.get('title'), + artist = line.get('artist'), + position = position, + ) + + track.in_seconds = in_seconds + track.info = line.get('info') + tags = line.get('tags') + if tags: + track.tags.add(*tags.split(',')) + except: + logger.warning('an error occured for track {index}, it may not ' + 'have been saved'.format(index = index)) + continue if save: track.save() diff --git a/aircox/models.py b/aircox/models.py index 9cab39b..b7e7615 100755 --- a/aircox/models.py +++ b/aircox/models.py @@ -1004,12 +1004,15 @@ class Sound(Nameable): except: meta = {} + if meta is None: + meta = {} + def get_meta(key, cast=str): value = meta.get(key) return cast(value[0]) if value else None info = '{} ({})'.format(get_meta('album'), get_meta('year')) \ - if 'album' and 'year' in meta else \ + if meta and ('album' and 'year' in meta) else \ get_meta('album') \ if 'album' else \ ('year' in meta) and get_meta('year') or ''