handle file updates

This commit is contained in:
j 2011-04-18 21:12:41 +02:00
parent 5744fdb53a
commit 1f3789eb52

View file

@ -25,18 +25,22 @@ def get_or_create_item(volume, f, user):
i = get_item(item_info, user) i = get_item(item_info, user)
return i return i
def get_or_create_file(volume, f, user): def get_or_create_file(volume, f, user, item=None):
try: try:
file = models.File.objects.get(oshash=f['oshash']) file = models.File.objects.get(oshash=f['oshash'])
except models.File.DoesNotExist: except models.File.DoesNotExist:
file = models.File() file = models.File()
file.oshash = f['oshash'] file.oshash = f['oshash']
file.name = f['name'] file.name = f['name']
file.item = get_or_create_item(volume, f, user) if item:
file.item = item
else:
file.item = get_or_create_item(volume, f, user)
file.save() file.save()
return file return file
def update_or_create_instance(volume, f): def update_or_create_instance(volume, f):
#instance with oshash exists
instance = models.Instance.objects.filter(file__oshash=f['oshash'], volume=volume) instance = models.Instance.objects.filter(file__oshash=f['oshash'], volume=volume)
if instance.count()>0: if instance.count()>0:
instance = instance[0] instance = instance[0]
@ -49,9 +53,17 @@ def update_or_create_instance(volume, f):
instance.save() instance.save()
instance.file.save() instance.file.save()
else: else:
instance = models.Instance.objects.filter(name=f['name'], folder=f['folder'], volume=volume)
if instance.count()>0:
#same path, other oshash, keep path/item mapping, remove instance
item = instance[0].item
instance.delete()
else: #new instance
item = None
instance = models.Instance() instance = models.Instance()
instance.volume = volume instance.volume = volume
instance.file = get_or_create_file(volume, f, volume.user) instance.file = get_or_create_file(volume, f, volume.user, item)
for key in _INSTANCE_KEYS: for key in _INSTANCE_KEYS:
setattr(instance, key, f[key]) setattr(instance, key, f[key])
instance.save() instance.save()
@ -72,7 +84,7 @@ def update_files(user, volume, files):
f['name'] = name f['name'] = name
all_files.append(f['oshash']) all_files.append(f['oshash'])
update_or_create_instance(volume, f) update_or_create_instance(volume, f)
#remove deleted files #remove deleted files
#FIXME: can this have any bad consequences? i.e. on the selction of used item files. #FIXME: can this have any bad consequences? i.e. on the selction of used item files.
models.Instance.objects.filter(volume=volume).exclude(file__oshash__in=all_files).delete() models.Instance.objects.filter(volume=volume).exclude(file__oshash__in=all_files).delete()