From d9d1a600a96ce746231ba3c96fb5bf596a3b09fb Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 6 Mar 2013 14:08:42 +0000 Subject: [PATCH] add manage.py import_srt command --- pandora/annotation/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../management/commands/import_srt.py | 54 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 pandora/annotation/management/__init__.py create mode 100644 pandora/annotation/management/commands/__init__.py create mode 100644 pandora/annotation/management/commands/import_srt.py diff --git a/pandora/annotation/management/__init__.py b/pandora/annotation/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pandora/annotation/management/commands/__init__.py b/pandora/annotation/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pandora/annotation/management/commands/import_srt.py b/pandora/annotation/management/commands/import_srt.py new file mode 100644 index 000000000..e203593ed --- /dev/null +++ b/pandora/annotation/management/commands/import_srt.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 + +from django.core.management.base import BaseCommand +from django.db import transaction +from django.conf import settings + +import ox + +import monkey_patch.models + +from item.models import Item +from user.models import User + +from ... import models + +class Command(BaseCommand): + """ + import annotations + """ + help = 'import annotations from srt' + args = 'username itemId layername filename.srt' + option_list = BaseCommand.option_list + ( + ) + + def handle(self, *args, **options): + if len(args) != 4: + print self.usage('import_srt') + return + username, itemId, layer_id, filename = args + user = User.objects.get(username=username) + item = Item.objects.get(itemId=itemId) + layer = filter(lambda l: l['id'] == layer_id, settings.CONFIG['layers'])[0] + + annotations = ox.srt.load(filename) + print 'importing %d annotations into %s/%s' % (len(annotations), itemId, layer_id) + with transaction.commit_on_success(): + for a in annotations: + if a['value']: + annotation = models.Annotation( + item=item, + layer=layer_id, + user=user, + start=float(a['in']), end=float(a['out']), + value=a['value']) + annotation.save() + #update facets if needed + if filter(lambda f: f['id'] == layer_id, settings.CONFIG['filters']): + item.update_layer_facet(layer_id) + Item.objects.filter(id=item.id).update(modified=annotation.modified) + annotation.item.modified = annotation.modified + annotation.item.update_find() + annotation.item.update_sort() + annotation.item.update_facets()