From a546d07b7346fb8898c6277c8dabbbb5e6d74e90 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 20 Oct 2011 21:32:31 +0200 Subject: [PATCH] get_or_create clip --- pandora/annotation/models.py | 8 +------- pandora/archive/models.py | 5 +++-- pandora/archive/tasks.py | 22 +++++++++++++++------- pandora/clip/models.py | 24 ++++++++++++++++++++---- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 90573746c..0695f2aa0 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -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) diff --git a/pandora/archive/models.py b/pandora/archive/models.py index f6199e8e9..1b951b5bd 100644 --- a/pandora/archive/models.py +++ b/pandora/archive/models.py @@ -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'): diff --git a/pandora/archive/tasks.py b/pandora/archive/tasks.py index f6b53cb11..89b352c79 100644 --- a/pandora/archive/tasks.py +++ b/pandora/archive/tasks.py @@ -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): diff --git a/pandora/clip/models.py b/pandora/clip/models.py index 651982b1b..f54443568 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -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