File: fields.py

package info (click to toggle)
python-django-imagekit 4.0.2-3%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 704 kB
  • sloc: python: 2,000; makefile: 133
file content (33 lines) | stat: -rw-r--r-- 1,316 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django.forms import ImageField
from ..specs import SpecHost
from ..utils import generate


class ProcessedImageField(ImageField, SpecHost):

    def __init__(self, processors=None, format=None, options=None,
                 autoconvert=True, spec_id=None, spec=None, *args, **kwargs):

        if spec_id is None:
            # 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')

        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)

        if data and data != initial:
            spec = self.get_spec(source=data)
            f = generate(spec)
            # Name is required in Django 1.4. When we drop support for it
            # then we can dirrectly return the result from `generate(spec)`
            f.name = data.name
            return f

        return data