Commits

tehfink committed b0f07ac

now caching nav tree, should be much faster

  • Participants
  • Parent commits 510c201
  • Branches cms-2.X

Comments (0)

Files changed (1)

cmsplugin_photologue/navigation.py

 from cms.utils.navigation import NavigationNode
 from photologue.models import Gallery, Photo
 from photologue.urls import gallery_args, photo_args
+from django.core.cache import cache
 
 
 def get_nodes(request):
     """ creates mptt style parent-child list for extending django-cms navigation (menu & breadcrumb); structured on photologue.urls """
 
-    res = []                                                        #NavigationNode result list
+    if cache.get('cmsplugin_photologue_get_nodes'):                 #if nav tree exists in cache, return it immediately
+        return cache.get('cmsplugin_photologue_get_nodes')
+
+    res = []                                                        #create NavigationNode result list
 
     for q in gallery_args, photo_args:                              #get public Galleries & Photos
         if q.has_key('queryset'):                                   #use photologue defaults
             try:                                                    #get the name of the queryset's model
                 model = q['queryset'][0].__class__.__name__.lower()
             except IndexError:                                      #abort if empty
-                return res
+                return res                                          #return empty navigation tree
         else:
-            return res
+            return res                                              #if no queryset, return empty navigation tree
 
 
         #create the base path of the URL ('gallery/' or 'photo/')
         #path example: request.META['PATH_INFO'] = u'/galleries/photo/page/2/'  or  u'/galleries/gallery/page/1/'
         try:
             #page_path gets the relevant parts of the path for paging
-            page_path = request.META['PATH_INFO'].split('/')[-4:-1]     #eg: '/mygalleries/gallery/page/1/' ->  ['gallery', 'page', '1']
+            #eg: '/mygalleries/gallery/page/1/' ->  ['gallery', 'page', '1']
+            page_path = request.META['PATH_INFO'].split('/')[-4:-1]
             #execute only if page_path is a photologue paged url for this model
             assert len(page_path) == 3 and [model, 'page', str(int(page_path[2]))] == page_path
             #name the node with a string, reverse to the paged named url, and pass the page value from page_path
         #create NavigationNodes for the queryset
         for item in queryset:
 
-            years_done = []                                             #keeps track of published item years
+            years_done = []                                         #keeps track of published item years
             months_done = []
             days_done = []
             slug_done = []
                 item_node = NavigationNode(item.title, reverse('pl-%s' % model, kwargs=dict(slug=item.title_slug)))
                 base_node.childrens.append(item_node)
 
-        res.append(base_node)
+        res.append(base_node)                                       #add base node ('gallery/' or 'photo/') to nav tree
+
+    cache.set('cmsplugin_photologue_get_nodes', res)                #add nav tree to the django cache
 
     return res