tehfink avatar tehfink committed 61ca166

templatetag caching & version bump

Comments (0)

Files changed (3)

cmsplugin_faq/__init__.py

-VERSION = (0, 2, 2, 'beta')
+VERSION = (0, 2, 4, 'beta')
 __version__ = '.'.join(map(str, VERSION))

cmsplugin_faq/cms_plugins.py

 
 plugin_pool.register_plugin(CMSFaqListPlugin)
 
+
 #DOESN'T WORK WITH 2.0.2 & CMS_MODERATOR: published plugin `link` field is blank, so returned link plugin is random
 class CMSFaqEntryLinkPlugin(CMSPluginBase):
     """Links to a single FaqEntry plugin"""

cmsplugin_faq/templatetags/cmsplugin_faq_templatetags.py

 from django import template
 from cmsplugin_faq.models import FaqEntry
+from django.core.cache import cache
 register = template.Library()
 
 
+
 class LatestFAQsNode(template.Node):
     """
     parses & checks arguments passed to ``get_latest_faqs``; returns applicable Queryset of recently added CMSFaqPlugin models descendant from and including the current page
 
     def render(self, context):
 
-        #apparently publisher_is_draft has different meanings depending on the status of CMS_MODERATOR?
-        from django.conf import settings
-        if settings.CMS_MODERATOR:
-            #this seems logical
-            PUBLISHER_STATE = False
-        else:
-            #this does not seem logical
-            PUBLISHER_STATE = True
+        #shortcircuit for django admin
+        if context.has_key('current_page'):
+            page = context['current_page']
 
-        #the current django-cms Page of the template tag
-        page = context['current_page']
+            if page is 'dummy':
+                return ''
 
-        #shortcircuit for django admin
-        if page is not 'dummy':
+            #check cache first
+            if cache.get('cmsplugin_faq_templatetags_get_latest_faqs'):                                     #if latest_pages exists in cache, return it immediately
+                 context[self.varname] = cache.get('cmsplugin_faq_templatetags_get_latest_faqs')
+            else:
+                #apparently publisher_is_draft has different meanings depending on the status of CMS_MODERATOR?
+                from django.conf import settings
+                if settings.CMS_MODERATOR:
+                    #this seems logical
+                    PUBLISHER_STATE = False
+                else:
+                    #this does not seem logical
+                    PUBLISHER_STATE = True
 
-            #get published descendant Pages of the current Page
-            subpages = page.get_descendants().filter(publisher_is_draft=PUBLISHER_STATE)
+                #get published descendant Pages of the current Page
+                subpages = page.get_descendants().filter(publisher_is_draft=PUBLISHER_STATE)
 
-            #list of all published faq plugins for descendant and current page
-            allfaqs= []
+                #list of all published faq plugins for descendant and current page
+                allfaqs= []
 
-            #get published plugins for this page
-            for faq in page.cmsplugin_set.filter(plugin_type='CMSFaqEntryPlugin', publisher_is_draft=PUBLISHER_STATE):
-                allfaqs.append(faq)
+                #get published plugins for this page
+                for faq in page.cmsplugin_set.filter(plugin_type='CMSFaqEntryPlugin', publisher_is_draft=PUBLISHER_STATE):
+                    allfaqs.append(faq)
 
-            #get published plugins for each subpage
-            for subpage in subpages:
-                for subpagefaq in subpage.cmsplugin_set.filter(plugin_type='CMSFaqEntryPlugin', publisher_is_draft=PUBLISHER_STATE):
-                    allfaqs.append(subpagefaq)
+                #get published plugins for each subpage
+                for subpage in subpages:
+                    for subpagefaq in subpage.cmsplugin_set.filter(plugin_type='CMSFaqEntryPlugin', publisher_is_draft=PUBLISHER_STATE):
+                        allfaqs.append(subpagefaq)
 
-#            import ipdb; ipdb.set_trace()
+                #shortened list according to given argument
+                #putting this here for performance reasons?
+                context[self.varname] = allfaqs[:self.num]
 
-            #shortened list according to given argument
-            faqs = allfaqs[:self.num]
-
-            context[self.varname] = faqs
+                cache.set('cmsplugin_faq_templatetags_get_latest_faqs', context[self.varname])               #add latest_pages to the django cache
 
         return ''
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.