|
1 from django import http |
|
2 from django.db import models |
|
3 from django.contrib.databrowse.datastructures import EasyModel |
|
4 from django.contrib.databrowse.sites import DatabrowsePlugin |
|
5 from django.shortcuts import render_to_response |
|
6 from django.utils.text import capfirst |
|
7 from django.utils.translation import get_date_formats |
|
8 from django.utils.encoding import force_unicode |
|
9 from django.utils.safestring import mark_safe |
|
10 from django.views.generic import date_based |
|
11 |
|
12 class CalendarPlugin(DatabrowsePlugin): |
|
13 def __init__(self, field_names=None): |
|
14 self.field_names = field_names |
|
15 |
|
16 def field_dict(self, model): |
|
17 """ |
|
18 Helper function that returns a dictionary of all DateFields or |
|
19 DateTimeFields in the given model. If self.field_names is set, it takes |
|
20 take that into account when building the dictionary. |
|
21 """ |
|
22 if self.field_names is None: |
|
23 return dict([(f.name, f) for f in model._meta.fields if isinstance(f, models.DateField)]) |
|
24 else: |
|
25 return dict([(f.name, f) for f in model._meta.fields if isinstance(f, models.DateField) and f.name in self.field_names]) |
|
26 |
|
27 def model_index_html(self, request, model, site): |
|
28 fields = self.field_dict(model) |
|
29 if not fields: |
|
30 return u'' |
|
31 return mark_safe(u'<p class="filter"><strong>View calendar by:</strong> %s</p>' % \ |
|
32 u', '.join(['<a href="calendars/%s/">%s</a>' % (f.name, force_unicode(capfirst(f.verbose_name))) for f in fields.values()])) |
|
33 |
|
34 def urls(self, plugin_name, easy_instance_field): |
|
35 if isinstance(easy_instance_field.field, models.DateField): |
|
36 return [mark_safe(u'%s%s/%s/%s/%s/%s/' % ( |
|
37 easy_instance_field.model.url(), |
|
38 plugin_name, easy_instance_field.field.name, |
|
39 easy_instance_field.raw_value.year, |
|
40 easy_instance_field.raw_value.strftime('%b').lower(), |
|
41 easy_instance_field.raw_value.day))] |
|
42 |
|
43 def model_view(self, request, model_databrowse, url): |
|
44 self.model, self.site = model_databrowse.model, model_databrowse.site |
|
45 self.fields = self.field_dict(self.model) |
|
46 |
|
47 # If the model has no DateFields, there's no point in going further. |
|
48 if not self.fields: |
|
49 raise http.Http404('The requested model has no calendars.') |
|
50 |
|
51 if url is None: |
|
52 return self.homepage_view(request) |
|
53 url_bits = url.split('/') |
|
54 if self.fields.has_key(url_bits[0]): |
|
55 return self.calendar_view(request, self.fields[url_bits[0]], *url_bits[1:]) |
|
56 |
|
57 raise http.Http404('The requested page does not exist.') |
|
58 |
|
59 def homepage_view(self, request): |
|
60 easy_model = EasyModel(self.site, self.model) |
|
61 field_list = self.fields.values() |
|
62 field_list.sort(lambda x, y: cmp(x.verbose_name, y.verbose_name)) |
|
63 return render_to_response('databrowse/calendar_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list}) |
|
64 |
|
65 def calendar_view(self, request, field, year=None, month=None, day=None): |
|
66 easy_model = EasyModel(self.site, self.model) |
|
67 queryset = easy_model.get_query_set() |
|
68 extra_context = {'root_url': self.site.root_url, 'model': easy_model, 'field': field} |
|
69 if day is not None: |
|
70 return date_based.archive_day(request, year, month, day, queryset, field.name, |
|
71 template_name='databrowse/calendar_day.html', allow_empty=False, allow_future=True, |
|
72 extra_context=extra_context) |
|
73 elif month is not None: |
|
74 return date_based.archive_month(request, year, month, queryset, field.name, |
|
75 template_name='databrowse/calendar_month.html', allow_empty=False, allow_future=True, |
|
76 extra_context=extra_context) |
|
77 elif year is not None: |
|
78 return date_based.archive_year(request, year, queryset, field.name, |
|
79 template_name='databrowse/calendar_year.html', allow_empty=False, allow_future=True, |
|
80 extra_context=extra_context) |
|
81 else: |
|
82 return date_based.archive_index(request, queryset, field.name, |
|
83 template_name='databrowse/calendar_main.html', allow_empty=True, allow_future=True, |
|
84 extra_context=extra_context) |
|
85 assert False, ('%s, %s, %s, %s' % (field, year, month, day)) |