From 4884ae0503e67d0b268ffe1d4ab3e00acc424528 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 20 Oct 2011 18:15:59 +0200 Subject: [PATCH] clips --- pandora/annotation/models.py | 8 +------- pandora/clip/models.py | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 90573746..0695f2aa 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/clip/models.py b/pandora/clip/models.py index 651982b1..41f1430b 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') @@ -90,6 +91,19 @@ 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): + 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) + clips.save() + created = True + else: + clip = qs[0] + created = False + return clip, created + + def __unicode__(self): + return self.public_id