<s> from django import http <EOL> from django . db import models <EOL> from django . contrib . databrowse . datastructures import EasyModel <EOL> from django . contrib . databrowse . sites import DatabrowsePlugin <EOL> from django . shortcuts import render_to_response <EOL> from django . utils . text import capfirst <EOL> from django . utils . encoding import force_unicode <EOL> from django . utils . safestring import mark_safe <EOL> from django . views . generic import date_based <EOL> from django . utils import datetime_safe <EOL> class CalendarPlugin ( DatabrowsePlugin ) : <EOL> def __init__ ( self , field_names = None ) : <EOL> self . field_names = field_names <EOL> def field_dict ( self , model ) : <EOL> """<STR_LIT>""" <EOL> if self . field_names is None : <EOL> return dict ( [ ( f . name , f ) for f in model . _meta . fields if isinstance ( f , models . DateField ) ] ) <EOL> else : <EOL> return dict ( [ ( f . name , f ) for f in model . _meta . fields if isinstance ( f , models . DateField ) and f . name in self . field_names ] ) <EOL> def model_index_html ( self , request , model , site ) : <EOL> fields = self . field_dict ( model ) <EOL> if not fields : <EOL> return u'<STR_LIT>' <EOL> return mark_safe ( u'<STR_LIT>' % u'<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( f . name , force_unicode ( capfirst ( f . verbose_name ) ) ) for f in fields . values ( ) ] ) ) <EOL> def urls ( self , plugin_name , easy_instance_field ) : <EOL> if isinstance ( easy_instance_field . field , models . DateField ) : <EOL> d = easy_instance_field . raw_value <EOL> return [ mark_safe ( u'<STR_LIT>' % ( <EOL> easy_instance_field . model . url ( ) , <EOL> plugin_name , easy_instance_field . field . name , <EOL> d . year , <EOL> datetime_safe . new_date ( d ) . strftime ( '<STR_LIT>' ) . lower ( ) , <EOL> d . day ) ) ] <EOL> def model_view ( self , request , model_databrowse , url ) : <EOL> self . model , self . site = model_databrowse . model , model_databrowse . site <EOL> self . fields = self . field_dict ( self . model ) <EOL> if not self . fields : <EOL> raise http . Http404 ( '<STR_LIT>' ) <EOL> if url is None : <EOL> return self . homepage_view ( request ) <EOL> url_bits = url . split ( '<STR_LIT:/>' ) <EOL> if self . fields . has_key ( url_bits [ <NUM_LIT:0> ] ) : <EOL> return self . calendar_view ( request , self . fields [ url_bits [ <NUM_LIT:0> ] ] , * url_bits [ <NUM_LIT:1> : ] ) <EOL> raise http . Http404 ( '<STR_LIT>' ) <EOL> def homepage_view ( self , request ) : <EOL> easy_model = EasyModel ( self . site , self . model ) <EOL> field_list = self . fields . values ( ) <EOL> field_list . sort ( lambda x , y : cmp ( x . verbose_name , y . verbose_name ) ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : self . site . root_url , '<STR_LIT>' : easy_model , '<STR_LIT>' : field_list } ) <EOL> def calendar_view ( self , request , field , year = None , month = None , day = None ) : <EOL> easy_model = EasyModel ( self . site , self . model ) <EOL> queryset = easy_model . get_query_set ( ) <EOL> extra_context = { '<STR_LIT>' : self . site . root_url , '<STR_LIT>' : easy_model , '<STR_LIT>' : field } <EOL> if day is not None : <EOL> return date_based . archive_day ( request , year , month , day , queryset , field . name , <EOL> template_name = '<STR_LIT>' , allow_empty = False , allow_future = True , <EOL> extra_context = extra_context ) <EOL> elif month is not None : <EOL> return date_based . archive_month ( request , year , month , queryset , field . name , <EOL> template_name = '<STR_LIT>' , allow_empty = False , allow_future = True , <EOL> extra_context = extra_context ) <EOL> elif year is not None : <EOL> return date_based . archive_year ( request , year , queryset , field . name , <EOL> template_name = '<STR_LIT>' , allow_empty = False , allow_future = True , <EOL> extra_context = extra_context ) <EOL> else : <EOL> return date_based . archive_index ( request , queryset , field . name , <EOL> template_name = '<STR_LIT>' , allow_empty = True , allow_future = True , <EOL> extra_context = extra_context ) <EOL> assert False , ( '<STR_LIT>' % ( field , year , month , day ) ) </s>
