From 4f7c7e9abeb3aa833031417956c6866d8eead3e0 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 11 Sep 2013 12:18:01 +0200 Subject: [PATCH] add method to recover an item from changelog --- pandora/changelog/recover.py | 92 ++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 pandora/changelog/recover.py diff --git a/pandora/changelog/recover.py b/pandora/changelog/recover.py new file mode 100644 index 00000000..165f2e16 --- /dev/null +++ b/pandora/changelog/recover.py @@ -0,0 +1,92 @@ +import models +import item.models +import user.models +import archive.models + +def recover_item(id): + if item.models.Item.objects.filter(itemId=id).exists(): + raise Exception('id is taken') + qs = models.Changelog.objects.filter(value__contains='id": "%s"' % id) + if not qs.exists(): + raise Exception('id not found') + old = qs.order_by('-created')[0] + i = item.models.Item() + i.itemId = id + i.data = old.value + created = old.value['created'] + i.user = user.models.User.objects.get(username=i.data['user']) + for key in [ + u'rendered', + u'random', + u'cuts', + u'duration', + u'id', + u'size', + u'posterFrame', + u'parts', + u'cutsperminute', + u'hue', + u'numberofcuts', + u'durations', + u'volume', + u'user', + u'words', + u'videoRatio', + u'aspectratio', + u'bitrate', + u'pixels', + u'created', + u'numberoffiles', + u'modified', + u'timesaccessed', + u'accessed', + u'resolution', + u'wordsperminute', + u'posterRatio' + ]: + if key in i.data: + del i.data[key] + i.save() + i.itemId = id + i.created = created + i.save() + i.update_sort() + i.update_find() + print 'created', i, i.id + + for a in models.Changelog.objects.filter(value__contains='id": "%s/' % id).order_by('created'): + qs = i.annotations.filter(public_id=a.value['id']) + if qs.exists(): + annot = qs[0] + else: + annot = item.models.Annotation() + annot.item = i + annot.public_id = a.value['id'] + annot.created = a.value['created'] + annot.modified = a.value['modified'] + annot.user = user.models.User.objects.get(username=a.value['user']) + annot.start = a.value['in'] + annot.end = a.value['out'] + annot.value = a.value['value'] + annot.layer = a.value.get('layer', 'transcripts') + annot.save() + return i + +def recover_file(id, oshash, filename): + i = item.models.Item.objects.get(itemId=id) + file, created = archive.models.File.objects.get_or_create(oshash=oshash) + if created: + file.item = i + file.path = filename + file.wanted = True + file.selected = True + extension = file.path.split('.') + if len(extension) > 1: + extension = extension[-1] + else: + extension = 'webm' + file.info['extension'] = extension.lower() + file.save() + else: + raise Exception('file exists', oshash) + return file