use some transactions

This commit is contained in:
j 2011-10-29 16:59:12 +02:00
parent 88b4d9f62f
commit 21ab5fa451

View file

@ -13,7 +13,7 @@ import uuid
import unicodedata import unicodedata
from urllib import quote from urllib import quote
from django.db import models from django.db import models, transaction
from django.db.models import Count, Q, Sum from django.db.models import Count, Q, Sum
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.conf import settings from django.conf import settings
@ -518,45 +518,46 @@ class Item(models.Model):
else: else:
ItemFind.objects.filter(item=self, key=key).delete() ItemFind.objects.filter(item=self, key=key).delete()
for key in settings.CONFIG['itemKeys']: with transaction.commit_on_success():
i = key['id'] for key in settings.CONFIG['itemKeys']:
if i == 'title': i = key['id']
save(i, u'\n'.join([self.get('title', 'Untitled'), if i == 'title':
self.get('originalTitle', '')])) save(i, u'\n'.join([self.get('title', 'Untitled'),
elif i == 'rightslevel': self.get('originalTitle', '')]))
save(i, self.level) elif i == 'rightslevel':
elif i == 'filename': save(i, self.level)
save(i, elif i == 'filename':
'\n'.join([f.path for f in self.files.all()])) save(i,
elif key['type'] == 'layer': '\n'.join([f.path for f in self.files.all()]))
qs = Annotation.objects.filter(layer__name=i, item=self).order_by('start') elif key['type'] == 'layer':
save(i, '\n'.join([l.value for l in qs])) qs = Annotation.objects.filter(layer__name=i, item=self).order_by('start')
elif i != '*' and i not in self.facet_keys: save(i, '\n'.join([l.value for l in qs]))
value = self.get(i) elif i != '*' and i not in self.facet_keys:
if isinstance(value, list): value = self.get(i)
value = u'\n'.join(value) if isinstance(value, list):
save(i, value) value = u'\n'.join(value)
save(i, value)
for key in self.facet_keys: for key in self.facet_keys:
if key == 'character': if key == 'character':
values = self.get('cast', '') values = self.get('cast', '')
if values: if values:
values = filter(lambda x: x.strip(), values = filter(lambda x: x.strip(),
[f['character'] for f in values]) [f['character'] for f in values])
values = list(set(values))
elif key == 'name':
values = []
for k in map(lambda x: x['id'],
filter(lambda x: x.get('sort') == 'person',
settings.CONFIG['itemKeys'])):
values += self.get(k, [])
values = list(set(values)) values = list(set(values))
elif key == 'name': else:
values = [] values = self.get(key, '')
for k in map(lambda x: x['id'], if isinstance(values, list):
filter(lambda x: x.get('sort') == 'person', save(key, '\n'.join(values))
settings.CONFIG['itemKeys'])): else:
values += self.get(k, []) save(key, values)
values = list(set(values))
else:
values = self.get(key, '')
if isinstance(values, list):
save(key, '\n'.join(values))
else:
save(key, values)
def update_sort(self): def update_sort(self):
try: try:
@ -1043,62 +1044,63 @@ class Item(models.Model):
return icon return icon
def load_subtitles(self): def load_subtitles(self):
layer = Layer.objects.get(name='subtitles') with transaction.commit_on_success():
Annotation.objects.filter(layer=layer,item=self).delete() layer = Layer.objects.get(name='subtitles')
offset = 0 Annotation.objects.filter(layer=layer,item=self).delete()
language = '' offset = 0
subtitles = self.files.filter(selected=True, is_subtitle=True, available=True) language = ''
languages = [f.language for f in subtitles] subtitles = self.files.filter(selected=True, is_subtitle=True, available=True)
if languages: languages = [f.language for f in subtitles]
if 'en' in languages: if languages:
language = 'en' if 'en' in languages:
elif '' in languages: language = 'en'
language = '' elif '' in languages:
else: language = ''
language = languages[0] else:
language = languages[0]
#loop over all videos #loop over all videos
for f in self.files.filter(Q(is_audio=True)|Q(is_video=True)) \ for f in self.files.filter(Q(is_audio=True)|Q(is_video=True)) \
.filter(selected=True).order_by('part'): .filter(selected=True).order_by('part'):
subtitles_added = False subtitles_added = False
prefix = os.path.splitext(f.path)[0] prefix = os.path.splitext(f.path)[0]
if f.instances.all().count() > 0: if f.instances.all().count() > 0:
user = f.instances.all()[0].volume.user user = f.instances.all()[0].volume.user
else: else:
#FIXME: allow annotations from no user instead? #FIXME: allow annotations from no user instead?
user = User.objects.all().order_by('id')[0] user = User.objects.all().order_by('id')[0]
#if there is a subtitle with the same prefix, import #if there is a subtitle with the same prefix, import
q = subtitles.filter(path__startswith=prefix, q = subtitles.filter(path__startswith=prefix,
language=language) language=language)
if q.count() == 1: if q.count() == 1:
s = q[0] s = q[0]
for data in s.srt(offset): for data in s.srt(offset):
subtitles_added = True subtitles_added = True
annotation = Annotation( annotation = Annotation(
item=self, item=self,
layer=layer, layer=layer,
start=data['in'], start=data['in'],
end=data['out'], end=data['out'],
value=data['value'], value=data['value'],
user=user user=user
) )
annotation.save() annotation.save()
#otherwise add empty 5 seconds annotation every minute #otherwise add empty 5 seconds annotation every minute
if not subtitles_added: if not subtitles_added:
start = offset and int (offset / 60) * 60 + 60 or 0 start = offset and int (offset / 60) * 60 + 60 or 0
for i in range(start, for i in range(start,
int(offset + f.duration) - 5, int(offset + f.duration) - 5,
60): 60):
annotation = Annotation( annotation = Annotation(
item=self, item=self,
layer=layer, layer=layer,
start=i, start=i,
end=i + 5, end=i + 5,
value='', value='',
user=user user=user
) )
annotation.save() annotation.save()
offset += f.duration offset += f.duration
self.update_find() self.update_find()
def delete_item(sender, **kwargs): def delete_item(sender, **kwargs):