<s> from django . template import loader , RequestContext <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . xheaders import populate_xheaders <EOL> from django . db . models . fields import DateTimeField <EOL> from django . http import Http404 , HttpResponse <EOL> import datetime , time <EOL> def archive_index ( request , queryset , date_field , num_latest = <NUM_LIT:15> , <EOL> template_name = None , template_loader = loader , <EOL> extra_context = None , allow_empty = False , context_processors = None , <EOL> mimetype = None , allow_future = False ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> model = queryset . model <EOL> if not allow_future : <EOL> queryset = queryset . filter ( ** { '<STR_LIT>' % date_field : datetime . datetime . now ( ) } ) <EOL> date_list = queryset . dates ( date_field , '<STR_LIT>' ) [ : : - <NUM_LIT:1> ] <EOL> if not date_list and not allow_empty : <EOL> raise Http404 , "<STR_LIT>" % model . _meta . verbose_name <EOL> if date_list and num_latest : <EOL> latest = queryset . order_by ( '<STR_LIT:->' + date_field ) [ : num_latest ] <EOL> else : <EOL> latest = None <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> '<STR_LIT>' : date_list , <EOL> '<STR_LIT>' : latest , <EOL> } , context_processors ) <EOL> for key , value in extra_context . items ( ) : <EOL> if callable ( value ) : <EOL> c [ key ] = value ( ) <EOL> else : <EOL> c [ key ] = value <EOL> return HttpResponse ( t . render ( c ) , mimetype = mimetype ) <EOL> def archive_year ( request , year , queryset , date_field , template_name = None , <EOL> template_loader = loader , extra_context = None , allow_empty = False , <EOL> context_processors = None , template_object_name = '<STR_LIT:object>' , mimetype = None , <EOL> make_object_list = False , allow_future = False ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> model = queryset . model <EOL> now = datetime . datetime . now ( ) <EOL> lookup_kwargs = { '<STR_LIT>' % date_field : year } <EOL> if int ( year ) >= now . year and not allow_future : <EOL> lookup_kwargs [ '<STR_LIT>' % date_field ] = now <EOL> date_list = queryset . filter ( ** lookup_kwargs ) . dates ( date_field , '<STR_LIT>' ) <EOL> if not date_list and not allow_empty : <EOL> raise Http404 <EOL> if make_object_list : <EOL> object_list = queryset . filter ( ** lookup_kwargs ) . order_by ( date_field ) <EOL> else : <EOL> object_list = [ ] <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> '<STR_LIT>' : date_list , <EOL> '<STR_LIT>' : year , <EOL> '<STR_LIT>' % template_object_name : object_list , <EOL> } , context_processors ) <EOL> for key , value in extra_context . items ( ) : <EOL> if callable ( value ) : <EOL> c [ key ] = value ( ) <EOL> else : <EOL> c [ key ] = value <EOL> return HttpResponse ( t . render ( c ) , mimetype = mimetype ) <EOL> def archive_month ( request , year , month , queryset , date_field , <EOL> month_format = '<STR_LIT>' , template_name = None , template_loader = loader , <EOL> extra_context = None , allow_empty = False , context_processors = None , <EOL> template_object_name = '<STR_LIT:object>' , mimetype = None , allow_future = False ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> try : <EOL> date = datetime . date ( * time . strptime ( year + month , '<STR_LIT>' + month_format ) [ : <NUM_LIT:3> ] ) <EOL> except ValueError : <EOL> raise Http404 <EOL> model = queryset . model <EOL> now = datetime . datetime . now ( ) <EOL> first_day = date . replace ( day = <NUM_LIT:1> ) <EOL> if first_day . month == <NUM_LIT:12> : <EOL> last_day = first_day . replace ( year = first_day . year + <NUM_LIT:1> , month = <NUM_LIT:1> ) <EOL> else : <EOL> last_day = first_day . replace ( month = first_day . month + <NUM_LIT:1> ) <EOL> lookup_kwargs = { '<STR_LIT>' % date_field : ( first_day , last_day ) } <EOL> if last_day >= now . date ( ) and not allow_future : <EOL> lookup_kwargs [ '<STR_LIT>' % date_field ] = now <EOL> object_list = queryset . filter ( ** lookup_kwargs ) <EOL> if not object_list and not allow_empty : <EOL> raise Http404 <EOL> if allow_future : <EOL> next_month = last_day + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> elif last_day < datetime . date . today ( ) : <EOL> next_month = last_day + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> else : <EOL> next_month = None <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> '<STR_LIT>' % template_object_name : object_list , <EOL> '<STR_LIT>' : date , <EOL> '<STR_LIT>' : next_month , <EOL> '<STR_LIT>' : first_day - datetime . timedelta ( days = <NUM_LIT:1> ) , <EOL> } , context_processors ) <EOL> for key , value in extra_context . items ( ) : <EOL> if callable ( value ) : <EOL> c [ key ] = value ( ) <EOL> else : <EOL> c [ key ] = value <EOL> return HttpResponse ( t . render ( c ) , mimetype = mimetype ) <EOL> def archive_week ( request , year , week , queryset , date_field , <EOL> template_name = None , template_loader = loader , <EOL> extra_context = None , allow_empty = True , context_processors = None , <EOL> template_object_name = '<STR_LIT:object>' , mimetype = None , allow_future = False ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> try : <EOL> date = datetime . date ( * time . strptime ( year + '<STR_LIT>' + week , '<STR_LIT>' ) [ : <NUM_LIT:3> ] ) <EOL> except ValueError : <EOL> raise Http404 <EOL> model = queryset . model <EOL> now = datetime . datetime . now ( ) <EOL> first_day = date <EOL> last_day = date + datetime . timedelta ( days = <NUM_LIT:7> ) <EOL> lookup_kwargs = { '<STR_LIT>' % date_field : ( first_day , last_day ) } <EOL> if last_day >= now . date ( ) and not allow_future : <EOL> lookup_kwargs [ '<STR_LIT>' % date_field ] = now <EOL> object_list = queryset . filter ( ** lookup_kwargs ) <EOL> if not object_list and not allow_empty : <EOL> raise Http404 <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> '<STR_LIT>' % template_object_name : object_list , <EOL> '<STR_LIT>' : date , <EOL> } ) <EOL> for key , value in extra_context . items ( ) : <EOL> if callable ( value ) : <EOL> c [ key ] = value ( ) <EOL> else : <EOL> c [ key ] = value <EOL> return HttpResponse ( t . render ( c ) , mimetype = mimetype ) <EOL> def archive_day ( request , year , month , day , queryset , date_field , <EOL> month_format = '<STR_LIT>' , day_format = '<STR_LIT>' , template_name = None , <EOL> template_loader = loader , extra_context = None , allow_empty = False , <EOL> context_processors = None , template_object_name = '<STR_LIT:object>' , <EOL> mimetype = None , allow_future = False ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> try : <EOL> date = datetime . date ( * time . strptime ( year + month + day , '<STR_LIT>' + month_format + day_format ) [ : <NUM_LIT:3> ] ) <EOL> except ValueError : <EOL> raise Http404 <EOL> model = queryset . model <EOL> now = datetime . datetime . now ( ) <EOL> if isinstance ( model . _meta . get_field ( date_field ) , DateTimeField ) : <EOL> lookup_kwargs = { '<STR_LIT>' % date_field : ( datetime . datetime . combine ( date , datetime . time . min ) , datetime . datetime . combine ( date , datetime . time . max ) ) } <EOL> else : <EOL> lookup_kwargs = { date_field : date } <EOL> if date >= now . date ( ) and not allow_future : <EOL> lookup_kwargs [ '<STR_LIT>' % date_field ] = now <EOL> object_list = queryset . filter ( ** lookup_kwargs ) <EOL> if not allow_empty and not object_list : <EOL> raise Http404 <EOL> if allow_future : <EOL> next_day = date + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> elif date < datetime . date . today ( ) : <EOL> next_day = date + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> else : <EOL> next_day = None <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> '<STR_LIT>' % template_object_name : object_list , <EOL> '<STR_LIT>' : date , <EOL> '<STR_LIT>' : date - datetime . timedelta ( days = <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : next_day , <EOL> } , context_processors ) <EOL> for key , value in extra_context . items ( ) : <EOL> if callable ( value ) : <EOL> c [ key ] = value ( ) <EOL> else : <EOL> c [ key ] = value <EOL> return HttpResponse ( t . render ( c ) , mimetype = mimetype ) <EOL> def archive_today ( request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> today = datetime . date . today ( ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : str ( today . year ) , <EOL> '<STR_LIT>' : today . strftime ( '<STR_LIT>' ) . lower ( ) , <EOL> '<STR_LIT>' : str ( today . day ) , <EOL> } ) <EOL> return archive_day ( request , ** kwargs ) <EOL> def object_detail ( request , year , month , day , queryset , date_field , <EOL> month_format = '<STR_LIT>' , day_format = '<STR_LIT>' , object_id = None , slug = None , <EOL> slug_field = None , template_name = None , template_name_field = None , <EOL> template_loader = loader , extra_context = None , context_processors = None , <EOL> template_object_name = '<STR_LIT:object>' , mimetype = None , allow_future = False ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> try : <EOL> date = datetime . date ( * time . strptime ( year + month + day , '<STR_LIT>' + month_format + day_format ) [ : <NUM_LIT:3> ] ) <EOL> except ValueError : <EOL> raise Http404 <EOL> model = queryset . model <EOL> now = datetime . datetime . now ( ) <EOL> if isinstance ( model . _meta . get_field ( date_field ) , DateTimeField ) : <EOL> lookup_kwargs = { '<STR_LIT>' % date_field : ( datetime . datetime . combine ( date , datetime . time . min ) , datetime . datetime . combine ( date , datetime . time . max ) ) } <EOL> else : <EOL> lookup_kwargs = { date_field : date } <EOL> if date >= now . date ( ) and not allow_future : <EOL> lookup_kwargs [ '<STR_LIT>' % date_field ] = now <EOL> if object_id : <EOL> lookup_kwargs [ '<STR_LIT>' % model . _meta . pk . name ] = object_id <EOL> elif slug and slug_field : <EOL> lookup_kwargs [ '<STR_LIT>' % slug_field ] = slug <EOL> else : <EOL> raise AttributeError , "<STR_LIT>" <EOL> try : <EOL> obj = queryset . get ( ** lookup_kwargs ) <EOL> except ObjectDoesNotExist : <EOL> raise Http404 , "<STR_LIT>" % model . _meta . verbose_name <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> if template_name_field : <EOL> template_name_list = [ getattr ( obj , template_name_field ) , template_name ] <EOL> t = template_loader . select_template ( template_name_list ) <EOL> else : <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> template_object_name : obj , <EOL> } , context_processors ) <EOL> for key , value in extra_context . items ( ) : <EOL> if callable ( value ) : <EOL> c [ key ] = value ( ) <EOL> else : <EOL> c [ key ] = value <EOL> response = HttpResponse ( t . render ( c ) , mimetype = mimetype ) <EOL> populate_xheaders ( request , response , model , getattr ( obj , obj . _meta . pk . name ) ) <EOL> return response </s>
