1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
   4 from django import template
 
   5 from django.core.cache import cache
 
   6 from django.utils.safestring import mark_safe
 
   7 from django.utils.translation import get_language
 
   8 from ..models import Chunk, Attachment
 
  11 register = template.Library()
 
  14 @register.simple_tag(takes_context=True)
 
  15 def chunk(context, key, cache_time=0):
 
  17         cache_key = 'chunk:%s:%s' % (key, get_language())
 
  18         c = cache.get(cache_key)
 
  20             c = Chunk.objects.get(key=key)
 
  21             cache.set(cache_key, c, int(cache_time))
 
  23     except Chunk.DoesNotExist:
 
  28     if context['request'].user.is_staff:
 
  29         content = f'<span data-edit="chunks/chunk/{key}"></span>' + content
 
  31     return mark_safe(content)
 
  35 def attachment(key, cache_time=0):
 
  37         cache_key = 'attachment_' + key
 
  38         c = cache.get(cache_key)
 
  40             c = Attachment.objects.get(key=key)
 
  41             cache.set(cache_key, c, int(cache_time))
 
  42         return c.attachment.url
 
  43     except Attachment.DoesNotExist: