From 55c548c872d1a7a6e3902376aaa8dab36dd9e877 Mon Sep 17 00:00:00 2001 From: Sanj Date: Wed, 28 Mar 2012 16:20:07 +0530 Subject: [PATCH] padma videos attached to talks --- itf/festival/admin.py | 4 +- itf/festival/models.py | 3 +- itf/padmavideos/__init__.py | 0 itf/padmavideos/models.py | 85 +++++++++++++++++++++ itf/templates/modules/festival/meeting.html | 18 +++-- 5 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 itf/padmavideos/__init__.py create mode 100644 itf/padmavideos/models.py diff --git a/itf/festival/admin.py b/itf/festival/admin.py index cdf1a0c..004dcc8 100755 --- a/itf/festival/admin.py +++ b/itf/festival/admin.py @@ -30,7 +30,7 @@ class MeetingSponsorInline(admin.StackedInline): class TalkAdmin(admin.ModelAdmin): - inlines = [AudioInline, ImageInline, DocumentInline] + inlines = [PadmaVideoInline, AudioInline, ImageInline, DocumentInline] save_on_top = True list_display = ('__unicode__', 'presenter', 'session',) list_filter = ['session'] @@ -64,7 +64,7 @@ class ProjectAdmin(admin.ModelAdmin): save_on_top = True class SessionAdmin(admin.ModelAdmin): - inlines = [PadmaVideoInline] +# inlines = [PadmaVideoInline] search_fields = ('title', 'intro',) list_filter = ['day'] list_display = ('__unicode__',) diff --git a/itf/festival/models.py b/itf/festival/models.py index c22cf26..e27bf6f 100755 --- a/itf/festival/models.py +++ b/itf/festival/models.py @@ -132,6 +132,7 @@ class Talk(models.Model): intro = models.TextField(blank=True, null=True) presenter = models.CharField(max_length=255, blank=True) session = models.ForeignKey('Session') # name of meeting - name of session + videos = generic.GenericRelation(PadmaVideo) def __unicode__(self): return "%s - %s" % (self.session.day.meeting.title, self.title) # name of meeting - name of session @@ -151,7 +152,7 @@ class Session(models.Model): intro = models.TextField(blank=True, null=True) day = models.ForeignKey('MeetingDay') session_no = models.IntegerField(blank=True, null=True) - videos = generic.GenericRelation(PadmaVideo) +# videos = generic.GenericRelation(PadmaVideo) def __unicode__(self): return "%s - %s" % (self.day.meeting.title, self.title) # name of meeting - name of session diff --git a/itf/padmavideos/__init__.py b/itf/padmavideos/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/itf/padmavideos/models.py b/itf/padmavideos/models.py new file mode 100644 index 0000000..aabad5a --- /dev/null +++ b/itf/padmavideos/models.py @@ -0,0 +1,85 @@ +from django.db import models +from ox.django.fields import DictField +from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes import generic +from django.forms import ValidationError +import re +import ox +from app.models import ItfModel +from django.core.files import File +from django.core.files.temp import NamedTemporaryFile +import urllib2 + +PANDORA_BASE = 'pad.ma' + +class PadmaVideo(ItfModel): + 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) + 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 + + def get_dict(self): + return { + 'embed_code': self.embed_code, + 'url': self.url, + 'title': self.title, + 'data': self.data, + 'time_in': self.time_in, + 'time_out': self.time_out, + 'layers':self.layers + + } + + 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") + self.width, self.height, self.padma_id, self.time_in, self.time_out = match.groups() + self.time_in = int(self.time_in) + self.time_out = int(self.time_out) + 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) + self.data = api.get({ + 'id': self.padma_id, + 'keys': [] + })['data'] + if not self.title: + self.title = self.data['title'] + if self.time_in == self.time_out: + self.time_out = int(self.data['duration']) + if update_poster: + self.update_poster() + self.layers = api.get({ + 'id': self.padma_id, + 'keys': ['layers'] + }) + return self + + #ref: http://djangosnippets.org/snippets/2587/ + def update_poster(self): + poster_time = int((self.time_in + self.time_out) / 2) + 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) diff --git a/itf/templates/modules/festival/meeting.html b/itf/templates/modules/festival/meeting.html index 9331c1d..7245bfc 100755 --- a/itf/templates/modules/festival/meeting.html +++ b/itf/templates/modules/festival/meeting.html @@ -45,7 +45,7 @@ {% for s in sponsors %} {% thumbnail s.logo "120" crop="center" as thumb %} {% if s.url %} {% endif %} - {{ s.name }} + {{ s.name }}
{% if s.url %}
{% endif %} {% endthumbnail %}   @@ -70,13 +70,7 @@
{{ s.intro|markdown }}
- {% ifnotequal s.videos.all|length 0 %} -
- {% for v in s.videos.all %} - {{ v.embed_code|safe }} - {% endfor %} -
- {% endifnotequal %} + {% ifnotequal s.talks|length 0 %}