summaryrefslogtreecommitdiff
path: root/app/lib/chunks
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2012-09-22 22:27:04 -0400
committerluxagraf <sng@luxagraf.net>2012-09-22 22:27:04 -0400
commitefb623af0bcb47d510501c282e1326b11343a29c (patch)
tree3a35fb19f5eba3b219c65277a5fb712cbe9604ac /app/lib/chunks
parent0b481fd7931c2ae20ca21f89a87f2ba6a6c01e10 (diff)
site reorg
Diffstat (limited to 'app/lib/chunks')
-rwxr-xr-xapp/lib/chunks/__init__.py0
-rwxr-xr-xapp/lib/chunks/admin.py8
-rwxr-xr-xapp/lib/chunks/models.py14
-rwxr-xr-xapp/lib/chunks/templatetags/__init__.py0
-rwxr-xr-xapp/lib/chunks/templatetags/chunks.py43
5 files changed, 65 insertions, 0 deletions
diff --git a/app/lib/chunks/__init__.py b/app/lib/chunks/__init__.py
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/app/lib/chunks/__init__.py
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
--- /dev/null
+++ b/app/lib/chunks/templatetags/__init__.py
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)