Initial commit
[copyspeak.git] / src / 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     print content
24     return content.raw if raw else 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