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 catalogue.utils import split_tags
 
   7 from ..engine import CustomCroppingEngine
 
   8 import sorl.thumbnail.default
 
  11 register = template.Library()
 
  13 cropper = CustomCroppingEngine()
 
  15 @register.inclusion_tag('picture/picture_short.html', takes_context=True)
 
  16 def picture_short(context, picture):
 
  19         'main_link': picture.get_absolute_url(),
 
  20         'request': context.get('request'),
 
  21         'tags': split_tags(picture.tags),
 
  25 @register.inclusion_tag('picture/picture_wide.html', takes_context=True)
 
  26 def picture_wide(context, picture):
 
  29         'main_link': picture.get_absolute_url(),
 
  30         'request': context.get('request'),
 
  31         'tags': split_tags(picture.tags),
 
  36 @register.simple_tag()
 
  37 def area_thumbnail_url(area, geometry):
 
  38     def to_square(coords):
 
  39         w = coords[1][0] - coords[0][0]
 
  40         h = coords[1][1] - coords[0][1]
 
  44             return [[coords[0][0] + w/2 - h/2, coords[0][1]],
 
  45                     [coords[1][0] - w/2 + h/2, coords[1][1]]]
 
  47             return [[coords[0][0], coords[0][1] + h/2 - w/2],
 
  48                     [coords[1][0], coords[1][1] - h/2 + w/2, ]]
 
  50     # so much for sorl extensibility.
 
  51     # what to do about this?
 
  52     _engine = sorl.thumbnail.default.engine
 
  53     sorl.thumbnail.default.engine = cropper
 
  54     coords = to_square(area.area)
 
  57         th = sorl.thumbnail.default.backend.get_thumbnail(
 
  58             area.picture.image_file,
 
  60             crop="%dpx %dpx %dpx %dpx" % tuple(map(lambda d: max(0, d), tuple(coords[0] + coords[1]))))
 
  62         logging.exception("Error creating a thumbnail for PictureArea")
 
  65     sorl.thumbnail.default.engine = _engine