File: models.py

package info (click to toggle)
python-django-imagekit 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 684 kB
  • sloc: python: 2,000; makefile: 133
file content (72 lines) | stat: -rw-r--r-- 2,137 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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')


class Photo(models.Model):
    original_image = models.ImageField(upload_to='photos')

    # Implicit source field
    thumbnail = ImageSpecField([Adjust(contrast=1.2, sharpness=1.1),
                               ResizeToFill(50, 50)], format='JPEG',
                               options={'quality': 90})

    smartcropped_thumbnail = ImageSpecField([Adjust(contrast=1.2,
            sharpness=1.1), SmartCrop(50, 50)], source='original_image',
            format='JPEG', options={'quality': 90})


class ProcessedImageFieldModel(models.Model):
    processed = ProcessedImageField([SmartCrop(50, 50)], format='JPEG',
            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
        self.on_content_required_count = 0
        self.on_source_saved_count = 0

    def on_existence_required(self, file):
        self.on_existence_required_count += 1

    def on_content_required(self, file):
        self.on_content_required_count += 1

    def on_source_saved(self, file):
        self.on_source_saved_count += 1


class AbstractImageModel(models.Model):
    original_image = models.ImageField(upload_to='photos')
    abstract_class_spec = ImageSpecField(source='original_image',
                                         format='JPEG',
                                         cachefile_strategy=CountingCacheFileStrategy())

    class Meta:
        abstract = True


class ConcreteImageModel(AbstractImageModel):
    pass


class ConcreteImageModelSubclass(ConcreteImageModel):
    pass