]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/context_processors.py
b464307381ffc634e3bc542d6d362dec1df48cfd
[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     }