2012-10-26 02:46:28 +00:00
|
|
|
from django.forms import ImageField
|
|
|
|
|
from ..specs import SpecHost
|
2013-01-24 03:54:25 +00:00
|
|
|
from ..utils import generate
|
2012-10-26 02:46:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessedImageField(ImageField, SpecHost):
|
|
|
|
|
|
|
|
|
|
def __init__(self, processors=None, format=None, options=None,
|
2012-12-01 20:56:37 +00:00
|
|
|
autoconvert=True, spec_id=None, spec=None, *args, **kwargs):
|
2012-10-26 02:46:28 +00:00
|
|
|
|
|
|
|
|
if spec_id is None:
|
2012-12-01 20:56:37 +00:00
|
|
|
# Unlike model fields, form fields are never told their field name.
|
|
|
|
|
# (Model fields are done so via `contribute_to_class()`.) Therefore
|
|
|
|
|
# we can't really generate a good spec id automatically.
|
|
|
|
|
raise TypeError('You must provide a spec_id')
|
2012-10-26 02:46:28 +00:00
|
|
|
|
|
|
|
|
SpecHost.__init__(self, processors=processors, format=format,
|
|
|
|
|
options=options, autoconvert=autoconvert, spec=spec,
|
|
|
|
|
spec_id=spec_id)
|
|
|
|
|
super(ProcessedImageField, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def clean(self, data, initial=None):
|
|
|
|
|
data = super(ProcessedImageField, self).clean(data, initial)
|
|
|
|
|
|
2013-08-13 12:52:21 +00:00
|
|
|
if data and data != initial:
|
2013-01-24 03:54:25 +00:00
|
|
|
spec = self.get_spec(source=data)
|
|
|
|
|
data = generate(spec)
|
2012-10-26 02:46:28 +00:00
|
|
|
|
|
|
|
|
return data
|