Commits

Kai Diefenbach  committed e89386e

Improved caching

  • Participants
  • Parent commits 4a1e1f1

Comments (0)

Files changed (3)

File lfc/models.py

 from django.contrib.auth.models import User
 from django.contrib.contenttypes import generic
 from django.contrib.contenttypes.models import ContentType
+from django.core.cache import cache
 from django.core.urlresolvers import reverse
 from django.db import models
 from django.db.models.signals import post_syncdb
         """Overwrites django-permissions' has_permission in order to add LFC
         specific groups.
         """
+        # Get cache
+        cache_key = "object-%s-%s-%s" % (self.id, user.id, codename)
+        result = cache.get(cache_key)
+        if result:
+            return result
+
         # Every user is also anonymous user
         try:
             roles = [Role.objects.get(name="Anonymous")]
         except (AttributeError, Role.DoesNotExist):
             pass
 
-        return super(BaseContent, self).has_permission(user, codename, roles)
+        result = super(BaseContent, self).has_permission(user, codename, roles)
+
+        # set cache
+        cache.set(cache_key, result)
+        return result
 
     def is_active(self, user):
         """Returns True if now is between start and end date of the object.
         transitions = []
         for transition in state.transitions.all():
             permission = transition.permission
-            if permission is None or self.has_permission(user, permission.codename):
+            if permission is None or self.has_permission(obj, user, permission.codename):
                transitions.append(transition)
 
         return transitions
     creation_date = models.DateTimeField(_(u"Creation date"), auto_now_add=True)
     image = ImageWithThumbsField(_(u"Image"), upload_to="uploads",
         sizes=((60, 60), (100, 100), (200, 200), (400, 400), (600, 600), (800, 800)))
-        
+
     class Meta:
         ordering = ("position", )
 

File lfc/templatetags/lfc_tags.py

         lfc_context = context.get("lfc_context")
         request = context.get("request")
 
-        languages = []
-        for language in settings.LANGUAGES:
-            if lfc_context is None:
-                is_available = True
-            elif lfc_context.has_language(request, language[0]):
-                is_available = True
-            else:
-                is_available = False
+        # Get cache
+        if lfc_context:
+            cache_key = "languages-%s" % lfc_context.id
+        else:
+            cache_key = "languages-none"
 
-            languages.append({
-                "code" : language[0],
-                "name" : language[1],
-                "is_available" : is_available,
-            })
+        languages = cache.get(cache_key)
+
+        if languages is None:
+            languages = []
+            for language in settings.LANGUAGES:
+                if lfc_context is None:
+                    is_available = True
+                elif lfc_context.has_language(request, language[0]):
+                    is_available = True
+                else:
+                    is_available = False
+
+                languages.append({
+                    "code" : language[0],
+                    "name" : language[1],
+                    "is_available" : is_available,
+                })
+
+        # Set cache
+        cache.set(cache_key, languages)
 
         context["lfc_languages"] = languages
         return ''
     else:
         cache_key = "tabs-portal"
 
-    pages = cache.get(cache_key)
+    tabs = cache.get(cache_key)
 
-    if pages is None:
+    if tabs is None:
         tl_objs = lfc.utils.get_content_objects(
             request,
             language__in=(language, "0"),
             obj.current = obj in current_pages
             tabs.append(obj)
 
-        cache.set(cache_key, pages)
+        cache.set(cache_key, tabs)
 
     return {
         "language" : language,
         return { "objs" : [] }
 
     objs = obj.children.all().order_by("-publication_date")[:limit]
-    
+
     result = []
     for obj in objs:
         result.append(obj.get_content_object())
 
-    return { 
+    return {
         "objs" : result,
         "title" : title,
         "text" : text,
 
 @register.filter(name='field_value')
 def field_value(field):
-    """ Returns the value for this BoundField, as rendered in widgets. 
-    """ 
-    if field.form.is_bound: 
-        if isinstance(field.field, FileField) and field.data is None: 
-            val = field.form.initial.get(field.name, field.field.initial) 
-        else: 
-            val = field.data 
+    """ Returns the value for this BoundField, as rendered in widgets.
+    """
+    if field.form.is_bound:
+        if isinstance(field.field, FileField) and field.data is None:
+            val = field.form.initial.get(field.name, field.field.initial)
+        else:
+            val = field.data
     else:
         val = field.form.initial.get(field.name, field.field.initial)
         if callable(val):
     return val
 
 @register.filter(name='display_value')
-def display_value(field): 
-    """ 
-    Returns the displayed value for this BoundField, as rendered in widgets. 
-    """ 
+def display_value(field):
+    """
+    Returns the displayed value for this BoundField, as rendered in widgets.
+    """
     value = field_value(field)
-    if isinstance(field.field, CharField) and isinstance(field.field.widget, Textarea): 
+    if isinstance(field.field, CharField) and isinstance(field.field.widget, Textarea):
         value = """<div class="%s field-wrapper">%s</div>""" % (field.name, value)
 
-    if isinstance(field.field, ChoiceField): 
-        for (val, desc) in field.field.choices: 
-            if val == value: 
-                return desc 
+    if isinstance(field.field, ChoiceField):
+        for (val, desc) in field.field.choices:
+            if val == value:
+                return desc
     return value
 
 class PermissionComparisonNode(template.Node):

File lfc/utils/registration.py

 # django imports
+from django.core.cache import cache
 from django.contrib.contenttypes.models import ContentType
 from django.core.exceptions import ObjectDoesNotExist
 from django.db import IntegrityError
     else:
         type = obj_or_type
 
+    # Get cache
+    cache_key = "info-%s" % type
+    result = cache.get(cache_key)
+    if result:
+        return result
+
     try:
-        return ContentTypeRegistration.objects.get(type = type)
+        result = ContentTypeRegistration.objects.get(type = type)
     except ContentTypeRegistration.DoesNotExist:
-        return None
+        result = None
+
+    # Set cache
+    cache.set(cache_key, result)
+
+    return result
 
 def get_allowed_subtypes(obj_or_type=None):
     """Returns all allowed sub types for given object or type. Returns a list