]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/context_processors.py
Make menu items more seperated
[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
6 def content_menu(request):
7     menu_items = cache.get('bpcms_mainmenu')
8
9     if not menu_items:
10         items = Document.objects.filter(folder__isnull=True, islive__exact=True)
11         menu_items = []
12         for item in items:
13             if item.slug != 'index':
14                 menu_items.append(
15                     {
16                         'title': item.title,
17                         'url': item.get_basic_url(),
18                     }
19                 )
20         folders = Folder.objects.filter(parent__isnull=True)
21         for folder in folders:
22             try:
23                 items = Document.objects.filter(folder__exact=folder, islive__exact=True, slug__exact='index')
24                 item = items[0]
25                 menu_items.append(
26                     {
27                         'title': item.title,
28                         'url': folder.get_basic_url(),
29                     }
30                 )
31             except:
32                 pass
33
34         menu_items.sort( \
35             cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
36         cache.set('bpcms_mainmenu', menu_items, 3600)
37
38     return {
39         'content_menu': menu_items,
40     }
41
42 def content_submenu(request):
43     # find where we are in the tree first
44     folderpath = request.path
45     folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
46     # right - that should be any prefix got rid of, now traverse the tree till
47     # we've got something
48     menu_items = cache.get('bpcms_contentmenu_%s' %(folderpath))
49
50     if not menu_items:
51         folder = None
52         folderpath = folderpath.split('/')
53         for part in folderpath:
54             try:
55                 if folder:
56                     f = Folder.objects.get(parent__exact=folder, slug__exact=part)
57                 else:
58                     f = Folder.objects.get(parent__isnull=True, slug__exact=part)
59                 folder = f
60             except:
61                 break
62
63         # We have the folder (in theory)
64         menu_items = []
65         if folder:
66             items = Document.objects.filter(folder__exact=folder, \
67                 islive__exact=True)
68             for item in items:
69                 if item.slug != 'index':
70                     menu_items.append(
71                         {
72                             'title': item.title,
73                             'url': item.get_basic_url(),
74                         }
75                     )
76             try:
77                 folders = Folder.objects.filter(parent__exact=folder)
78             except:
79                 pass
80             for folder in folders:
81                 try:
82                     items = Document.objects.filter(folder__exact=folder, \
83                         islive__exact=True, slug__exact='index')
84                     for item in items:
85                         menu_items.append(
86                             {
87                                 'title': item.title,
88                                 'url': folder.get_basic_url(),
89                             }
90                         )
91                 except:
92                     pass
93
94         menu_items.sort( \
95             cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
96
97         cache.set('bpcms_contentmenu_%s' %(folderpath), menu_items)
98
99     return {
100         'content_submenu': menu_items,
101     }
102
103 def content_breadcrumb(request):
104     breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': settings.BPCMS_ROOT},]
105     # find where we are in the tree first
106     folderpath = request.path
107     folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
108     # right - that should be any prefix got rid of, now traverse the tree till
109     # we've got something
110     folder = None
111     folderpath = folderpath.split('/')
112     for part in folderpath:
113         try:
114             if folder:
115                 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
116             else:
117                 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
118             folder = f
119             try:
120                 item = Document.objects.get(folder__exact=folder,
121                     slug__exact=u'index', islive__exact=True)
122                 breadcrumb.append({'url': folder.get_basic_url(),
123                     'title': item.title})
124             except:
125                 breadcrumb.append({'url': folder.get_basic_url(),
126                     'title': folder.title})
127         except:
128             # must have hit the end of the tree, lets see if we can get an
129             # item...
130             try:
131                 if folder:
132                     item = Document.objects.get(folder__exact=folder, \
133                         slug__exact=part, islive__exact=True)
134                 else:
135                     item = Document.objects.get(folder__isnull=True, \
136                         slug__exact=part, islive__exact=True)
137                 breadcrumb.append({'url': item.get_basic_url(), 'title': item.title})
138             except:
139                 pass
140             break
141
142     return {
143         'content_breadcrumb': breadcrumb,
144     }