Seasonal annoying.
[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 ..models import Chunk, Attachment
5
6
7 register = template.Library()
8
9
10 @register.simple_tag
11 def chunk(key, cache_time=0, raw=False):
12     try:
13         cache_key = 'chunk_' + key
14         c = cache.get(cache_key)
15         if c is None:
16             c = Chunk.objects.get(key=key)
17             cache.set(cache_key, c, int(cache_time))
18         content = c.content
19     except Chunk.DoesNotExist:
20         n = Chunk(key=key)
21         n.save()
22         return ''
23     return content.raw if raw else content
24
25
26 @register.simple_tag
27 def attachment(key, cache_time=0):
28     try:
29         cache_key = 'attachment_' + key
30         c = cache.get(cache_key)
31         if c is None:
32             c = Attachment.objects.get(key=key)
33             cache.set(cache_key, c, int(cache_time))
34         return c.attachment.url
35     except Attachment.DoesNotExist:
36         return ''
37
38