]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/context_processors.py
Make posted comments give a better message
[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_absolute_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_absolute_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_absolute_url(),
74                         }
75                     )
76             folders = Folder.objects.filter(parent__exact=folder)
77             for folder in folders:
78                 items = Document.objects.filter(folder__exact=folder, \
79                     islive__exact=True, slug__exact='index')
80                 for item in items:
81                     menu_items.append(
82                         {
83                             'title': item.title,
84                             'url': folder.get_absolute_url(),
85                         }
86                     )
87
88         menu_items.sort( \
89             cmp=lambda x,y: cmp(x['title'].lower(), y['title'].lower()))
90
91         cache.set('bpcms_contentmenu_%s' %(folderpath), menu_items)
92
93     return {
94         'content_submenu': menu_items,
95     }
96
97 def content_breadcrumb(request):
98     if settings.BPCMS_ROOT == "/":
99         breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': "Home"}]
100     else:
101         breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': settings.BPCMS_ROOT},]
102     # find where we are in the tree first
103     folderpath = request.path
104     folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
105     # right - that should be any prefix got rid of, now traverse the tree till
106     # we've got something
107     folder = None
108     folderpath = folderpath.split('/')
109     for part in folderpath:
110         try:
111             if folder:
112                 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
113             else:
114                 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
115             folder = f
116             try:
117                 item = Document.objects.get(folder__exact=folder,
118                     slug__exact=u'index', islive__exact=True)
119                 breadcrumb.append({'url': folder.get_absolute_url(),
120                     'title': item.title})
121             except:
122                 breadcrumb.append({'url': folder.get_absolute_url(),
123                     'title': folder.title})
124         except:
125             # must have hit the end of the tree, lets see if we can get an
126             # item...
127             try:
128                 if folder:
129                     item = Document.objects.get(folder__exact=folder, \
130                         slug__exact=part, islive__exact=True)
131                 else:
132                     item = Document.objects.get(folder__isnull=True, \
133                         slug__exact=part, islive__exact=True)
134                 breadcrumb.append({'url': item.get_absolute_url(), 'title': item.title})
135             except:
136                 pass
137             break
138
139     return {
140         'content_breadcrumb': breadcrumb,
141     }