From 9e3a4aa979de9daa9eade49f988a129dc9d4fccc Mon Sep 17 00:00:00 2001 From: Sanj Date: Mon, 9 Apr 2012 03:56:35 +0530 Subject: [PATCH] PadmaClip and PadmaVideo as two separate classes --- itf/festival/admin.py | 4 +-- itf/itfcore/models.py | 7 ++++++ itf/padmavideos/models.py | 52 ++++++++++++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/itf/festival/admin.py b/itf/festival/admin.py index 004dcc8..95b9dbb 100755 --- a/itf/festival/admin.py +++ b/itf/festival/admin.py @@ -2,7 +2,7 @@ from django.contrib import admin from models import * from markitup.widgets import MarkItUpWidget from django.contrib.contenttypes import generic -from padmavideos.models import PadmaVideo +from padmavideos.models import PadmaVideo, PadmaClip class AudioInline(admin.StackedInline): model = Audio @@ -13,7 +13,7 @@ class ImageInline(admin.StackedInline): extra = 3 class PadmaVideoInline(generic.GenericStackedInline): - model = PadmaVideo + model = PadmaClip extra = 3 class DocumentInline(admin.StackedInline): diff --git a/itf/itfcore/models.py b/itf/itfcore/models.py index ca29591..3b14714 100755 --- a/itf/itfcore/models.py +++ b/itf/itfcore/models.py @@ -126,7 +126,14 @@ class Training(models.Model): until_when = models.DateField(blank=True, null=True) +class Play(models.Model): + pass +# title +# author + + class Production(models.Model): + play = models.ForeignKey("Play", null=True, blank=True) name = models.CharField(max_length=255, db_index=True) language = models.ForeignKey("Language", blank=True, null=True) group = models.ForeignKey("TheatreGroup") diff --git a/itf/padmavideos/models.py b/itf/padmavideos/models.py index aabad5a..9650f31 100644 --- a/itf/padmavideos/models.py +++ b/itf/padmavideos/models.py @@ -9,24 +9,21 @@ from app.models import ItfModel from django.core.files import File from django.core.files.temp import NamedTemporaryFile import urllib2 +from decimal import Decimal PANDORA_BASE = 'pad.ma' class PadmaVideo(ItfModel): - embed_code = models.TextField() - url = models.CharField(max_length=512, editable=False) +# embed_code = models.TextField() +# url = models.CharField(max_length=512, editable=False) title = models.CharField(max_length=512, blank=True) poster = models.ImageField(blank=True, upload_to='upload/padmaposters/') - padma_id = models.CharField(editable=False, max_length=8, db_index=True) - time_in = models.IntegerField(null=True, editable=False) - time_out = models.IntegerField(null=True, editable=False) + padma_id = models.CharField(editable=False, max_length=16, unique=True, db_index=True) + duration = models.DecimalField(max_digits=16, decimal_places=3, editable=False) width = models.IntegerField(null=True, editable=False) height = models.IntegerField(null=True, editable=False) data = DictField(default={}, editable=False) layers = DictField(default={}, editable=False) - content_type = models.ForeignKey(ContentType) - object_id = models.PositiveIntegerField() - content_object = generic.GenericForeignKey('content_type', 'object_id') def __unicode__(self): return self.padma_id @@ -43,6 +40,7 @@ class PadmaVideo(ItfModel): } + ''' def save(self, *args, **kwargs): embed_code = self.embed_code regex = r'\' @@ -55,7 +53,7 @@ class PadmaVideo(ItfModel): self.url = "http://%s/%s/" % (PANDORA_BASE, self.padma_id,) #FIXME time_in, time_out self.get_padma_data() super(PadmaVideo, self).save(*args, **kwargs) - + ''' def get_padma_data(self, update_poster=True): api = ox.api.API("http://%s/api" % PANDORA_BASE) @@ -65,8 +63,7 @@ class PadmaVideo(ItfModel): })['data'] if not self.title: self.title = self.data['title'] - if self.time_in == self.time_out: - self.time_out = int(self.data['duration']) + self.duration = Decimal(str(self.data['duration'])) if update_poster: self.update_poster() self.layers = api.get({ @@ -77,9 +74,40 @@ class PadmaVideo(ItfModel): #ref: http://djangosnippets.org/snippets/2587/ def update_poster(self): - poster_time = int((self.time_in + self.time_out) / 2) + #poster_time = int((self.time_in + self.time_out) / 2) + poster_time = self.data['posterFrame'] url = 'http://%s/%s/480p%d.jpg' % (PANDORA_BASE, self.padma_id, poster_time,) img_temp = NamedTemporaryFile(delete=True) img_temp.write(urllib2.urlopen(url).read()) img_temp.flush() self.poster.save("%s.jpg" % self.padma_id, File(img_temp), save=False) + + +class PadmaClip(ItfModel): + padmavideo = models.ForeignKey(PadmaVideo, editable=False) + embed_code = models.TextField() + time_in = models.DecimalField(max_digits=16, decimal_places=3, editable=False) + time_out = models.DecimalField(max_digits=16, decimal_places=3, editable=False) + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + content_object = generic.GenericForeignKey('content_type', 'object_id') + + def save(self, *args, **kwargs): + embed_code = self.embed_code + regex = r'\' + match = re.match(regex, embed_code) + if not match: + raise ValidationError("Invalid Embed Code") + width, height, padma_id, time_in, time_out = match.groups() + self.time_in = Decimal(time_in) + self.time_out = Decimal(time_out) + try: + padmaVideo = PadmaVideo.objects.get(padma_id=padma_id) + created = True + except PadmaVideo.DoesNotExist: + padmaVideo = PadmaVideo(padma_id=padma_id) + created = False + padmaVideo.get_padma_data(update_poster=created) #update poster only if video is freshly created + padmaVideo.save() + self.padmavideo = padmaVideo + super(PadmaClip, self).save(*args, **kwargs) \ No newline at end of file