summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParth Buch2012-06-22 19:14:49 +0530
committerParth Buch2012-06-22 19:14:49 +0530
commitcc4f9c1254d0358f6a872be8d41d52587640a921 (patch)
tree553fa63f207fe491c672ca8bc4c89d0e4efb8ddf
parent1a88222e5fd611185c5283c5799e6cb3463628fd (diff)
downloadstproject-cc4f9c1254d0358f6a872be8d41d52587640a921.tar.gz
stproject-cc4f9c1254d0358f6a872be8d41d52587640a921.tar.bz2
stproject-cc4f9c1254d0358f6a872be8d41d52587640a921.zip
Added an image and thumbnail field which is to be loaded in HTML5 poster. The 100x100 thumbnails are automatically generated from the image field.
-rw-r--r--stapp/templates/video/home.html4
-rw-r--r--stapp/video/migrations/0006_auto__add_field_video_image__add_field_video_thumbnail.py52
-rw-r--r--stapp/video/models.py39
3 files changed, 91 insertions, 4 deletions
diff --git a/stapp/templates/video/home.html b/stapp/templates/video/home.html
index ba84628..9a55eb0 100644
--- a/stapp/templates/video/home.html
+++ b/stapp/templates/video/home.html
@@ -8,7 +8,7 @@
<div class="hero-unit">
<h3>Latest Video</h3>
- <video width="700" height="400" controls="controls" id="mainvideo">
+ <video width="700" height="400" controls="controls" id="mainvideo" poster="{{play.thumbnail.url}}">
<source src="{{play.filename.url}}">
</video>
<div id="footnote"></div>
@@ -20,7 +20,7 @@
<h2>{{module.name}}</h2>
{% for video in module.video_list.all %}
<video width="150" height="150" controls="controls">
- <source src="{{video.filename.url}}">
+ <source src="{{video.filename.url}}" poster="{{video.thumbnail.url}}">
</video>
{{video.name}}
{% endfor %}
diff --git a/stapp/video/migrations/0006_auto__add_field_video_image__add_field_video_thumbnail.py b/stapp/video/migrations/0006_auto__add_field_video_image__add_field_video_thumbnail.py
new file mode 100644
index 0000000..13cb57b
--- /dev/null
+++ b/stapp/video/migrations/0006_auto__add_field_video_image__add_field_video_thumbnail.py
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Adding field 'Video.image'
+ db.add_column('video_video', 'image',
+ self.gf('django.db.models.fields.files.ImageField')(default='/media/screenshots/demo.jpg', max_length=100),
+ keep_default=False)
+
+ # Adding field 'Video.thumbnail'
+ db.add_column('video_video', 'thumbnail',
+ self.gf('django.db.models.fields.files.ImageField')(max_length=100, null=True, blank=True),
+ keep_default=False)
+
+
+ def backwards(self, orm):
+ # Deleting field 'Video.image'
+ db.delete_column('video_video', 'image')
+
+ # Deleting field 'Video.thumbnail'
+ db.delete_column('video_video', 'thumbnail')
+
+
+ models = {
+ 'video.module': {
+ 'Meta': {'object_name': 'Module'},
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'description': ('django.db.models.fields.TextField', [], {}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'modified': ('django.db.models.fields.DateTimeField', [], {}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'video_list': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['video.Video']", 'symmetrical': 'False'})
+ },
+ 'video.video': {
+ 'Meta': {'object_name': 'Video'},
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'description': ('django.db.models.fields.TextField', [], {}),
+ 'filename': ('django.db.models.fields.files.FileField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'image': ('django.db.models.fields.files.ImageField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'thumbnail': ('django.db.models.fields.files.ImageField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'})
+ }
+ }
+
+ complete_apps = ['video'] \ No newline at end of file
diff --git a/stapp/video/models.py b/stapp/video/models.py
index 56cb5fe..181299c 100644
--- a/stapp/video/models.py
+++ b/stapp/video/models.py
@@ -1,17 +1,52 @@
from django.db import models
-from django.contrib.auth.models import User
+import settings
+
+
import datetime
+import Image, os
+
+def handle_thumb(image_obj, thumb_obj, width, height):
+ # create thumbnail
+ if image_obj and not thumb_obj:
+ thumb = image_obj.path + ('-t%sx%s.jpg' % (width, height))
+ #try:
+ t = Image.open(image_obj.path)
+
+ w, h = t.size
+ if float(w)/h < float(width)/height:
+ t = t.resize((width, h*width/w), Image.ANTIALIAS)
+ else:
+ t = t.resize((w*height/h, height), Image.ANTIALIAS)
+ w, h = t.size
+ t = t.crop( ((w-width)/2, (h-height)/4, (w-width)/2+width, (h-height)/4+height) )
+
+ t.save(settings.MEDIA_ROOT + thumb, 'JPEG')
+ os.chmod(settings.MEDIA_ROOT + thumb, 0666)
+ thumb_obj = image_obj.url + ('-t%sx%s.jpg' % (width, height))
+ #except:
+ # pass
+ return thumb_obj
+
class Video(models.Model):
"""Videos to be uploaded.."""
name = models.CharField(max_length=128)
- filename = models.FileField(upload_to="videos")
+ filename = models.FileField(upload_to="uploads/videos")
+ image = models.ImageField(upload_to="uploads/screenshots")
+ thumbnail = models.ImageField(upload_to='uploads/screenshots-thumbs', blank=True, null=True, editable=False)
description = models.TextField()
created = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.name
+ def save(self, *args, **kwargs):
+ '''On save, generate thumbnails'''
+ self.thumbnail = handle_thumb(self.image, self.thumbnail, 100, 100)
+ super(Video, self).save()
+
+
+
class Module(models.Model):
name = models.CharField(max_length=128)
description = models.TextField()