Permalink
Browse files

Rewrite of models, flattening everything into a single media class.

  • Loading branch information...
1 parent 2b39122 commit 8a0c798ab4ce13d82e2def2a0a5f685a18b2ec51 @lukeman lukeman committed Jan 13, 2011
Showing with 34 additions and 61 deletions.
  1. +3 −7 mimesis/admin.py
  2. +3 −3 mimesis/managers.py
  3. +28 −51 mimesis/models.py
View
@@ -1,10 +1,6 @@
from django.contrib import admin
-from mimesis.models import Image, ImageAssociation
-from mimesis.models import Audio, AudioAssociation
+from mimesis.models import FileUpload, FileAssociation
-admin.site.register(Image)
-admin.site.register(ImageAssociation)
-
-admin.site.register(Audio)
-admin.site.register(AudioAssociation)
+admin.site.register(FileUpload)
+admin.site.register(FileAssociation)
View
@@ -2,12 +2,12 @@
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import force_unicode
-class MediaAssociationManager(models.Manager):
+class FileAssociationManager(models.Manager):
def for_model(self, model, content_type=None):
"""
- QuerySet for all media for a particular model (either an instance or
- a class).
+ QuerySet returning all media for a particular model (either an
+ instance or a class).
"""
ct = content_type or ContentType.objects.get_for_model(model)
qs = self.get_query_set().filter(content_type=ct)
View
@@ -1,4 +1,5 @@
import datetime
+import mimetypes
from django.db import models
@@ -10,70 +11,46 @@
from taggit.managers import TaggableManager
-class MediaBase(models.Model):
+class FileUpload(models.Model):
title = models.CharField(max_length=150)
+ description = models.TextField()
+ file = models.FileField(upload_to="mimesis")
creator = models.ForeignKey(User)
created = models.DateTimeField(default=datetime.datetime.now)
+ type = models.CharField(editable=False, max_length=100)
+ subtype = models.CharField(editable=False, max_length=100)
tags = TaggableManager()
- class Meta:
- abstract = True
-
def __unicode__(self):
return self.title
+
+ @property
+ def mime_type(self):
+ return "%s/%s" % (self.type, self.subtype)
+
+ def save(self, *args, **kwargs):
+ (mime_type, encoding) = mimetypes.guess_type(self.upload.path)
+ try:
+ mime = mime_type.split("/")
+ self.type = mime[0]
+ self.subtype = mime[1]
+ except:
+ # Mime type unknown, use text/plain
+ self.type = "text"
+ self.sub_type = "plain"
+ super(UploadedFile, self).save()
-class MediaAssociationBase(models.Model):
+class FileAssociation(models.Model):
+ """
+ A generic association of a FileUpload object and any other Django model.
+ """
content_type = models.ForeignKey(ContentType)
object_pk = models.PositiveIntegerField()
content_object = generic.GenericForeignKey("content_type", "object_pk")
+ description = models.TextField()
- caption = models.TextField()
-
- objects = MediaAssociationManager()
-
- class Meta:
- abstract = True
-
-
-class Image(MediaBase):
-
- image = models.ImageField(upload_to="mimesis/image/")
-
-
-class ImageAssociation(MediaAssociationBase):
-
- image = models.ForeignKey(Image)
-
- def get_absolute_url(self):
- return self.image.url
-
-
-class Audio(MediaBase):
-
- audio = models.FileField(upload_to="mimesis/audio/")
-
-
-class AudioAssociation(MediaAssociationBase):
-
- audio = models.ForeignKey(Audio)
-
- def get_absolute_url(self):
- return self.audio.url
-
-
-class Video(MediaBase):
-
- video = models.URLField(blank=True)
-
-
-class VideoAssociation(MediaAssociationBase):
-
- video = models.ForeignKey(Video)
-
- def get_absolute_url(self):
- return self.video
-
+ objects = FileAssociationManager()

0 comments on commit 8a0c798

Please sign in to comment.