handle file updates

This commit is contained in:
j 2011-04-18 21:12:41 +02:00
parent 99899235ce
commit 24cf7e5625

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']
if item:
file.item = item
else:
file.item = get_or_create_item(volume, f, user) 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()