-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathmodels.py
More file actions
94 lines (78 loc) · 2.92 KB
/
models.py
File metadata and controls
94 lines (78 loc) · 2.92 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Simple image inclusion content: You should probably use the media library
instead.
"""
import os
from django.db import models
from django.utils.translation import gettext_lazy as _
from feincms import settings
from feincms.templatetags import feincms_thumbnail
from feincms.utils.tuple import AutoRenderTuple
class ImageContent(models.Model):
# You should probably use
# `feincms.content.medialibrary.models.MediaFileContent` instead.
"""
Create an ImageContent like this::
Cls.create_content_type(
ImageContent,
POSITION_CHOICES=(
('left', 'Float to left'),
('right', 'Float to right'),
('block', 'Block'),
),
FORMAT_CHOICES=(
('noop', 'Do not resize'),
('cropscale:100x100', 'Square Thumbnail'),
('cropscale:200x450', 'Medium Portait'),
('thumbnail:1000x1000', 'Large'),
))
Note that FORMAT_CHOICES is optional. The part before the colon
corresponds to the template filters in the ``feincms_thumbnail``
template filter library. Known values are ``cropscale`` and
``thumbnail``. Everything else (such as ``noop``) is ignored.
"""
image = models.ImageField(
_("image"),
max_length=255,
upload_to=os.path.join(settings.FEINCMS_UPLOAD_PREFIX, "imagecontent"),
)
alt_text = models.CharField(
_("alternate text"),
max_length=255,
blank=True,
help_text=_("Description of image"),
)
caption = models.CharField(_("caption"), max_length=255, blank=True)
class Meta:
abstract = True
verbose_name = _("image")
verbose_name_plural = _("images")
def render(self, **kwargs):
templates = ["content/image/default.html"]
if hasattr(self, "position"):
templates.insert(0, "content/image/%s.html" % self.position)
return AutoRenderTuple((templates, {"content": self}))
def get_image(self):
img_type, _, size = getattr(self, "format", "").partition(":")
if not size:
return self.image
thumbnailer = {"cropscale": feincms_thumbnail.CropscaleThumbnailer}.get(
img_type, feincms_thumbnail.Thumbnailer
)
return thumbnailer(self.image, size)
@classmethod
def initialize_type(cls, POSITION_CHOICES=None, FORMAT_CHOICES=None):
if POSITION_CHOICES:
models.CharField(
_("position"),
max_length=10,
choices=POSITION_CHOICES,
default=POSITION_CHOICES[0][0],
).contribute_to_class(cls, "position")
if FORMAT_CHOICES:
models.CharField(
_("format"),
max_length=64,
choices=FORMAT_CHOICES,
default=FORMAT_CHOICES[0][0],
).contribute_to_class(cls, "format")