#NEWLINE# def get_object_from_date_based_view( request , * args , **kwargs ) : #NEWLINE# #INDENT# '\n    Get object from generic date_based.detail view\n\n    Parameters\n    ----------\n    request : instance\n        An instance of HttpRequest\n\n    Returns\n    -------\n    instance\n        An instance of model object or None\n    ' #NEWLINE# import time #NEWLINE# import datetime #NEWLINE# from django.http import Http404 #NEWLINE# from django.db.models.fields import DateTimeField #NEWLINE# try : #NEWLINE# #INDENT# from django.utils import timezone #NEWLINE# datetime_now = timezone . now #NEWLINE# #UNINDENT# except ImportError : #NEWLINE# #INDENT# datetime_now = datetime . datetime . now #NEWLINE# #UNINDENT# ( year , month , day ) = ( kwargs [ 'year' ] , kwargs [ 'month' ] , kwargs [ 'day' ] ) #NEWLINE# month_format = kwargs . get ( 'month_format' , '%b' ) #NEWLINE# day_format = kwargs . get ( 'day_format' , '%d' ) #NEWLINE# date_field = kwargs [ 'date_field' ] #NEWLINE# queryset = kwargs [ 'queryset' ] #NEWLINE# object_id = kwargs . get ( 'object_id' , None ) #NEWLINE# slug = kwargs . get ( 'slug' , None ) #NEWLINE# slug_field = kwargs . get ( 'slug_field' , 'slug' ) #NEWLINE# try : #NEWLINE# #INDENT# tt = time . strptime ( ( '%s-%s-%s' % ( year , month , day ) ) , ( '%s-%s-%s' % ( '%Y' , month_format , day_format ) ) ) #NEWLINE# date = datetime . date ( * tt [ : 3 ] ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# raise Http404 #NEWLINE# #UNINDENT# model = queryset . model #NEWLINE# if isinstance ( model . _meta . get_field ( date_field ) , DateTimeField ) : #NEWLINE# #INDENT# lookup_kwargs = { #NEWLINE# #INDENT# ( '%s__range' % date_field ) : ( datetime . datetime . combine ( date , datetime . time . min ) , datetime . datetime . combine ( date , datetime . time . max ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# lookup_kwargs = { #NEWLINE# #INDENT# date_field : date , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# now = datetime_now ( ) #NEWLINE# if ( ( date >= now . date ( ) ) and ( not kwargs . get ( 'allow_future' , False ) ) ) : #NEWLINE# #INDENT# lookup_kwargs [ ( '%s__lte' % date_field ) ] = now #NEWLINE# #UNINDENT# if object_id : #NEWLINE# #INDENT# lookup_kwargs [ 'pk' ] = object_id #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( slug and slug_field ) : #NEWLINE# #INDENT# lookup_kwargs [ ( '%s__exact' % slug_field ) ] = slug #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise AttributeError ( 'Generic detail view must be called with either an object_id or a slug/slug_field.' ) #NEWLINE# #UNINDENT# #UNINDENT# return get_object_or_404 ( queryset , ** lookup_kwargs )