Completed
Pull Request — master (#81)
by
unknown
01:15
created

ThumbnailWithoutDirectoryModel   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %
Metric Value
wmc 1
dl 0
loc 5
rs 10
1
from io import BytesIO
2
3
from django.core.files.base import ContentFile
4
from django.core.files.storage import FileSystemStorage
5
from django.db import models
6
from django.db.models.signals import post_delete, pre_save
7
from PIL import Image
8
9
from stdimage import StdImageField
10
from stdimage.models import StdImageFieldFile
11
from stdimage.utils import (UploadTo, UploadToAutoSlugClassNameDir,
12
                            UploadToUUID, pre_delete_delete_callback,
13
                            pre_save_delete_callback, render_variations)
14
from stdimage.validators import MaxSizeValidator, MinSizeValidator
15
16
17
class SimpleModel(models.Model):
18
    """works as ImageField"""
19
    image = StdImageField(upload_to='img/')
20
21
22
class AdminDeleteModel(models.Model):
23
    """can be deleted through admin"""
24
    image = StdImageField(
25
        upload_to=UploadTo(name='image', path='img'),
26
        blank=True
27
    )
28
29
30
class ResizeModel(models.Model):
31
    """resizes image to maximum size to fit a 640x480 area"""
32
    image = StdImageField(
33
        upload_to=UploadTo(name='image', path='img'),
34
        variations={
35
            'medium': {'width': 400, 'height': 400},
36
            'thumbnail': (100, 75),
37
        }
38
    )
39
40
41
class ResizeCropModel(models.Model):
42
    """resizes image to 640x480 cropping if necessary"""
43
    image = StdImageField(
44
        upload_to=UploadTo(name='image', path='img'),
45
        variations={'thumbnail': (150, 150, True)}
46
    )
47
48
49
class ThumbnailModel(models.Model):
50
    """creates a thumbnail resized to maximum size to fit a 100x75 area"""
51
    image = StdImageField(
52
        upload_to=UploadTo(name='image', path='img'),
53
        blank=True,
54
        variations={'thumbnail': (100, 75)}
55
    )
56
57
58
class MaxSizeModel(models.Model):
59
    image = StdImageField(
60
        upload_to=UploadTo(name='image', path='img'),
61
        validators=[MaxSizeValidator(16, 16)]
62
    )
63
64
65
class MinSizeModel(models.Model):
66
    image = StdImageField(
67
        upload_to=UploadTo(name='image', path='img'),
68
        validators=[MinSizeValidator(200, 200)]
69
    )
70
71
72
class ForceMinSizeModel(models.Model):
73
    """creates a thumbnail resized to maximum size to fit a 100x75 area"""
74
    image = StdImageField(
75
        upload_to=UploadTo(name='image', path='img'),
76
        force_min_size=True,
77
        variations={'thumbnail': (600, 600)}
78
    )
79
80
81
class AutoSlugClassNameDirModel(models.Model):
82
    name = models.CharField(max_length=50)
83
    image = StdImageField(
84
        upload_to=UploadToAutoSlugClassNameDir(populate_from='name')
85
    )
86
87
88
class UUIDModel(models.Model):
89
    image = StdImageField(upload_to=UploadToUUID(path='img'))
90
91
92
class CustomManager(models.Manager):
93
    """Just like Django's default, but a different class."""
94
    pass
95
96
97
class CustomManagerModel(models.Model):
98
    customer_manager = CustomManager()
99
100
    class Meta:
101
        abstract = True
102
103
104
class ManualVariationsModel(CustomManagerModel):
105
    """delays creation of 150x150 thumbnails until it is called manually"""
106
    image = StdImageField(
107
        upload_to=UploadTo(name='image', path='img'),
108
        variations={'thumbnail': (150, 150, True)},
109
        render_variations=False
110
    )
111
112
113
class MyStorageModel(CustomManagerModel):
114
    """delays creation of 150x150 thumbnails until it is called manually"""
115
    image = StdImageField(
116
        upload_to=UploadTo(name='image', path='img'),
117
        variations={'thumbnail': (150, 150, True)},
118
        render_variations=False,
119
        storage=FileSystemStorage(),
120
    )
121
122
123
def render_job(**kwargs):
124
    render_variations(**kwargs)
125
    return False
126
127
128
class UtilVariationsModel(models.Model):
129
    """delays creation of 150x150 thumbnails until it is called manually"""
130
    image = StdImageField(
131
        upload_to=UploadTo(name='image', path='img'),
132
        variations={'thumbnail': (150, 150, True)},
133
        render_variations=render_job
134
    )
135
136
137
class ThumbnailWithoutDirectoryModel(models.Model):
138
    """Save into a generated filename that does not contain any '/' char"""
139
    image = StdImageField(
140
        upload_to=lambda instance, filename: 'custom.gif',
141
        variations={'thumbnail': {'width': 150, 'height': 150}},
142
    )
143
144
145 View Code Duplication
def custom_render_variations(file_name, variations, storage):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
146
    """Resize image to 100x100."""
147
    for _, variation in variations.items():
148
        variation_name = StdImageFieldFile.get_variation_name(
149
            file_name,
150
            variation['name']
151
        )
152
        if storage.exists(variation_name):
153
            storage.delete(variation_name)
154
155
        with storage.open(file_name) as f:
156
            with Image.open(f) as img:
157
                size = 100, 100
158
                img = img.resize(size)
159
160
                with BytesIO() as file_buffer:
161
                    img.save(file_buffer, 'JPEG')
162
                    f = ContentFile(file_buffer.getvalue())
163
                    storage.save(variation_name, f)
164
165
    return False
166
167
168
class CustomRenderVariationsModel(models.Model):
169
    """Use custom render_variations."""
170
171
    image = StdImageField(
172
        upload_to=UploadTo(name='image', path='img'),
173
        variations={'thumbnail': (150, 150)},
174
        render_variations=custom_render_variations,
175
    )
176
177
178 View Code Duplication
def custom_render_variations_not_returning_bool(file_name, variations,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
179
                                                storage):
180
    """Save files as is BUT do not return bool."""
181
    for _, variation in variations.items():
182
        variation_name = StdImageFieldFile.get_variation_name(
183
            file_name,
184
            variation['name']
185
        )
186
        if storage.exists(variation_name):
187
            storage.delete(variation_name)
188
189
        with storage.open(file_name) as f:
190
            with Image.open(f) as img, BytesIO() as file_buffer:
191
                img.save(file_buffer, 'JPEG')
192
                f = ContentFile(file_buffer.getvalue())
193
                storage.save(variation_name, f)
194
195
196
class CustomRenderVariationsNotReturningBoolModel(models.Model):
197
    """Must raise TypeError when create."""
198
199
    image = StdImageField(
200
        upload_to=UploadTo(name='image', path='img'),
201
        variations={'thumbnail': (150, 150)},
202
        render_variations=custom_render_variations_not_returning_bool,
203
    )
204
205
post_delete.connect(pre_delete_delete_callback, sender=SimpleModel)
206
pre_save.connect(pre_save_delete_callback, sender=AdminDeleteModel)
207