Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit cfee57ed16c92a146db441294fb87783be444f36 @howiworkdaily committed Nov 11, 2008
Showing with 76 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +5 −0 README
  3. 0 __init__.py
  4. +24 −0 models.py
  5. 0 templatetags/__init__.py
  6. +43 −0 templatetags/freetext.py
@@ -0,0 +1,4 @@
+*.pyc
+*.bak
+*.db
+local_settings.py
5 README
@@ -0,0 +1,5 @@
+I love django-chunks but the name confused people.
+
+This is a fork of the popular django-chunks project with the addition of a field "active" which is a BooleanField for controlling the display of the content on the site.
+
+TODO: make a better README
No changes.
@@ -0,0 +1,24 @@
+from django.db import models
+
+class FreeText(models.Model):
+ """
+ A FreeText 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('key', help_text="A unique name for this FreeText of content", blank=False, maxlength=255, unique=True)
+ content = models.TextField('content', blank=True)
+ active = models.BooleanField("active", default=False)
+
+ class Admin:
+ list_display = ('key','active')
+ search_fields = ('key', 'content')
+
+ class Meta:
+ verbose_name = 'free text'
+ verbose_name_plural = 'free texts'
+
+ def __unicode__(self):
+ return u"%s" % (self.key,)
+
No changes.
@@ -0,0 +1,43 @@
+from django import template
+from django.db import models
+from django.core.cache import cache
+
+register = template.Library()
+
+FreeText = models.get_model('freetext', 'freetext')
+CACHE_PREFIX = "freetext_"
+
+def do_get_freetext(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 FreeTextNode(key[1:-1], cache_time)
+
+class FreeTextNode(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 = FreeText.objects.get(key=self.key, active=True)
+ cache.set(cache_key, c, int(self.cache_time))
+ content = c.content
+ except FreeText.DoesNotExist:
+ content = ''
+ return content
+
+register.tag('freetext', do_get_freetext)

0 comments on commit cfee57e

Please sign in to comment.