From 50e2e2a8c94fa0502fc03fcfbcc828b5e902ec8b Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 2 Feb 2011 16:06:04 +0530 Subject: [PATCH] refactor file update code --- pandora/archive/tasks.py | 100 +++++++++++++++++++-------------------- pandora/settings.py | 2 + 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/pandora/archive/tasks.py b/pandora/archive/tasks.py index 3624c7a..5645e7c 100644 --- a/pandora/archive/tasks.py +++ b/pandora/archive/tasks.py @@ -10,6 +10,51 @@ from django.conf import settings import models +_INSTANCE_KEYS = ('mtime', 'name', 'folder') + + +def get_or_create_item(volume, f): + in_same_folder = models.Instance.objects.filter(folder=f['folder'], volume=volume) + if in_same_folder.count() > 0: + i = in_same_folder[0].file.item + else: + if settings.USE_FOLDER: + item_info = parse_path(f['folder']) + else: + item_info = parse_path(f['path']) + i = get_item(item_info) + return i + +def get_or_create_file(volume, f): + try: + file = models.File.objects.get(oshash=f['oshash']) + except models.File.DoesNotExist: + file = models.File() + file.oshash = f['oshash'] + file.name = f['name'] + file.item = get_or_create_item(volume, f) + file.save() + return file + +def update_or_create_instance(i, volume, f): + instance = models.Instance.objects.filter(file__oshash=f['oshash'], volume=f['volume']) + if instance.count()>0: + instance = instance[0] + updated = False + for key in _INSTANCE_KEYS: + if f[key] != getattr(instance, key): + setattr(instance, key, f[key]) + updated=True + if updated: + instance.save() + else: + instance = models.Instance() + instance.volume = volume + instance.file = get_or_create_file(volume, f) + for key in _INSTANCE_KEYS: + setattr(instance, key, f[key]) + instance.save() + return instance @task(ignore_resulsts=True, queue='default') def update_files(user, volume, files): @@ -17,61 +62,14 @@ def update_files(user, volume, files): volume, created = models.Volume.objects.get_or_create(user=user, name=volume) all_files = [] for f in files: - path = f['path'] - folder = path.split('/') + folder = f['path'].split('/') name = folder.pop() if folder and folder[-1] in ('Extras', 'Versions', 'DVDs'): name = '/'.join([folder.pop(), name]) - folder = '/'.join(folder) - #print folder - #print name - f['folder'] = folder + f['folder'] = '/'.join(folder) f['name'] = name - oshash = f['oshash'] - all_files.append(oshash) - - same_folder = models.Instance.objects.filter(folder=folder, volume=volume) - if same_folder.count() > 0: - i = same_folder[0].file.item - else: - i = None - - path = os.path.join(folder, name) - - instance = models.Instance.objects.filter(file__oshash=oshash, volume=volume) - if instance.count()>0: - instance = instance[0] - updated = False - for key in ('mtime', 'name', 'folder'): - if f[key] != getattr(instance, key): - setattr(instance, key, f[key]) - updated=True - if updated: - instance.save() - else: - #look if oshash is known - file_objects = models.File.objects.filter(oshash=oshash) - if file_objects.count() > 0: - file_object = file_objects[0] - #new oshash, add to database - else: - if not i: - if settings.USE_IMDB: - item_info = parse_path(folder) - else: - item_info = parse_path(path) - i = get_item(item_info) - file_object = models.File() - file_object.oshash = oshash - file_object.name = name - file_object.item = i - file_object.save() - instance = models.Instance() - instance.volume = volume - instance.file = file_object - for key in ('mtime', 'name', 'folder'): - setattr(instance, key, f[key]) - instance.save() + all_files.append(f['oshash']) + update_or_create_instance(volume, f) #remove deleted files #FIXME: can this have any bad consequences? i.e. on the selction of used item files. diff --git a/pandora/settings.py b/pandora/settings.py index fffce4e..55150a9 100644 --- a/pandora/settings.py +++ b/pandora/settings.py @@ -193,6 +193,8 @@ DEFAULT_LISTS = [ ] #0xdb.org USE_IMDB = True +#this should idealy go away, one folder per item +USE_FOLDER = True #POSTER_SERVICES=['http://data.0xdb.org/poster/']