2013-03-06 14:08:42 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-02-18 10:49:26 +00:00
|
|
|
from __future__ import print_function
|
2013-03-06 14:08:42 +00:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.db import transaction
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
import ox
|
|
|
|
|
2013-05-18 21:43:52 +00:00
|
|
|
import app.monkey_patch
|
2013-03-06 14:08:42 +00:00
|
|
|
|
|
|
|
from item.models import Item
|
|
|
|
from user.models import User
|
|
|
|
|
|
|
|
from ... import models
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
"""
|
|
|
|
import annotations
|
|
|
|
"""
|
2016-07-01 16:10:01 +00:00
|
|
|
help = 'import annotations from srt or vtt'
|
2014-09-19 12:26:46 +00:00
|
|
|
args = 'username item layername filename.srt'
|
2013-03-06 14:08:42 +00:00
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
if len(args) != 4:
|
2016-02-18 10:49:26 +00:00
|
|
|
print(self.usage('import_srt'))
|
2013-03-06 14:08:42 +00:00
|
|
|
return
|
2014-09-19 12:26:46 +00:00
|
|
|
username, public_id, layer_id, filename = args
|
2013-03-06 14:08:42 +00:00
|
|
|
user = User.objects.get(username=username)
|
2014-09-19 12:26:46 +00:00
|
|
|
item = Item.objects.get(public_id=public_id)
|
2017-02-16 13:24:51 +00:00
|
|
|
layer = list(filter(lambda l: l['id'] == layer_id, settings.CONFIG['layers']))[0]
|
2013-03-06 14:08:42 +00:00
|
|
|
|
2016-07-01 16:10:01 +00:00
|
|
|
if filename.endswith('.vtt'):
|
|
|
|
annotations = ox.vtt.load(filename)
|
|
|
|
else:
|
|
|
|
annotations = ox.srt.load(filename)
|
2016-02-18 10:49:26 +00:00
|
|
|
print('importing %d annotations into %s/%s' % (len(annotations), public_id, layer_id))
|
2013-03-06 14:19:13 +00:00
|
|
|
for i in range(len(annotations)-1):
|
|
|
|
if annotations[i]['out'] == annotations[i+1]['in']:
|
|
|
|
annotations[i]['out'] = annotations[i]['out'] - 0.001
|
2016-02-19 16:25:09 +00:00
|
|
|
with transaction.atomic():
|
2013-03-06 14:08:42 +00:00
|
|
|
for a in annotations:
|
|
|
|
if a['value']:
|
|
|
|
annotation = models.Annotation(
|
|
|
|
item=item,
|
|
|
|
layer=layer_id,
|
|
|
|
user=user,
|
2013-03-06 14:19:13 +00:00
|
|
|
start=float(a['in']),
|
|
|
|
end=float(a['out']),
|
2013-03-06 14:08:42 +00:00
|
|
|
value=a['value'])
|
|
|
|
annotation.save()
|
|
|
|
#update facets if needed
|
2017-02-16 13:24:51 +00:00
|
|
|
if list(filter(lambda f: f['id'] == layer_id, settings.CONFIG['filters'])):
|
2013-03-06 14:08:42 +00:00
|
|
|
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()
|