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
.encoding
import force_unicode
8 from django
.utils
.safestring
import mark_safe
9 from django
.views
.generic
import date_based
10 from django
.utils
import datetime_safe
12 class CalendarPlugin(DatabrowsePlugin
):
13 def __init__(self
, field_names
=None):
14 self
.field_names
= field_names
16 def field_dict(self
, model
):
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.
22 if self
.field_names
is None:
23 return dict([(f
.name
, f
) for f
in model
._meta
.fields
if isinstance(f
, models
.DateField
)])
25 return dict([(f
.name
, f
) for f
in model
._meta
.fields
if isinstance(f
, models
.DateField
) and f
.name
in self
.field_names
])
27 def model_index_html(self
, request
, model
, site
):
28 fields
= self
.field_dict(model
)
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()]))
34 def urls(self
, plugin_name
, easy_instance_field
):
35 if isinstance(easy_instance_field
.field
, models
.DateField
):
36 d
= easy_instance_field
.raw_value
37 return [mark_safe(u
'%s%s/%s/%s/%s/%s/' % (
38 easy_instance_field
.model
.url(),
39 plugin_name
, easy_instance_field
.field
.name
,
41 datetime_safe
.new_date(d
).strftime('%b').lower(),
44 def model_view(self
, request
, model_databrowse
, url
):
45 self
.model
, self
.site
= model_databrowse
.model
, model_databrowse
.site
46 self
.fields
= self
.field_dict(self
.model
)
48 # If the model has no DateFields, there's no point in going further.
50 raise http
.Http404('The requested model has no calendars.')
53 return self
.homepage_view(request
)
54 url_bits
= url
.split('/')
55 if self
.fields
.has_key(url_bits
[0]):
56 return self
.calendar_view(request
, self
.fields
[url_bits
[0]], *url_bits
[1:])
58 raise http
.Http404('The requested page does not exist.')
60 def homepage_view(self
, request
):
61 easy_model
= EasyModel(self
.site
, self
.model
)
62 field_list
= self
.fields
.values()
63 field_list
.sort(lambda x
, y
: cmp(x
.verbose_name
, y
.verbose_name
))
64 return render_to_response('databrowse/calendar_homepage.html', {'root_url': self
.site
.root_url
, 'model': easy_model
, 'field_list': field_list
})
66 def calendar_view(self
, request
, field
, year
=None, month
=None, day
=None):
67 easy_model
= EasyModel(self
.site
, self
.model
)
68 queryset
= easy_model
.get_query_set()
69 extra_context
= {'root_url': self
.site
.root_url
, 'model': easy_model
, 'field': field
}
71 return date_based
.archive_day(request
, year
, month
, day
, queryset
, field
.name
,
72 template_name
='databrowse/calendar_day.html', allow_empty
=False, allow_future
=True,
73 extra_context
=extra_context
)
74 elif month
is not None:
75 return date_based
.archive_month(request
, year
, month
, queryset
, field
.name
,
76 template_name
='databrowse/calendar_month.html', allow_empty
=False, allow_future
=True,
77 extra_context
=extra_context
)
78 elif year
is not None:
79 return date_based
.archive_year(request
, year
, queryset
, field
.name
,
80 template_name
='databrowse/calendar_year.html', allow_empty
=False, allow_future
=True,
81 extra_context
=extra_context
)
83 return date_based
.archive_index(request
, queryset
, field
.name
,
84 template_name
='databrowse/calendar_main.html', allow_empty
=True, allow_future
=True,
85 extra_context
=extra_context
)
86 assert False, ('%s, %s, %s, %s' % (field
, year
, month
, day
))