]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blobdiff - sommitrealweird/bpcms/context_processors.py
add caching to the toplevel menu
[sommitrealweird.git] / sommitrealweird / bpcms / context_processors.py
index b464307381ffc634e3bc542d6d362dec1df48cfd..768d481b3ebce7d030e79f5cd091b11d20fd3cb1 100644 (file)
@@ -1,34 +1,39 @@
 from bpcms.models import Document, Folder
 from django.conf import settings
+from django.core.cache import cache
 import re
 
 def content_menu(request):
-    items = Document.objects.filter(folder__isnull=True, islive__exact=True)
-    menu_items = []
-    for item in items:
-        if item.slug != 'index':
-            menu_items.append(
-                {
-                    'title': item.title,
-                    'url': item.get_basic_url(),
-                }
-            )
-    folders = Folder.objects.filter(parent__isnull=True)
-    for folder in folders:
-        try:
-            items = Document.objects.filter(folder__exact=folder, islive__exact=True, slug__exact='index')
-            item = items[0]
-            menu_items.append(
-                {
-                    'title': item.title,
-                    'url': folder.get_basic_url(),
-                }
-            )
-        except:
-            pass
+    menu_items = cache.get('bpcms_mainmenu')
 
-    menu_items.sort( \
-        cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
+    if not menu_items:
+        items = Document.objects.filter(folder__isnull=True, islive__exact=True)
+        menu_items = []
+        for item in items:
+            if item.slug != 'index':
+                menu_items.append(
+                    {
+                        'title': item.title,
+                        'url': item.get_basic_url(),
+                    }
+                )
+        folders = Folder.objects.filter(parent__isnull=True)
+        for folder in folders:
+            try:
+                items = Document.objects.filter(folder__exact=folder, islive__exact=True, slug__exact='index')
+                item = items[0]
+                menu_items.append(
+                    {
+                        'title': item.title,
+                        'url': folder.get_basic_url(),
+                    }
+                )
+            except:
+                pass
+
+        menu_items.sort( \
+            cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
+        cache.set('bpcms_mainmenu', menu_items, 3600)
 
     return {
         'content_menu': menu_items,
@@ -89,3 +94,46 @@ def content_submenu(request):
     return {
         'content_submenu': menu_items,
     }
+
+def content_breadcrumb(request):
+    breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': settings.BPCMS_ROOT},]
+    # find where we are in the tree first
+    folderpath = request.path
+    folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
+    # right - that should be any prefix got rid of, now traverse the tree till
+    # we've got something
+    folder = None
+    folderpath = folderpath.split('/')
+    for part in folderpath:
+        try:
+            if folder:
+                f = Folder.objects.get(parent__exact=folder, slug__exact=part)
+            else:
+                f = Folder.objects.get(parent__isnull=True, slug__exact=part)
+            folder = f
+            try:
+                item = Document.objects.get(folder__exact=folder,
+                    slug__exact=u'index', islive__exact=True)
+                breadcrumb.append({'url': folder.get_basic_url(),
+                    'title': item.title})
+            except:
+                breadcrumb.append({'url': folder.get_basic_url(),
+                    'title': folder.title})
+        except:
+            # must have hit the end of the tree, lets see if we can get an
+            # item...
+            try:
+                if folder:
+                    item = Document.objects.get(folder__exact=folder, \
+                        slug__exact=part, islive__exact=True)
+                else:
+                    item = Document.objects.get(folder__isnull=True, \
+                        slug__exact=part, islive__exact=True)
+                breadcrumb.append({'url': item.get_basic_url(), 'title': item.title})
+            except:
+                pass
+            break
+
+    return {
+        'content_breadcrumb': breadcrumb,
+    }