Source code for mg.core.element.models
# MG_GPL_HEADER_BEGIN
#
# This file is part of Media Gallery, GNU GPLv2.
#
# Media Gallery is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# Media Gallery is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Media Gallery; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# MG_GPL_HEADER_END
__author__ = "Media Gallery contributors <mg@lists.ssji.net>"
__copyright__ = "(c) 2010"
__credits__ = ["Dimitri Refauvelet", "Nicolas Pouillon"]
from django.contrib.auth.models import User
from django.db import models
__all__ = [
'Element', 'ThumbnailSize', 'Thumbnail'
]
class Element(models.Model):
[docs] timestamp = models.DateTimeField(db_index = True, auto_now_add = True)
name = models.CharField(max_length = 64)
description = models.TextField(blank = True, null = True)
owner = models.ForeignKey(User)
type = models.CharField(max_length = 8, db_index = True)
class Meta:
ordering = ('timestamp',)
# Handling of transparent typecasting
# Register all inheriting models with register_type.
__types = {}
@classmethod
def register_type(cls, name, klass):
klass.__element_type = name
cls.__types[name] = klass, klass._meta.object_name.lower()
@classmethod
def registry(cls):
"""
Dict of (element_type_name, class)
"""
return cls.__types
# __init__ and save do some inheritance checking
def __init__(self, *args, **kwargs):
models.Model.__init__(self, *args, **kwargs)
if self.pk is None:
if self.__class__ is Element:
raise ValueError('Should not instanciate an element')
self.type = self.__element_type
def save(self, *args, **kwargs):
if self.type not in self.__types:
raise ValueError("Unregistered element type %s"%self.type)
models.Model.save(self, *args, **kwargs)
# Methods
def base_element(self):
if self.__class__ is Element:
return self
return self.element_ptr
def actual_element(self):
[docs] if self.__class__ is not Element:
return self
klass, pname = self.__types[self.type]
return getattr(self, pname)
def thumbnail_new(self, thumb_size):
[docs] raise NotImplementedError()
def __unicode__(self):
return self.name
def thumbnail_get(self, size = None):
[docs] try:
if size is None:
return self.thumbnail_set.order_by("size__width")[0]
else:
return self.thumbnail_set.get(size = size)
except Exception, e:
return None
class ThumbnailSize(models.Model):
[docs] width = models.IntegerField(db_index = True)
height = models.IntegerField(db_index = True)
class Meta:
unique_together = (
("width", "height"),
)
def __unicode__(self):
return u"%dx%d"%(self.width, self.height)
@property
def wh(self):
[docs] """
property corresponding to (width, height) of thumbnail
"""
return self.width, self.height
class Thumbnail(models.Model):
[docs] element = models.ForeignKey(Element)
size = models.ForeignKey(ThumbnailSize)
file = models.ImageField(upload_to = "thumbnail")
def filename_gen(self):
[docs] base = u"%s_%s"%(self.element.name, self.size)
return self.file.storage.get_available_name(
self.file.storage.get_valid_name(base+".jpg"))
class Meta:
unique_together = (
('element', 'size'),
)
def __unicode__(self):
return "<%s t%s>"%(self.element, self.size)