1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django import template
6 from django.template import Node, Variable, Template, Context
7 from catalogue.utils import split_tags
8 from itertools import chain
9 from ..engine import CustomCroppingEngine
10 import sorl.thumbnail.default
13 register = template.Library()
15 cropper = CustomCroppingEngine()
17 @register.inclusion_tag('picture/picture_short.html', takes_context=True)
18 def picture_short(context, picture):
21 'main_link': picture.get_absolute_url(),
22 # 'related': picture.related_info(),
23 'request': context.get('request'),
24 'tags': split_tags(picture.tags),
28 @register.inclusion_tag('picture/picture_wide.html', takes_context=True)
29 def picture_wide(context, picture):
32 'main_link': picture.get_absolute_url(),
33 # 'related': picture.related_info(),
34 'request': context.get('request'),
35 'tags': split_tags(picture.tags),
40 @register.simple_tag()
41 def area_thumbnail_url(area, geometry):
42 def to_square(coords):
43 w = coords[1][0] - coords[0][0]
44 h = coords[1][1] - coords[0][1]
48 return [[coords[0][0] + w/2 - h/2, coords[0][1]],
49 [coords[1][0] - w/2 + h/2, coords[1][1]]]
51 return [[coords[0][0], coords[0][1] + h/2 - w/2],
52 [coords[1][0], coords[1][1] - h/2 + w/2, ]]
54 # so much for sorl extensibility. # what to do about this?
55 _engine = sorl.thumbnail.default.engine
56 sorl.thumbnail.default.engine = cropper
57 coords = to_square(area.area)
60 th = sorl.thumbnail.default.backend.get_thumbnail(
61 area.picture.image_file,
63 crop="%dpx %dpx %dpx %dpx" % tuple(map(lambda d: max(0, d), tuple(coords[0] + coords[1]))))
65 logging.exception("Error creating a thumbnail for PictureArea")
68 sorl.thumbnail.default.engine = _engine