mark_safe chunks.
[prawokultury.git] / chunks / templatetags / chunks.py
1 from django import template
2 from django.db import models
3 from django.core.cache import cache
4 from django.utils.safestring import mark_safe
5 from ..models import Chunk, Attachment
6
7
8 register = template.Library()
9
10
11 @register.simple_tag
12 def chunk(key, cache_time=0):
13     try:
14         cache_key = 'chunk_' + key
15         c = cache.get(cache_key)
16         if c is None:
17             c = Chunk.objects.get(key=key)
18             cache.set(cache_key, c, int(cache_time))
19         content = c.content
20     except Chunk.DoesNotExist:
21         n = Chunk(key=key)
22         n.save()
23         return ''
24     return mark_safe(content)
25
26
27 @register.simple_tag
28 def attachment(key, cache_time=0):
29     try:
30         cache_key = 'attachment_' + key
31         c = cache.get(cache_key)
32         if c is None:
33             c = Attachment.objects.get(key=key)
34             cache.set(cache_key, c, int(cache_time))
35         return c.attachment.url
36     except Attachment.DoesNotExist:
37         return ''
38
39