]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/context_processors.py
First draft image gallery
[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     if settings.BPCMS_ROOT == "/":
105         breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': "Home"}]
106     else:
107         breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': settings.BPCMS_ROOT},]
108     # find where we are in the tree first
109     folderpath = request.path
110     folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
111     # right - that should be any prefix got rid of, now traverse the tree till
112     # we've got something
113     folder = None
114     folderpath = folderpath.split('/')
115     for part in folderpath:
116         try:
117             if folder:
118                 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
119             else:
120                 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
121             folder = f
122             try:
123                 item = Document.objects.get(folder__exact=folder,
124                     slug__exact=u'index', islive__exact=True)
125                 breadcrumb.append({'url': folder.get_basic_url(),
126                     'title': item.title})
127             except:
128                 breadcrumb.append({'url': folder.get_basic_url(),
129                     'title': folder.title})
130         except:
131             # must have hit the end of the tree, lets see if we can get an
132             # item...
133             try:
134                 if folder:
135                     item = Document.objects.get(folder__exact=folder, \
136                         slug__exact=part, islive__exact=True)
137                 else:
138                     item = Document.objects.get(folder__isnull=True, \
139                         slug__exact=part, islive__exact=True)
140                 breadcrumb.append({'url': item.get_basic_url(), 'title': item.title})
141             except:
142                 pass
143             break
144
145     return {
146         'content_breadcrumb': breadcrumb,
147     }