1 from bpcms.models import Document, Folder
2 from django.conf import settings
3 from django.core.cache import cache
7 def content_menu(request):
9 print >>sys.stderr, "Trying to get menu items from cache"
11 menu_items = cache.get('bpcms_mainmenu')
14 print >>sys.stderr, "Generating menu items"
15 items = Document.objects.filter(folder__isnull=True, islive__exact=True)
18 print >>sys.stderr, "Found: %s" %(item.slug)
19 if item.slug != 'index':
23 'url': item.get_absolute_url(),
26 print >>sys.stderr, "Now looking for folders"
27 folders = Folder.objects.filter(parent__isnull=True)
28 for folder in folders:
29 print >>sys.stderr, "Found: %s" %(folder)
30 print >>sys.stderr, "folder path: %s" %(folder.get_absolute_url())
32 items = Document.objects.filter(folder__exact=folder, islive__exact=True, slug__exact='index')
37 'url': folder.get_absolute_url(),
41 print >>sys.stderr, "Failed to add: %s" %(folder)
45 cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
46 cache.set('bpcms_mainmenu', menu_items, 3600)
49 'content_menu': menu_items,
52 def content_submenu(request):
53 # find where we are in the tree first
54 folderpath = request.path
55 folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
56 # right - that should be any prefix got rid of, now traverse the tree till
58 menu_items = cache.get('bpcms_contentmenu_%s' %(folderpath))
62 folderpath = folderpath.split('/')
63 for part in folderpath:
66 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
68 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
73 # We have the folder (in theory)
76 items = Document.objects.filter(folder__exact=folder, \
79 if item.slug != 'index':
83 'url': item.get_absolute_url(),
86 folders = Folder.objects.filter(parent__exact=folder)
87 for folder in folders:
88 items = Document.objects.filter(folder__exact=folder, \
89 islive__exact=True, slug__exact='index')
94 'url': folder.get_absolute_url(),
99 cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
101 cache.set('bpcms_contentmenu_%s' %("/".join(folderpath)), menu_items)
104 'content_submenu': menu_items,
107 def content_breadcrumb(request):
108 if settings.BPCMS_ROOT == "/":
109 breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': "Home"}]
111 breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': settings.BPCMS_ROOT},]
112 # find where we are in the tree first
113 folderpath = request.path
114 folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
115 # right - that should be any prefix got rid of, now traverse the tree till
116 # we've got something
118 folderpath = folderpath.split('/')
119 for part in folderpath:
122 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
124 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
127 item = Document.objects.get(folder__exact=folder,
128 slug__exact=u'index', islive__exact=True)
129 breadcrumb.append({'url': folder.get_absolute_url(),
130 'title': item.title})
132 breadcrumb.append({'url': folder.get_absolute_url(),
133 'title': folder.title})
135 # must have hit the end of the tree, lets see if we can get an
139 item = Document.objects.get(folder__exact=folder, \
140 slug__exact=part, islive__exact=True)
142 item = Document.objects.get(folder__isnull=True, \
143 slug__exact=part, islive__exact=True)
144 breadcrumb.append({'url': item.get_absolute_url(), 'title': item.title})
150 'content_breadcrumb': breadcrumb,