Commits

hakanw committed e05efd3

Add support for translated chunks and let chunks be tied to a site.

  • Participants
  • Parent commits 969e9f3

Comments (0)

Files changed (3)

File chunks/admin.py

 from models import Chunk
 
 class ChunkAdmin(admin.ModelAdmin):
-  list_display = ('key',)
+  list_display = ('key', 'lang_code', 'site')
   search_fields = ('key', 'content')
 
 admin.site.register(Chunk, ChunkAdmin)

File chunks/models.py

 from django.db import models
+from django.contrib.sites.models import Site
+from django.conf import settings
+from django.utils.translation import ugettext_lazy as _
 
 class Chunk(models.Model):
     """
     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)
+    key = models.CharField(help_text="A unique name for this chunk of content", blank=False, max_length=255)
     content = models.TextField(blank=True)
+    lang_code = models.CharField(verbose_name=_(u"language"), help_text="Language code, if this chunk is translated. Same format as LANGUAGE_CODE setting, e.g. sv-se, or de-de, etc.", blank=True, max_length=5, default=settings.LANGUAGE_CODE)
+    site = models.ForeignKey(Site, default=settings.SITE_ID, blank=True, null=True, verbose_name=_('site'))
+
+    class Meta:
+        unique_together = (('key', 'lang_code', 'site'),)
 
     def __unicode__(self):
         return u"%s" % (self.key,)

File chunks/templatetags/chunks.py

 from django import template
 from django.db import models
 from django.core.cache import cache
+from django.contrib.sites.models import Site
 
 register = template.Library()
 
     return ChunkNode(key[1:-1], cache_time)
     
 class ChunkNode(template.Node):
-    def __init__(self, key, cache_time=0):
+    def __init__(self, key, language, cache_time=0):
        self.key = key
        self.cache_time = cache_time
+       self.lang_code = template.Variable('LANGUAGE_CODE')
     
     def render(self, context):
         try:
-            cache_key = CACHE_PREFIX + self.key
+            lang = self.lang_code.resolve(context)
+            site = Site.objects.get_current().id
+            cache_key = CACHE_PREFIX + self.key + lang + str(site)
             c = cache.get(cache_key)
             if c is None:
-                c = Chunk.objects.get(key=self.key)
+                c = Chunk.objects.get(key=self.key, lang_code=lang, site=site)
                 cache.set(cache_key, c, int(self.cache_time))
             content = c.content
         except Chunk.DoesNotExist:
             content = ''
         return content
-        
+
 register.tag('chunk', do_get_chunk)