2008-12-31 18:40:51 +00:00
|
|
|
import os
|
2008-12-28 21:48:21 +00:00
|
|
|
from datetime import datetime
|
2009-01-04 16:29:53 +00:00
|
|
|
from django.conf import settings
|
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.utils.translation import ugettext_lazy as _
|
2009-01-04 18:40:22 +00:00
|
|
|
|
2009-01-04 18:30:03 +00:00
|
|
|
from imagekit.options import Options
|
|
|
|
|
from imagekit import specs
|
2008-12-28 21:48:21 +00:00
|
|
|
|
|
|
|
|
|
2008-12-31 17:54:47 +00:00
|
|
|
class IKModelBase(ModelBase):
|
2009-01-03 14:44:43 +00:00
|
|
|
def __init__(cls, name, bases, attrs):
|
2009-01-04 16:29:53 +00:00
|
|
|
|
|
|
|
|
parents = [b for b in bases if isinstance(b, IKModelBase)]
|
|
|
|
|
if not parents:
|
|
|
|
|
return
|
2009-01-03 14:44:43 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
user_opts = getattr(cls, 'IKConfig', None)
|
2009-01-04 18:30:03 +00:00
|
|
|
opts = Options(user_opts)
|
2009-01-04 16:29:53 +00:00
|
|
|
|
2009-01-03 14:44:43 +00:00
|
|
|
try:
|
2009-01-04 16:29:53 +00:00
|
|
|
module = __import__(opts.config_module, {}, {}, [''])
|
|
|
|
|
except ImportError:
|
|
|
|
|
raise ImportError('Unable to load imagekit config module: %s' % opts.config_module)
|
2008-12-31 17:54:47 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
for spec in [spec for spec in module.__dict__.values() \
|
|
|
|
|
if isinstance(spec, type) \
|
|
|
|
|
and issubclass(spec, specs.ImageSpec) \
|
|
|
|
|
and spec != specs.ImageSpec]:
|
2009-01-04 22:14:13 +00:00
|
|
|
setattr(cls, spec.name(), specs.Descriptor(spec))
|
|
|
|
|
opts.specs.append(spec)
|
2009-01-06 19:00:53 +00:00
|
|
|
|
2009-01-04 22:14:13 +00:00
|
|
|
setattr(cls, '_ik', opts)
|
2009-01-04 16:29:53 +00:00
|
|
|
|
|
|
|
|
|
2008-12-31 17:54:47 +00:00
|
|
|
class IKModel(models.Model):
|
2008-12-28 21:48:21 +00:00
|
|
|
""" Abstract base class implementing all core ImageKit functionality
|
|
|
|
|
|
2008-12-31 17:54:47 +00:00
|
|
|
Subclasses of IKModel can override the inner IKConfig class to customize
|
2008-12-28 21:48:21 +00:00
|
|
|
storage locations and other options.
|
|
|
|
|
|
|
|
|
|
"""
|
2008-12-31 17:54:47 +00:00
|
|
|
__metaclass__ = IKModelBase
|
2008-12-28 21:48:21 +00:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
class IKConfig:
|
2009-01-04 16:29:53 +00:00
|
|
|
pass
|
2009-01-04 22:14:13 +00:00
|
|
|
|
|
|
|
|
def admin_thumbnail_view(self):
|
|
|
|
|
prop = getattr(self, 'admin_thumbnail', None)
|
|
|
|
|
if prop is None:
|
|
|
|
|
return 'An "admin_thumbnail" image spec has not been defined.'
|
|
|
|
|
else:
|
|
|
|
|
if hasattr(self, 'get_absolute_url'):
|
|
|
|
|
return u'<a href="%s"><img src="%s"></a>' % \
|
|
|
|
|
(self.get_absolute_url(), prop.url)
|
|
|
|
|
else:
|
|
|
|
|
return u'<a href="%s"><img src="%s"></a>' % \
|
2009-01-06 19:00:53 +00:00
|
|
|
(self.ik_image_field.url, prop.url)
|
2009-01-04 22:14:13 +00:00
|
|
|
admin_thumbnail_view.short_description = _('Thumbnail')
|
|
|
|
|
admin_thumbnail_view.allow_tags = True
|
2009-01-04 18:30:03 +00:00
|
|
|
|
2009-01-06 19:00:53 +00:00
|
|
|
@property
|
|
|
|
|
def ik_image_field(self):
|
|
|
|
|
return getattr(self, self._ik.image_field)
|
|
|
|
|
|
2009-01-04 18:30:03 +00:00
|
|
|
@property
|
|
|
|
|
def cache_dir(self):
|
2008-12-28 21:48:21 +00:00
|
|
|
""" Returns the path to the image cache directory """
|
2009-01-06 19:00:53 +00:00
|
|
|
return os.path.join(os.path.dirname(self.ik_image_field.path),
|
|
|
|
|
self._ik.cache_dir)
|
2008-12-28 21:48:21 +00:00
|
|
|
|
2009-01-04 18:30:03 +00:00
|
|
|
@property
|
|
|
|
|
def cache_url(self):
|
2008-12-28 21:48:21 +00:00
|
|
|
""" Returns a url pointing to the image cache directory """
|
2009-01-06 19:00:53 +00:00
|
|
|
return '/'.join([os.path.dirname(self.ik_image_field.url),
|
|
|
|
|
self._ik.cache_dir])
|
2009-01-04 18:30:03 +00:00
|
|
|
|
2008-12-28 21:48:21 +00:00
|
|
|
def _cleanup_cache_dirs(self):
|
|
|
|
|
try:
|
2009-01-04 22:14:13 +00:00
|
|
|
os.removedirs(self.cache_path)
|
2008-12-28 21:48:21 +00:00
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def _clear_cache(self):
|
2009-01-04 22:14:13 +00:00
|
|
|
for spec in self._ik.specs:
|
|
|
|
|
prop = getattr(self, spec.name())
|
|
|
|
|
prop.delete()
|
2008-12-28 21:48:21 +00:00
|
|
|
self._cleanup_cache_dirs()
|
|
|
|
|
|
|
|
|
|
def _pre_cache(self):
|
2009-01-04 22:14:13 +00:00
|
|
|
for spec in self._ik.specs:
|
|
|
|
|
if spec.pre_cache:
|
|
|
|
|
prop = getattr(self, spec.name())
|
|
|
|
|
prop.create()
|
2008-12-28 21:48:21 +00:00
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2009-01-06 19:00:53 +00:00
|
|
|
#if self._get_pk_val():
|
|
|
|
|
# self._clear_cache()
|
2009-01-04 16:29:53 +00:00
|
|
|
super(IKModel, self).save(*args, **kwargs)
|
2009-01-06 19:00:53 +00:00
|
|
|
#self._pre_cache()
|
2008-12-28 21:48:21 +00:00
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
|
assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
|
2009-01-06 19:00:53 +00:00
|
|
|
self._clear_cache()
|
2009-01-04 22:14:13 +00:00
|
|
|
super(IKModel, self).delete()
|