diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-05-22 18:52:28 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-05-22 18:52:28 +0200 |
commit | 42e31c5060a61e42822a166928b7aa39da7867fc (patch) | |
tree | 939f39f7bd4bfc50654d0c621777dd7b4bbae060 | |
parent | 3750e45bc8c7c3d6ab4c709f05415113c1d434c3 (diff) | |
download | Ishtar-42e31c5060a61e42822a166928b7aa39da7867fc.tar.bz2 Ishtar-42e31c5060a61e42822a166928b7aa39da7867fc.zip |
Document template: manage height, with and aspect ratio for images
-rw-r--r-- | ishtar_common/models.py | 4 | ||||
-rw-r--r-- | ishtar_common/utils_secretary.py | 56 |
2 files changed, 58 insertions, 2 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 7a10c3e9d..e78e18704 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -63,7 +63,7 @@ from django.utils.functional import lazy from django.utils.safestring import SafeUnicode, mark_safe from django.utils.translation import ugettext_lazy as _, ugettext, \ pgettext_lazy, activate, deactivate -from secretary import Renderer as SecretaryRenderer +from ishtar_common.utils_secretary import IshtarSecretaryRenderer from simple_history.models import HistoricalRecords as BaseHistoricalRecords from unidecode import unidecode @@ -2986,7 +2986,7 @@ class DocumentTemplate(models.Model): datetime.date.today().strftime('%Y-%m-%d') + \ u"." + self.template.name.split('.')[-1] values = c_object.get_values() - engine = SecretaryRenderer() + engine = IshtarSecretaryRenderer() try: result = engine.render(self.template, **values) except TemplateSyntaxError as e: diff --git a/ishtar_common/utils_secretary.py b/ishtar_common/utils_secretary.py new file mode 100644 index 000000000..6f605590e --- /dev/null +++ b/ishtar_common/utils_secretary.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from secretary import Renderer +from PIL import Image +import re + + +RE_UNITS = re.compile("([.0-9]+)([a-z]+)") + + +def parse_value_unit(value): + m = RE_UNITS.match(value) + if not m: + return None, None + value, unit = m.groups() + value = float(value) + return value, unit + + +class IshtarSecretaryRenderer(Renderer): + def __init__(self, *args, **kwargs): + super(IshtarSecretaryRenderer, self).__init__(*args, **kwargs) + self.media_callback = self.ishtar_media_loader + # self.environment.filters['pad'] = pad_string + + def ishtar_media_loader(self, media, *args, **kwargs): + """Loads a file from the file system. + :param media: A file object or a relative or absolute path of a file. + :type media: unicode + """ + image_file, mime = self.fs_loader(media, *args, **kwargs) + if "width" in kwargs: + kwargs['frame_attrs']['svg:width'] = kwargs["width"] + if "height" in kwargs: + kwargs['frame_attrs']['svg:height'] = kwargs["height"] + if "keep_ratio" in args: + image = Image.open(image_file.name) + width, width_unit = parse_value_unit( + kwargs['frame_attrs']['svg:width']) + height, height_unit = parse_value_unit( + kwargs['frame_attrs']['svg:height']) + if "height" not in kwargs and width: + new_height = width * image.height / image.width + kwargs['frame_attrs']['svg:height'] = "{}{}".format( + new_height, width_unit + ) + if "width" not in kwargs and height: + new_width = height * image.width / image.height + kwargs['frame_attrs']['svg:width'] = "{}{}".format( + new_width, height_unit + ) + return image_file, mime + + + |