Source

mediatr / models.py

Full commit
from django.contrib.auth.models import User
from django.db import models
from django.conf import settings
from django.template.defaultfilters import slugify
from django.db.models import permalink

from signals import add_mime_type, make_slug
import filters


class Stream(models.Model):
    """
    Stream or feed your uploads to public
    """
    name = models.CharField(max_length=250)
    slug = models.SlugField()
    description = models.TextField(blank=True)
    author = models.ForeignKey(User)
    copyright = models.CharField(blank=True, max_length=100)
    icon = models.ImageField(upload_to="stream-icon")
    tags = models.CharField(blank=True, max_length=200)
    
    def __unicode__(self):
        return self.name

    @permalink
    def get_absolute_url(self):
        return ('stream_page', (), {'slug': self.slug})

class MimeType(models.Model):
    """
    Mime Types table
    """
    name = models.CharField(max_length=200)
    slug = models.SlugField()

    def __unicode__(self):
        return self.name

class Gallery(models.Model):
    """
    Media gallery for internal resource categorization.
    """
    name = models.CharField(max_length=200)
    slug = models.SlugField()
    description = models.TextField(blank=True)

    def __unicode__(self):
        return self.name

class Batch(models.Model):
    """
    Batch upload tracking. For mass upload/edit capabilities.
    """
    ident = models.CharField(max_length=200)
    uploaded = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.ident


class Item(models.Model):
    """
    Main file
    """
    user = models.ForeignKey(User)
    name = models.CharField(max_length=200, blank=True)
    slug = models.SlugField(blank=True)
    text = models.TextField(blank=True)
    file = models.FileField(upload_to='files', blank=True)
    mime = models.ForeignKey(MimeType, blank=True, null=True)
    batch = models.ForeignKey(Batch, blank=True, null=True)
    gallery = models.ForeignKey(Gallery, blank=True, null=True)
    stream = models.ManyToManyField(Stream, blank=True, related_name='items')
    upload_date = models.DateTimeField(auto_now_add=True)
    publish_date = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        if self.name:
            return self.name
        else:
            return "Item: %s" % (self.slug)
    
    def set_mime(self, mime):
        obj, created = MimeType.objects.get_or_create(name=mime, slug=slugify(mime))
        self.mime = obj
    
    def is_image(self):
        if self.mime.name in ['image/png', 'image/jpeg', 'image/gif']:
            return True

    @permalink
    def get_absolute_url(self):
        return ('stream_item', (), {'slug': self.slug})

class Filter(models.Model):
    """
    File filter for creating variants, such as thumbnails, flv, etc
    """
    name = models.CharField(max_length=200)
    cmd = models.CharField(max_length=100)
    params = models.TextField(blank=True)
    template = models.TextField(default="<a href={{ object.get_absolute_url }}>{{ object.name }}</a>")
    default = models.BooleanField(default=False)

    class Admin:
        list_display = ('name',)
        search_fields = ('name',)

    def __unicode__(self):
        return self.name

class Variant(models.Model):
    """File item variant"""
    name = models.CharField(max_length=200)
    parent = models.ForeignKey(Item)
    file = models.FileField(upload_to='variants')
    mime = models.ForeignKey(MimeType)
    filter = models.ForeignKey(Filter)
    default = models.BooleanField(default=False)
    
    class Meta:
        unique_together = (("filter", "file"),)

    class Admin:
        list_display = ('name',)
        search_fields = ('name',)

    def __unicode__(self):
        return self.name

# class MetaField(models.Model):
#     """
#     MetaField aka meta type
#     """
#     name = models.CharField(max_length=150)
# 
#     def __unicode__(self):
#         return self.name

class Meta(models.Model):
    """
    File meta info, like exif data for jpg or id3 tags for mp3
    """
    item = models.ForeignKey(Item)
    field = models.CharField(max_length=250)
    value = models.CharField(max_length=250)

    class Meta:
        unique_together = (("item", "field", "value"),)

    def __unicode__(self):
        return u"%s: %s" % (self.field, self.value)

class Queue(models.Model):
    """
    Queue for creating variants from file via filter
    """
    file = models.ForeignKey(Item)
    filter = models.ForeignKey(Filter)
    date_added = models.DateTimeField(auto_now_add=True)
    date_done = models.DateTimeField(blank=True)
    done = models.BooleanField(default=False)
    log = models.TextField(blank=True)
    variant = models.ForeignKey(Variant, blank=True, null=True)

    class Admin:
        list_display = ('file', 'filter', 'date_added', 'done')

    def __unicode__(self):
        return self.file

models.signals.pre_save.connect(add_mime_type, sender=Item)
models.signals.pre_save.connect(make_slug, sender=Item)
models.signals.post_save.connect(filters.run, sender=Item)