]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/blog/context_processors.py
fix: quick and dirty update to django 4.2
[sommitrealweird.git] / sommitrealweird / blog / context_processors.py
1 from django.conf import settings
2 from blog.models import BlogEntry, BlogSection
3 import re
4
5 def blog_feed(request):
6     return {
7         'BLOG_FEED_ROOT': settings.BLOG_FEED_ROOT,
8     } 
9
10 def content_breadcrumb(request):
11     path = request.path
12     if path[0:len(settings.BLOG_ROOT)] == settings.BLOG_ROOT:
13         breadcrumb = [{'url': u'/', 'title': u'Home'}, {'url': settings.BLOG_ROOT, 'title': u'Blog'},]
14
15         path = path[len(settings.BLOG_ROOT):]
16
17         parts = path.split('/')
18
19         if parts[0] == "section":
20             # Just need to add the section title to the breadcrumb *if* we
21             # actually have a section
22             if len(parts) > 1:
23                 try:
24                     section = BlogSection.objects.get(slug__exact=parts[1])
25                     breadcrumb.append({'url': section.get_absolute_url(), 'title': section.title})
26                 except:
27                     pass
28
29         if len(parts) == 5 and  \
30             parts[0].isdigit() and \
31             parts[1].isdigit() and \
32             parts[2].isdigit():
33                 # try getting the blog entry
34                 try:
35                     entry = BlogEntry.objects.get(publish_date__year=int(parts[0]), publish_date__month=int(parts[1]), publish_date__day=int(parts[2]), slug__exact=parts[3])
36                     breadcrumb.append({'url': entry.get_absolute_url(), 'title': entry.title})
37                 except:
38                     breadcrumb.append({'url': 'nowhere', 'title': 'screwed'})
39                     pass
40
41         return {
42             'content_breadcrumb': breadcrumb,
43         }
44     else:
45         return {}