File: fields.py

package info (click to toggle)
python-django-imagekit 5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 692 kB
  • sloc: python: 1,975; makefile: 133; sh: 6
file content (34 lines) | stat: -rw-r--r-- 1,266 bytes parent folder | download
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
34
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().__init__(*args, **kwargs)

    def clean(self, data, initial=None):
        data = super().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 directly return the result from `generate(spec)`
            f.name = data.name
            return f

        return data