2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2007-04-17 05:56:10 +08:00
|
|
|
from django import http
|
|
|
|
from django.db import models
|
|
|
|
from django.contrib.databrowse.datastructures import EasyModel
|
|
|
|
from django.contrib.databrowse.sites import DatabrowsePlugin
|
|
|
|
from django.shortcuts import render_to_response
|
2012-07-03 07:31:14 +08:00
|
|
|
from django.utils.html import format_html, format_html_join
|
2007-04-17 05:56:10 +08:00
|
|
|
from django.utils.text import capfirst
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import force_text
|
2011-04-02 21:55:03 +08:00
|
|
|
from django.views.generic import dates
|
2008-07-18 11:47:27 +08:00
|
|
|
from django.utils import datetime_safe
|
2007-04-17 05:56:10 +08:00
|
|
|
|
2011-04-02 21:55:03 +08:00
|
|
|
|
|
|
|
class DateViewMixin(object):
|
|
|
|
allow_empty = False
|
|
|
|
allow_future = True
|
|
|
|
root_url = None
|
|
|
|
model = None
|
|
|
|
field = None
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(DateViewMixin, self).get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'root_url': self.root_url,
|
|
|
|
'model': self.model,
|
|
|
|
'field': self.field
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class DayView(DateViewMixin, dates.DayArchiveView):
|
|
|
|
template_name = 'databrowse/calendar_day.html'
|
|
|
|
|
|
|
|
|
|
|
|
class MonthView(DateViewMixin, dates.MonthArchiveView):
|
|
|
|
template_name = 'databrowse/calendar_month.html'
|
|
|
|
|
|
|
|
|
|
|
|
class YearView(DateViewMixin, dates.YearArchiveView):
|
|
|
|
template_name = 'databrowse/calendar_year.html'
|
|
|
|
|
|
|
|
|
|
|
|
class IndexView(DateViewMixin, dates.ArchiveIndexView):
|
|
|
|
template_name = 'databrowse/calendar_main.html'
|
|
|
|
|
|
|
|
|
2007-04-17 05:56:10 +08:00
|
|
|
class CalendarPlugin(DatabrowsePlugin):
|
|
|
|
def __init__(self, field_names=None):
|
|
|
|
self.field_names = field_names
|
|
|
|
|
|
|
|
def field_dict(self, model):
|
|
|
|
"""
|
|
|
|
Helper function that returns a dictionary of all DateFields or
|
|
|
|
DateTimeFields in the given model. If self.field_names is set, it takes
|
|
|
|
take that into account when building the dictionary.
|
|
|
|
"""
|
|
|
|
if self.field_names is None:
|
|
|
|
return dict([(f.name, f) for f in model._meta.fields if isinstance(f, models.DateField)])
|
|
|
|
else:
|
|
|
|
return dict([(f.name, f) for f in model._meta.fields if isinstance(f, models.DateField) and f.name in self.field_names])
|
|
|
|
|
|
|
|
def model_index_html(self, request, model, site):
|
|
|
|
fields = self.field_dict(model)
|
|
|
|
if not fields:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2012-07-03 07:31:14 +08:00
|
|
|
return format_html('<p class="filter"><strong>View calendar by:</strong> {0}</p>',
|
|
|
|
format_html_join(', ', '<a href="calendars/{0}/">{1}</a>',
|
2012-07-21 16:00:10 +08:00
|
|
|
((f.name, force_text(capfirst(f.verbose_name))) for f in fields.values())))
|
2007-04-17 05:56:10 +08:00
|
|
|
|
|
|
|
def urls(self, plugin_name, easy_instance_field):
|
|
|
|
if isinstance(easy_instance_field.field, models.DateField):
|
2008-07-18 11:47:27 +08:00
|
|
|
d = easy_instance_field.raw_value
|
2012-07-03 07:33:44 +08:00
|
|
|
return ['%s%s/%s/%s/%s/%s/' % (
|
2007-11-14 20:58:53 +08:00
|
|
|
easy_instance_field.model.url(),
|
2007-04-17 05:56:10 +08:00
|
|
|
plugin_name, easy_instance_field.field.name,
|
2010-01-02 05:40:19 +08:00
|
|
|
str(d.year),
|
2008-07-18 11:47:27 +08:00
|
|
|
datetime_safe.new_date(d).strftime('%b').lower(),
|
2012-07-03 07:33:44 +08:00
|
|
|
d.day)]
|
2007-04-17 05:56:10 +08:00
|
|
|
|
|
|
|
def model_view(self, request, model_databrowse, url):
|
|
|
|
self.model, self.site = model_databrowse.model, model_databrowse.site
|
|
|
|
self.fields = self.field_dict(self.model)
|
|
|
|
|
|
|
|
# If the model has no DateFields, there's no point in going further.
|
|
|
|
if not self.fields:
|
|
|
|
raise http.Http404('The requested model has no calendars.')
|
|
|
|
|
|
|
|
if url is None:
|
|
|
|
return self.homepage_view(request)
|
|
|
|
url_bits = url.split('/')
|
2011-09-10 03:33:40 +08:00
|
|
|
if url_bits[0] in self.fields:
|
2007-04-17 05:56:10 +08:00
|
|
|
return self.calendar_view(request, self.fields[url_bits[0]], *url_bits[1:])
|
|
|
|
|
|
|
|
raise http.Http404('The requested page does not exist.')
|
|
|
|
|
|
|
|
def homepage_view(self, request):
|
|
|
|
easy_model = EasyModel(self.site, self.model)
|
2012-07-21 03:14:27 +08:00
|
|
|
field_list = list(self.fields.values())
|
2010-08-07 00:31:44 +08:00
|
|
|
field_list.sort(key=lambda k:k.verbose_name)
|
2011-04-02 21:55:03 +08:00
|
|
|
return render_to_response('databrowse/calendar_homepage.html', {
|
|
|
|
'root_url': self.site.root_url,
|
|
|
|
'model': easy_model,
|
|
|
|
'field_list': field_list
|
|
|
|
})
|
2007-04-17 05:56:10 +08:00
|
|
|
|
|
|
|
def calendar_view(self, request, field, year=None, month=None, day=None):
|
|
|
|
easy_model = EasyModel(self.site, self.model)
|
2011-04-02 21:55:03 +08:00
|
|
|
root_url = self.site.root_url
|
|
|
|
|
2007-04-17 05:56:10 +08:00
|
|
|
if day is not None:
|
2011-04-02 21:55:03 +08:00
|
|
|
return DayView.as_view(
|
|
|
|
year=year, month=month, day=day,
|
|
|
|
date_field=field.name,
|
|
|
|
queryset=easy_model.get_query_set(),
|
|
|
|
root_url=root_url,
|
|
|
|
model=easy_model,
|
|
|
|
field=field
|
|
|
|
)(request)
|
2007-04-17 05:56:10 +08:00
|
|
|
elif month is not None:
|
2011-04-02 21:55:03 +08:00
|
|
|
return MonthView.as_view(
|
|
|
|
year=year, month=month,
|
|
|
|
date_field=field.name,
|
|
|
|
queryset=easy_model.get_query_set(),
|
|
|
|
root_url=root_url,
|
|
|
|
model=easy_model,
|
|
|
|
field=field
|
|
|
|
)(request)
|
2007-04-17 05:56:10 +08:00
|
|
|
elif year is not None:
|
2011-04-02 21:55:03 +08:00
|
|
|
return YearView.as_view(
|
|
|
|
year=year,
|
|
|
|
date_field=field.name,
|
|
|
|
queryset=easy_model.get_query_set(),
|
|
|
|
root_url=root_url,
|
|
|
|
model=easy_model,
|
|
|
|
field=field
|
|
|
|
)(request)
|
2007-04-17 05:56:10 +08:00
|
|
|
else:
|
2011-04-02 21:55:03 +08:00
|
|
|
return IndexView.as_view(
|
|
|
|
date_field=field.name,
|
|
|
|
queryset=easy_model.get_query_set(),
|
|
|
|
root_url=root_url,
|
|
|
|
model=easy_model,
|
|
|
|
field=field
|
|
|
|
)(request)
|
|
|
|
|
2007-04-17 05:56:10 +08:00
|
|
|
assert False, ('%s, %s, %s, %s' % (field, year, month, day))
|