From efb623af0bcb47d510501c282e1326b11343a29c Mon Sep 17 00:00:00 2001 From: luxagraf Date: Sat, 22 Sep 2012 22:27:04 -0400 Subject: site reorg --- app/lib/chunks/__init__.py | 0 app/lib/chunks/admin.py | 8 ++++++ app/lib/chunks/models.py | 14 +++++++++++ app/lib/chunks/templatetags/__init__.py | 0 app/lib/chunks/templatetags/chunks.py | 43 +++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+) create mode 100755 app/lib/chunks/__init__.py create mode 100755 app/lib/chunks/admin.py create mode 100755 app/lib/chunks/models.py create mode 100755 app/lib/chunks/templatetags/__init__.py create mode 100755 app/lib/chunks/templatetags/chunks.py (limited to 'app/lib/chunks') diff --git a/app/lib/chunks/__init__.py b/app/lib/chunks/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/app/lib/chunks/admin.py b/app/lib/chunks/admin.py new file mode 100755 index 0000000..06dd917 --- /dev/null +++ b/app/lib/chunks/admin.py @@ -0,0 +1,8 @@ +from django.contrib import admin +from models import Chunk + +class ChunkAdmin(admin.ModelAdmin): + list_display = ('key',) + search_fields = ('key', 'content') + +admin.site.register(Chunk, ChunkAdmin) \ No newline at end of file diff --git a/app/lib/chunks/models.py b/app/lib/chunks/models.py new file mode 100755 index 0000000..cdb36be --- /dev/null +++ b/app/lib/chunks/models.py @@ -0,0 +1,14 @@ +from django.db import models + +class Chunk(models.Model): + """ + A Chunk is a piece of content associated + with a unique key that can be inserted into + any template with the use of a special template + tag + """ + key = models.CharField(help_text="A unique name for this chunk of content", blank=False, max_length=255, unique=True) + content = models.TextField(blank=True) + + def __unicode__(self): + return u"%s" % (self.key,) diff --git a/app/lib/chunks/templatetags/__init__.py b/app/lib/chunks/templatetags/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/app/lib/chunks/templatetags/chunks.py b/app/lib/chunks/templatetags/chunks.py new file mode 100755 index 0000000..2b06cad --- /dev/null +++ b/app/lib/chunks/templatetags/chunks.py @@ -0,0 +1,43 @@ +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_" + +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: + content = '' + return content + +register.tag('chunk', do_get_chunk) -- cgit v1.2.3-70-g09d2