X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/0cae17bec6d31806615fae59a5b3945016285fbe..e6fc1b86fcdb4f99c00d4218fcaa5eab6f64c4cb:/apps/chunks/templatetags/chunks.py diff --git a/apps/chunks/templatetags/chunks.py b/apps/chunks/templatetags/chunks.py index f79d495ec..cc25df728 100644 --- a/apps/chunks/templatetags/chunks.py +++ b/apps/chunks/templatetags/chunks.py @@ -2,44 +2,37 @@ from django import template from django.db import models from django.core.cache import cache + register = template.Library() Chunk = models.get_model('chunks', 'chunk') -CACHE_PREFIX = "chunk_" +Attachment = models.get_model('chunks', 'attachment') + + +@register.simple_tag +def chunk(key, cache_time=0): + try: + cache_key = Chunk.cache_key(key) + c = cache.get(cache_key) + if c is None: + c = Chunk.objects.get(key=key) + cache.set(cache_key, c, int(cache_time)) + content = c.content + except Chunk.DoesNotExist: + n = Chunk(key=key) + n.save() + return '' + return content -def do_get_chunk(parser, token): - # split_contents() knows not to split quoted strings. - tokens = token.split_contents() - if len(tokens) < 2 or len(tokens) > 3: - raise template.TemplateSyntaxError, "%r tag should have either 2 or 3 arguments" % (tokens[0],) - if len(tokens) == 2: - tag_name, key = tokens - cache_time = 0 - if len(tokens) == 3: - tag_name, key, cache_time = tokens - # Check to see if the key is properly double/single quoted - if not (key[0] == key[-1] and key[0] in ('"', "'")): - raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name - # Send key without quotes and caching time - return ChunkNode(key[1:-1], cache_time) -class ChunkNode(template.Node): - def __init__(self, key, cache_time=0): - self.key = key - self.cache_time = cache_time - - def render(self, context): - try: - cache_key = CACHE_PREFIX + self.key - c = cache.get(cache_key) - if c is None: - c = Chunk.objects.get(key=self.key) - cache.set(cache_key, c, int(self.cache_time)) - content = c.content - except Chunk.DoesNotExist: - n = Chunk(key=self.key) - n.save() - return '' - return content - -register.tag('chunk', do_get_chunk) +@register.simple_tag +def attachment(key, cache_time=0): + try: + cache_key = 'attachment_' + key + c = cache.get(cache_key) + if c is None: + c = Attachment.objects.get(key=key) + cache.set(cache_key, c, int(cache_time)) + return c.attachment.url + except Attachment.DoesNotExist: + return ''