mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-16 21:30:23 +00:00
Fixed #368 use specs directly in ProcessedImageField
Thanks to @xcono for pointing to solution to the problem
This commit is contained in:
parent
c3dbb1edf0
commit
2b04099dc4
3 changed files with 29 additions and 2 deletions
|
|
@ -93,7 +93,7 @@ class ProcessedImageField(models.ImageField, SpecHostField):
|
|||
|
||||
def __init__(self, processors=None, format=None, options=None,
|
||||
verbose_name=None, name=None, width_field=None, height_field=None,
|
||||
autoconvert=True, spec=None, spec_id=None, **kwargs):
|
||||
autoconvert=None, spec=None, spec_id=None, **kwargs):
|
||||
"""
|
||||
The ProcessedImageField constructor accepts all of the arguments that
|
||||
the :class:`django.db.models.ImageField` constructor accepts, as well
|
||||
|
|
@ -101,6 +101,10 @@ class ProcessedImageField(models.ImageField, SpecHostField):
|
|||
:class:`imagekit.models.ImageSpecField`.
|
||||
|
||||
"""
|
||||
# if spec is not provided then autoconvert will be True by default
|
||||
if spec is None and autoconvert is None:
|
||||
autoconvert = True
|
||||
|
||||
SpecHost.__init__(self, processors=processors, format=format,
|
||||
options=options, autoconvert=autoconvert, spec=spec,
|
||||
spec_id=spec_id)
|
||||
|
|
|
|||
|
|
@ -1,10 +1,17 @@
|
|||
from django.db import models
|
||||
|
||||
from imagekit import ImageSpec
|
||||
from imagekit.models import ProcessedImageField
|
||||
from imagekit.models import ImageSpecField
|
||||
from imagekit.processors import Adjust, ResizeToFill, SmartCrop
|
||||
|
||||
|
||||
class Thumbnail(ImageSpec):
|
||||
processors = [ResizeToFill(100, 60)]
|
||||
format = 'JPEG'
|
||||
options = {'quality': 60}
|
||||
|
||||
|
||||
class ImageModel(models.Model):
|
||||
image = models.ImageField(upload_to='b')
|
||||
|
||||
|
|
@ -27,6 +34,10 @@ class ProcessedImageFieldModel(models.Model):
|
|||
options={'quality': 90}, upload_to='p')
|
||||
|
||||
|
||||
class ProcessedImageFieldWithSpecModel(models.Model):
|
||||
processed = ProcessedImageField(spec=Thumbnail, upload_to='p')
|
||||
|
||||
|
||||
class CountingCacheFileStrategy(object):
|
||||
def __init__(self):
|
||||
self.on_existence_required_count = 0
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@ from imagekit import forms as ikforms
|
|||
from imagekit.processors import SmartCrop
|
||||
from nose.tools import eq_
|
||||
from . import imagegenerators # noqa
|
||||
from .models import ProcessedImageFieldModel, ImageModel
|
||||
from .models import (ProcessedImageFieldModel,
|
||||
ProcessedImageFieldWithSpecModel,
|
||||
ImageModel)
|
||||
from .utils import get_image_file
|
||||
|
||||
|
||||
|
|
@ -19,6 +21,16 @@ def test_model_processedimagefield():
|
|||
eq_(instance.processed.height, 50)
|
||||
|
||||
|
||||
def test_model_processedimagefield_with_spec():
|
||||
instance = ProcessedImageFieldWithSpecModel()
|
||||
file = File(get_image_file())
|
||||
instance.processed.save('whatever.jpeg', file)
|
||||
instance.save()
|
||||
|
||||
eq_(instance.processed.width, 100)
|
||||
eq_(instance.processed.height, 60)
|
||||
|
||||
|
||||
def test_form_processedimagefield():
|
||||
class TestForm(forms.ModelForm):
|
||||
image = ikforms.ProcessedImageField(spec_id='tests:testform_image',
|
||||
|
|
|
|||
Loading…
Reference in a new issue