Cleaning: timezone issues, deprecated urls.py imports, missing notes.
[wolnelektury.git] / apps / picture / templatetags / picture_tags.py
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.
4 #
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
11 import logging 
12
13 register = template.Library()
14
15 cropper = CustomCroppingEngine()
16
17 @register.inclusion_tag('picture/picture_short.html', takes_context=True)
18 def picture_short(context, picture):
19     context.update({
20         'picture': picture,
21         'main_link': picture.get_absolute_url(),
22         # 'related': picture.related_info(),
23         'request': context.get('request'),
24         'tags': split_tags(picture.tags),
25         })
26     return context
27                             
28 @register.inclusion_tag('picture/picture_wide.html', takes_context=True)
29 def picture_wide(context, picture):
30     context.update({
31         'picture': picture,
32         'main_link': picture.get_absolute_url(),
33         # 'related': picture.related_info(),
34         'request': context.get('request'),
35         'tags': split_tags(picture.tags),
36         })
37     return context
38
39
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]
45         if w == h:
46             return coords
47         elif w > h:
48             return [[coords[0][0] + w/2 - h/2, coords[0][1]],
49                     [coords[1][0] - w/2 + h/2, coords[1][1]]]
50         else:
51             return [[coords[0][0], coords[0][1] + h/2 - w/2],
52                     [coords[1][0], coords[1][1] - h/2 + w/2, ]]
53
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)
58
59     try:
60         th = sorl.thumbnail.default.backend.get_thumbnail(
61             area.picture.image_file,
62             geometry,
63             crop="%dpx %dpx %dpx %dpx" % tuple(map(lambda d: max(0, d), tuple(coords[0] + coords[1]))))
64     except Exception, e:
65         logging.exception("Error creating a thumbnail for PictureArea")
66         return ''
67
68     sorl.thumbnail.default.engine = _engine
69
70     return th.url
71             
72
73                                             
74