1. Jay Baird
  2. Ochs


Ochs / ochs_media / models.py

from django.db import models
from django.template import Context, loader

from cms.models import CMSPlugin
from cms.models import Page

from news.models import Byline, Story
from ochs_filebrowser.fields import FileBrowseField
from lib import scrapers

    ('youtube', 'Video - YouTube'),
    ('vimeo', 'Video - Vimeo'),
    ('photo', 'Photo'),
    ('audio', 'Audio'),

class MediaEmbed(models.Model):
    title = models.CharField(max_length=255)
    caption = models.TextField(blank=True, null=True)
    media_type = models.CharField(max_length=25, choices=EMBED_TYPES)
    file = FileBrowseField(max_length=255, help_text="Enter a URL to the media you wish to embed, or click the file browser icon to browse available files on the server")
    width = models.PositiveIntegerField(blank=True, null=True, help_text="Applies only to Photos. Photo will be resized to this width")
    height = models.PositiveIntegerField(blank=True, null=True, help_text="Applies only to Photos. Photo will be resized")
    embed_object = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return "%s (%s)" % (self.title, self.get_media_type_display())

    class Meta:
        verbose_name = "Media"
        verbose_name_plural = "Embedded Media" 

    def save(self):
        super(MediaEmbed, self).save()
        if self.media_type == 'audio':
            self.embed_object = '<script type="text/javascript">AudioPlayer.embed("audio_player%d", {soundFile:"%s", titles: "%s", artists: "%s", autostart: "no"});</script>' % (self.id, self.file.original, self.title, "")
        elif self.media_type in ('vimeo', 'youtube'):
            s = scrapers.make_scraper(self.file.original)
            if s.media_object():
                self.embed_object = s.media_embed(s.video_id).content
        elif self.media_type == 'photo':
            self.embed_object = '<img src="%s" title="%s" alt="%s"/>' % (self.file.original, self.title, self.title)
        super(MediaEmbed, self).save()

class Gallery(models.Model):
    title = models.CharField(max_length=255)
    picture = models.ForeignKey(MediaEmbed)
    embed_code = models.TextField()

    def __unicode__(self):
        num_pictures = len(self.picture_set.all())
        return "Gallery: %s (%d picture%s)" % (self.title, num_pictures, 's' if num_pictures != 1 else '')

    def save(self):
        super(Gallery, self).save()
        t = loader.get_template('gallery.html')
        c = Context({
            'gallery_id': self.id,
            'pictures': self.picture_set.all(),
        embed_code = t.render(c)
        super(Gallery, self).save()

class GalleryPlugin(CMSPlugin):
    gallery = models.ForeignKey(Gallery)
    def __unicode__(self):
        return unicode(self.gallery)

class MainStoryPlugin(CMSPlugin):
    photo = models.ForeignKey(MediaEmbed, blank=True, null=True)
    story = models.ForeignKey(Story)