1 """Breadcrumb module for Zinnia templatetags"""
2 import re
3 from datetime import datetime
4
5 from django.core.urlresolvers import reverse
6 from django.utils.translation import ugettext as _
7
8
10 """Part of the Breadcrumbs"""
12 self.name = name
13 self.url = url
14
15
17 """Crumb for a year"""
18 year = creation_date.strftime('%Y')
19 return Crumb(year, reverse('zinnia_entry_archive_year',
20 args=[year]))
21
22
24 """Crumb for a month"""
25 year = creation_date.strftime('%Y')
26 month = creation_date.strftime('%m')
27 month_text = creation_date.strftime('%b').capitalize()
28 return Crumb(month_text, reverse('zinnia_entry_archive_month',
29 args=[year, month]))
30
31
39
40
41 ZINNIA_ROOT_URL = lambda: reverse('zinnia_entry_archive_index')
42
43 MODEL_BREADCRUMBS = {'Tag': lambda x: [Crumb(_('Tags'),
44 reverse('zinnia_tag_list')),
45 Crumb(x.name)],
46 'Author': lambda x: [Crumb(_('Authors'),
47 reverse('zinnia_author_list')),
48 Crumb(x.username)],
49 'Category': lambda x: [Crumb(_('Categories'),
50 reverse('zinnia_category_list'))] + \
51 [Crumb(anc.title,
52 anc.get_absolute_url())
53 for anc in x.get_ancestors()] + \
54 [Crumb(x.title)],
55 'Entry': lambda x: [year_crumb(x.creation_date),
56 month_crumb(x.creation_date),
57 day_crumb(x.creation_date),
58 Crumb(x.title)]}
59
60 DATE_REGEXP = re.compile(
61 r'.*(?P<year>\d{4})/(?P<month>\d{2})?/(?P<day>\d{2})?.*')
62
63
65 """Build a semi-hardcoded breadcrumbs
66 based of the model's url handled by Zinnia"""
67 breadcrumbs = []
68
69 if root_name:
70 breadcrumbs.append(Crumb(root_name, ZINNIA_ROOT_URL()))
71
72 if model_instance is not None:
73 key = model_instance.__class__.__name__
74 if key in MODEL_BREADCRUMBS:
75 breadcrumbs.extend(MODEL_BREADCRUMBS[key](model_instance))
76 return breadcrumbs
77
78 date_match = DATE_REGEXP.match(path)
79 if date_match:
80 date_dict = date_match.groupdict()
81 path_date = datetime(
82 int(date_dict['year']),
83 date_dict.get('month') is not None and \
84 int(date_dict.get('month')) or 1,
85 date_dict.get('day') is not None and \
86 int(date_dict.get('day')) or 1)
87
88 date_breadcrumbs = [year_crumb(path_date)]
89 if date_dict['month']:
90 date_breadcrumbs.append(month_crumb(path_date))
91 if date_dict['day']:
92 date_breadcrumbs.append(day_crumb(path_date))
93 breadcrumbs.extend(date_breadcrumbs)
94
95 return breadcrumbs
96
97 url_components = [comp for comp in
98 path.replace(ZINNIA_ROOT_URL(), '').split('/') if comp]
99 if len(url_components):
100 breadcrumbs.append(Crumb(_(url_components[-1].capitalize())))
101
102 return breadcrumbs
103