get_or_create clip

This commit is contained in:
j 2011-10-20 21:32:31 +02:00
parent 95ce013aa4
commit a546d07b73
4 changed files with 39 additions and 20 deletions

View file

@ -118,13 +118,7 @@ class Annotation(models.Model):
if not self.clip and not self.layer.private or \
(self.clip and not self.layer.private and \
self.start != self.clip.start or self.end != self.clip.end):
self.clip, created = Clip.objects.get_or_create(item=self.item,
start=self.start,
end=self.end)
if created:
clip = Clip.objects.get(pk=self.clip.pk)
clip.save()
self.clip, created = Clip.get_or_create(self.item, self.start, self.end)
super(Annotation, self).save(*args, **kwargs)

View file

@ -135,8 +135,9 @@ class File(models.Model):
self.is_subtitle = False
self.type = self.get_type()
info = ox.parse_movie_path(self.path)
self.language = info['language']
if self.instances.count()>0:
info = ox.parse_movie_path(self.path)
self.language = info['language']
self.part = self.get_part()
if self.type not in ('audio', 'video'):

View file

@ -3,16 +3,18 @@
from celery.decorators import task
import ox
from item.models import get_item
from django.conf import settings
from item.models import get_item
import item.tasks
import models
_INSTANCE_KEYS = ('mtime', 'path')
def get_or_create_item(volume, info, user):
item_info = ox.parse_movie_info(info['path'])
item_info = ox.parse_movie_path(info['path'])
return get_item(item_info, user)
def get_or_create_file(volume, f, user, item=None):
@ -67,17 +69,23 @@ def update_files(user, volume, files):
user = models.User.objects.get(username=user)
volume, created = models.Volume.objects.get_or_create(user=user, name=volume)
all_files = []
#ignore extras etc,
#imdb stlye is L/Last, First/Title (Year)/Title.. 4
#otherwise T/Title (Year)/Title... 3
folder_depth = settings.USE_IMDB and 4 or 3
for f in files:
#ignore extras etc,
#imdb stlye is L/Last, First/Title (Year)/Title.. 4
#otherwise T/Title (Year)/Title... 3
folder_depth = settings.USE_IMDB and 4 or 3
if len(f['path'].split('/')) == folder_depth:
all_files.append(f['oshash'])
update_or_create_instance(volume, f)
#remove deleted files
models.Instance.objects.filter(volume=volume).exclude(file__oshash__in=all_files).delete()
removed = models.Instance.objects.filter(volume=volume).exclude(file__oshash__in=all_files)
ids = [i['itemId'] for i in Item.models.objects.filter(
files__instances__in=removed.filter(selected=True)).distinct().values('itemId')]
removed.delete()
for i in ids:
i = Item.models.objects.get(itemId=i)
i.update_selected()
@task(queue="encoding")
def process_stream(fileId):

View file

@ -8,6 +8,7 @@ from django.conf import settings
from archive import extract
import managers
class Clip(models.Model):
'''
CREATE INDEX clip_clip_title_idx ON clip_clip (title ASC NULLS LAST);
@ -20,7 +21,7 @@ class Clip(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
public_id = models.CharField(max_length=128, unique=True, null=True)
public_id = models.CharField(max_length=128, unique=True)
item = models.ForeignKey('item.Item', related_name='clips')
@ -52,7 +53,7 @@ class Clip(models.Model):
self.title = self.item.sort.title
def save(self, *args, **kwargs):
self.public_id = u"%s/%s-%s" %(self.item.itemId, self.start, self.end)
self.public_id = u"%s/%s-%s" %(self.item.itemId, float(self.start), float(self.end))
if self.duration != self.end - self.start:
self.update_calculated_values()
super(Clip, self).save(*args, **kwargs)
@ -90,6 +91,21 @@ class Clip(models.Model):
j[key] = self.item.get(key)
return j
def __unicode__(self):
return u"%s/%s-%s" %(self.item, self.start, self.end)
@classmethod
def get_or_create(cls, item, start, end):
start = float(start)
end = float(end)
public_id = u"%s/%s-%s" %(item.itemId, start, end)
qs = cls.objects.filter(public_id=public_id)
if qs.count() == 0:
clip = Clip(item=item, start=start, end=end, public_id=public_id)
clip.save()
created = True
else:
clip = qs[0]
created = False
return clip, created
def __unicode__(self):
return self.public_id