]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/context_processors.py
2df38ae886c5bea4492c633be356b4c73c004a35
[sommitrealweird.git] / sommitrealweird / bpcms / context_processors.py
1 from bpcms.models import Document, Folder
2 from django.conf import settings
3 from django.core.cache import cache
4 import re
5 import sys
6
7 def content_menu(request):
8
9     print >>sys.stderr, "Trying to get menu items from cache"
10
11     menu_items = cache.get('bpcms_mainmenu')
12
13     if not menu_items:
14         print >>sys.stderr, "Generating menu items"
15         items = Document.objects.filter(folder__isnull=True, islive__exact=True)
16         menu_items = []
17         for item in items:
18             print >>sys.stderr, "Found: %s" %(item.slug)
19             if item.slug != 'index':
20                 menu_items.append(
21                     {
22                         'title': item.title,
23                         'url': item.get_absolute_url(),
24                     }
25                 )
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())
31             try:
32                 items = Document.objects.filter(folder__exact=folder, islive__exact=True, slug__exact='index')
33                 item = items[0]
34                 menu_items.append(
35                     {
36                         'title': item.title,
37                         'url': folder.get_absolute_url(),
38                     }
39                 )
40             except:
41                 print >>sys.stderr, "Failed to add: %s" %(folder)
42                 pass
43
44         menu_items.sort( \
45             cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
46         cache.set('bpcms_mainmenu', menu_items, 3600)
47
48     return {
49         'content_menu': menu_items,
50     }
51
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
57     # we've got something
58     menu_items = cache.get('bpcms_contentmenu_%s' %(folderpath))
59
60     if not menu_items:
61         folder = None
62         folderpath = folderpath.split('/')
63         for part in folderpath:
64             try:
65                 if folder:
66                     f = Folder.objects.get(parent__exact=folder, slug__exact=part)
67                 else:
68                     f = Folder.objects.get(parent__isnull=True, slug__exact=part)
69                 folder = f
70             except:
71                 break
72
73         # We have the folder (in theory)
74         menu_items = []
75         if folder:
76             items = Document.objects.filter(folder__exact=folder, \
77                 islive__exact=True)
78             for item in items:
79                 if item.slug != 'index':
80                     menu_items.append(
81                         {
82                             'title': item.title,
83                             'url': item.get_absolute_url(),
84                         }
85                     )
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')
90                 for item in items:
91                     menu_items.append(
92                         {
93                             'title': item.title,
94                             'url': folder.get_absolute_url(),
95                         }
96                     )
97
98         menu_items.sort( \
99             cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
100
101         cache.set('bpcms_contentmenu_%s' %("/".join(folderpath)), menu_items)
102
103     return {
104         'content_submenu': menu_items,
105     }
106
107 def content_breadcrumb(request):
108     if settings.BPCMS_ROOT == "/":
109         breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': "Home"}]
110     else:
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
117     folder = None
118     folderpath = folderpath.split('/')
119     for part in folderpath:
120         try:
121             if folder:
122                 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
123             else:
124                 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
125             folder = f
126             try:
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})
131             except:
132                 breadcrumb.append({'url': folder.get_absolute_url(),
133                     'title': folder.title})
134         except:
135             # must have hit the end of the tree, lets see if we can get an
136             # item...
137             try:
138                 if folder:
139                     item = Document.objects.get(folder__exact=folder, \
140                         slug__exact=part, islive__exact=True)
141                 else:
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})
145             except:
146                 pass
147             break
148
149     return {
150         'content_breadcrumb': breadcrumb,
151     }