]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/context_processors.py
fix: quick and dirty update to django 4.2
[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             key=lambda x: x['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
48     # right - that should be any prefix got rid of, now traverse the tree till
49     # we've got something
50     try:
51         menu_items = cache.get('bpcms_contentmenu_%s' %(folderpath))
52     except:
53         # fail on cache engine or bad key, ignore
54         return {
55             'content_submenu': {},
56         }
57
58     if not menu_items:
59         folder = None
60         folderpath = folderpath.split('/')
61         for part in folderpath:
62             try:
63                 if folder:
64                     f = Folder.objects.get(parent__exact=folder, slug__exact=part)
65                 else:
66                     f = Folder.objects.get(parent__isnull=True, slug__exact=part)
67                 folder = f
68             except:
69                 break
70
71         # We have the folder (in theory)
72         menu_items = []
73         if folder:
74             items = Document.objects.filter(folder__exact=folder, \
75                 islive__exact=True)
76             for item in items:
77                 if item.slug != 'index':
78                     menu_items.append(
79                         {
80                             'title': item.title,
81                             'url': item.get_absolute_url(),
82                         }
83                     )
84             folders = Folder.objects.filter(parent__exact=folder)
85             for folder in folders:
86                 items = Document.objects.filter(folder__exact=folder, \
87                     islive__exact=True, slug__exact='index')
88                 for item in items:
89                     menu_items.append(
90                         {
91                             'title': item.title,
92                             'url': folder.get_absolute_url(),
93                         }
94                     )
95
96         menu_items.sort( \
97             key=lambda x: x['title'].lower())
98
99         cache.set('bpcms_contentmenu_%s' %("/".join(folderpath)), menu_items)
100
101     return {
102         'content_submenu': menu_items,
103     }
104
105 def content_breadcrumb(request):
106     if settings.BPCMS_ROOT == "/":
107         breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': "Home"}]
108     else:
109         breadcrumb = [{'url': settings.BPCMS_ROOT, 'title': settings.BPCMS_ROOT},]
110     # find where we are in the tree first
111     folderpath = request.path
112     folderpath = re.sub(u'^%s' %(settings.BPCMS_ROOT), '', folderpath)
113     # right - that should be any prefix got rid of, now traverse the tree till
114     # we've got something
115     folder = None
116     folderpath = folderpath.split('/')
117     for part in folderpath:
118         try:
119             if folder:
120                 f = Folder.objects.get(parent__exact=folder, slug__exact=part)
121             else:
122                 f = Folder.objects.get(parent__isnull=True, slug__exact=part)
123             folder = f
124             try:
125                 item = Document.objects.get(folder__exact=folder,
126                     slug__exact=u'index', islive__exact=True)
127                 breadcrumb.append({'url': folder.get_absolute_url(),
128                     'title': item.title})
129             except:
130                 breadcrumb.append({'url': folder.get_absolute_url(),
131                     'title': folder.title})
132         except:
133             # must have hit the end of the tree, lets see if we can get an
134             # item...
135             try:
136                 if folder:
137                     item = Document.objects.get(folder__exact=folder, \
138                         slug__exact=part, islive__exact=True)
139                 else:
140                     item = Document.objects.get(folder__isnull=True, \
141                         slug__exact=part, islive__exact=True)
142                 breadcrumb.append({'url': item.get_absolute_url(), 'title': item.title})
143             except:
144                 pass
145             break
146
147     return {
148         'content_breadcrumb': breadcrumb,
149     }