django-imagekit/imagekit/models.py

78 lines
2.2 KiB
Python
Raw Normal View History

2008-12-31 18:40:51 +00:00
import os
2008-12-28 21:48:21 +00:00
from datetime import datetime
from django.conf import settings
2009-01-08 20:04:20 +00:00
from django.core.files.base import ContentFile
2008-12-28 21:48:21 +00:00
from django.db import models
2008-12-31 17:54:47 +00:00
from django.db.models.base import ModelBase
from django.db.models.signals import post_delete
2009-08-31 21:00:38 +00:00
from django.utils.html import conditional_escape as escape
2008-12-31 17:54:47 +00:00
from django.utils.translation import ugettext_lazy as _
from imagekit.specs import ImageSpec
2009-01-08 21:11:15 +00:00
from imagekit.lib import *
from imagekit.options import Options
2009-01-08 20:04:20 +00:00
from imagekit.utils import img_to_fobj
2008-12-28 21:48:21 +00:00
# Modify image file buffer size.
ImageFile.MAXBLOCK = getattr(settings, 'PIL_IMAGEFILE_MAXBLOCK', 256 * 2 ** 10)
# Choice tuples for specifying the crop origin.
2009-03-13 17:14:53 +00:00
# These are provided for convenience.
CROP_HORZ_CHOICES = (
(0, _('left')),
(1, _('center')),
(2, _('right')),
)
CROP_VERT_CHOICES = (
(0, _('top')),
(1, _('center')),
(2, _('bottom')),
)
2008-12-28 21:48:21 +00:00
2009-01-14 16:09:17 +00:00
class ImageModelBase(ModelBase):
""" ImageModel metaclass
2009-12-19 16:01:54 +00:00
This metaclass parses IKOptions and loads the specified specification
module.
2009-12-19 16:01:54 +00:00
"""
2011-09-08 12:30:54 +00:00
def __init__(self, name, bases, attrs):
2011-09-08 20:50:06 +00:00
user_opts = getattr(self, 'IKOptions', None)
specs = []
default_image_field = getattr(user_opts, 'default_image_field', None)
2011-09-08 20:50:06 +00:00
for k, v in attrs.items():
if isinstance(v, ImageSpec):
specs.append(v)
elif not default_image_field and isinstance(v, models.ImageField):
default_image_field = k
2011-09-08 20:50:06 +00:00
user_opts.specs = specs
user_opts.default_image_field = default_image_field
2011-09-08 20:50:06 +00:00
opts = Options(user_opts)
setattr(self, '_ik', opts)
2011-09-08 12:30:54 +00:00
ModelBase.__init__(self, name, bases, attrs)
2009-01-14 16:09:17 +00:00
class ImageModel(models.Model):
2008-12-28 21:48:21 +00:00
""" Abstract base class implementing all core ImageKit functionality
2009-12-19 16:01:54 +00:00
Subclasses of ImageModel are augmented with accessors for each defined
image specification and can override the inner IKOptions class to customize
2008-12-28 21:48:21 +00:00
storage locations and other options.
2009-12-19 16:01:54 +00:00
2008-12-28 21:48:21 +00:00
"""
2009-01-14 16:09:17 +00:00
__metaclass__ = ImageModelBase
2008-12-28 21:48:21 +00:00
class Meta:
abstract = True
2009-12-19 16:01:54 +00:00
2011-09-08 12:30:54 +00:00
class IKOptions:
pass
@property
def _imgfields(self):
return set([spec._get_imgfield(self) for spec in self._ik.specs])