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