]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/context_processors.py
768d481b3ebce7d030e79f5cd091b11d20fd3cb1
[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     folder = None
49     folderpath = folderpath.split('/')
50     for part in folderpath:
51         try:
52             if folder:
53                 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
54             else:
55                 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
56             folder = f
57         except:
58             break
59
60     # We have the folder (in theory)
61     menu_items = []
62     if folder:
63         items = Document.objects.filter(folder__exact=folder, \
64             islive__exact=True)
65         for item in items:
66             if item.slug != 'index':
67                 menu_items.append(
68                     {
69                         'title': item.title,
70                         'url': item.get_basic_url(),
71                     }
72                 )
73         try:
74             folders = Folder.objects.filter(parent__exact=folder)
75         except:
76             pass
77         for folder in folders:
78             try:
79                 items = Document.objects.filter(folder__exact=folder, \
80                     islive__exact=True, slug__exact='index')
81                 for item in items:
82                     menu_items.append(
83                         {
84                             'title': item.title,
85                             'url': folder.get_basic_url(),
86                         }
87                     )
88             except:
89                 pass
90
91     menu_items.sort( \
92         cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
93
94     return {
95         'content_submenu': menu_items,
96     }
97
98 def content_breadcrumb(request):
99     breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': settings.BPCMS_ROOT},]
100     # find where we are in the tree first
101     folderpath = request.path
102     folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
103     # right - that should be any prefix got rid of, now traverse the tree till
104     # we've got something
105     folder = None
106     folderpath = folderpath.split('/')
107     for part in folderpath:
108         try:
109             if folder:
110                 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
111             else:
112                 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
113             folder = f
114             try:
115                 item = Document.objects.get(folder__exact=folder,
116                     slug__exact=u'index', islive__exact=True)
117                 breadcrumb.append({'url': folder.get_basic_url(),
118                     'title': item.title})
119             except:
120                 breadcrumb.append({'url': folder.get_basic_url(),
121                     'title': folder.title})
122         except:
123             # must have hit the end of the tree, lets see if we can get an
124             # item...
125             try:
126                 if folder:
127                     item = Document.objects.get(folder__exact=folder, \
128                         slug__exact=part, islive__exact=True)
129                 else:
130                     item = Document.objects.get(folder__isnull=True, \
131                         slug__exact=part, islive__exact=True)
132                 breadcrumb.append({'url': item.get_basic_url(), 'title': item.title})
133             except:
134                 pass
135             break
136
137     return {
138         'content_breadcrumb': breadcrumb,
139     }