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