<s> from django . conf import settings <EOL> from django . contrib . admin . views . main import ALL_VAR , EMPTY_CHANGELIST_VALUE <EOL> from django . contrib . admin . views . main import ORDER_VAR , ORDER_TYPE_VAR , PAGE_VAR , SEARCH_VAR <EOL> from django . contrib . admin . models import PRIMARY_KEY_URL_SEPARATOR <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db import models <EOL> from django . utils import dateformat <EOL> from django . utils . html import escape , conditional_escape <EOL> from django . utils . text import capfirst <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import get_date_formats , get_partial_date_formats , ugettext as _ <EOL> from django . utils . encoding import smart_unicode , smart_str , force_unicode <EOL> from django . template import Library <EOL> import datetime <EOL> register = Library ( ) <EOL> DOT = '<STR_LIT:.>' <EOL> def paginator_number ( cl , i ) : <EOL> if i == DOT : <EOL> return u'<STR_LIT>' <EOL> elif i == cl . page_num : <EOL> return mark_safe ( u'<STR_LIT>' % ( i + <NUM_LIT:1> ) ) <EOL> else : <EOL> return mark_safe ( u'<STR_LIT>' % ( cl . get_query_string ( { PAGE_VAR : i } ) , ( i == cl . paginator . num_pages - <NUM_LIT:1> and '<STR_LIT>' or '<STR_LIT>' ) , i + <NUM_LIT:1> ) ) <EOL> paginator_number = register . simple_tag ( paginator_number ) <EOL> def pagination ( cl ) : <EOL> paginator , page_num = cl . paginator , cl . page_num <EOL> pagination_required = ( not cl . show_all or not cl . can_show_all ) and cl . multi_page <EOL> if not pagination_required : <EOL> page_range = [ ] <EOL> else : <EOL> ON_EACH_SIDE = <NUM_LIT:3> <EOL> ON_ENDS = <NUM_LIT:2> <EOL> if paginator . num_pages <= <NUM_LIT:10> : <EOL> page_range = range ( paginator . num_pages ) <EOL> else : <EOL> page_range = [ ] <EOL> if page_num > ( ON_EACH_SIDE + ON_ENDS ) : <EOL> page_range . extend ( range ( <NUM_LIT:0> , ON_EACH_SIDE - <NUM_LIT:1> ) ) <EOL> page_range . append ( DOT ) <EOL> page_range . extend ( range ( page_num - ON_EACH_SIDE , page_num + <NUM_LIT:1> ) ) <EOL> else : <EOL> page_range . extend ( range ( <NUM_LIT:0> , page_num + <NUM_LIT:1> ) ) <EOL> if page_num < ( paginator . num_pages - ON_EACH_SIDE - ON_ENDS - <NUM_LIT:1> ) : <EOL> page_range . extend ( range ( page_num + <NUM_LIT:1> , page_num + ON_EACH_SIDE + <NUM_LIT:1> ) ) <EOL> page_range . append ( DOT ) <EOL> page_range . extend ( range ( paginator . num_pages - ON_ENDS , paginator . num_pages ) ) <EOL> else : <EOL> page_range . extend ( range ( page_num + <NUM_LIT:1> , paginator . num_pages ) ) <EOL> need_show_all_link = cl . can_show_all and not cl . show_all and cl . multi_page <EOL> return { <EOL> '<STR_LIT>' : cl , <EOL> '<STR_LIT>' : pagination_required , <EOL> '<STR_LIT>' : need_show_all_link and cl . get_query_string ( { ALL_VAR : '<STR_LIT>' } ) , <EOL> '<STR_LIT>' : page_range , <EOL> '<STR_LIT>' : ALL_VAR , <EOL> '<STR_LIT:1>' : <NUM_LIT:1> , <EOL> } <EOL> pagination = register . inclusion_tag ( '<STR_LIT>' ) ( pagination ) <EOL> def result_headers ( cl ) : <EOL> lookup_opts = cl . lookup_opts <EOL> for i , field_name in enumerate ( cl . list_display ) : <EOL> attr = None <EOL> try : <EOL> f = lookup_opts . get_field ( field_name ) <EOL> admin_order_field = None <EOL> except models . FieldDoesNotExist : <EOL> if field_name == '<STR_LIT>' : <EOL> header = force_unicode ( lookup_opts . verbose_name ) <EOL> elif field_name == '<STR_LIT>' : <EOL> header = smart_str ( lookup_opts . verbose_name ) <EOL> else : <EOL> if callable ( field_name ) : <EOL> attr = field_name <EOL> else : <EOL> try : <EOL> attr = getattr ( cl . model_admin , field_name ) <EOL> except AttributeError : <EOL> try : <EOL> attr = getattr ( cl . model , field_name ) <EOL> except AttributeError : <EOL> raise AttributeError , "<STR_LIT>" % ( lookup_opts . object_name , cl . model_admin . __class__ , field_name ) <EOL> try : <EOL> header = attr . short_description <EOL> except AttributeError : <EOL> if callable ( field_name ) : <EOL> header = field_name . __name__ <EOL> else : <EOL> header = field_name <EOL> header = header . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> admin_order_field = getattr ( attr , "<STR_LIT>" , None ) <EOL> if not admin_order_field : <EOL> yield { "<STR_LIT:text>" : header } <EOL> continue <EOL> else : <EOL> header = f . verbose_name <EOL> th_classes = [ ] <EOL> new_order_type = '<STR_LIT>' <EOL> if field_name == cl . order_field or admin_order_field == cl . order_field : <EOL> th_classes . append ( '<STR_LIT>' % cl . order_type . lower ( ) ) <EOL> new_order_type = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } [ cl . order_type . lower ( ) ] <EOL> yield { "<STR_LIT:text>" : header , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT:url>" : cl . get_query_string ( { ORDER_VAR : i , ORDER_TYPE_VAR : new_order_type } ) , <EOL> "<STR_LIT>" : mark_safe ( th_classes and '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( th_classes ) or '<STR_LIT>' ) } <EOL> def _boolean_icon ( field_val ) : <EOL> BOOLEAN_MAPPING = { True : '<STR_LIT:yes>' , False : '<STR_LIT>' , None : '<STR_LIT>' } <EOL> return mark_safe ( u'<STR_LIT>' % ( settings . ADMIN_MEDIA_PREFIX , BOOLEAN_MAPPING [ field_val ] , field_val ) ) <EOL> def items_for_result ( cl , result ) : <EOL> first = True <EOL> for field_name in cl . list_display : <EOL> row_class = '<STR_LIT>' <EOL> try : <EOL> f = cl . lookup_opts . get_field ( field_name ) <EOL> except models . FieldDoesNotExist : <EOL> try : <EOL> if callable ( field_name ) : <EOL> attr = field_name <EOL> value = attr ( result ) <EOL> elif hasattr ( cl . model_admin , field_name ) and not field_name == '<STR_LIT>' and not field_name == '<STR_LIT>' : <EOL> attr = getattr ( cl . model_admin , field_name ) <EOL> value = attr ( result ) <EOL> else : <EOL> attr = getattr ( result , field_name ) <EOL> if callable ( attr ) : <EOL> value = attr ( ) <EOL> else : <EOL> value = attr <EOL> allow_tags = getattr ( attr , '<STR_LIT>' , False ) <EOL> boolean = getattr ( attr , '<STR_LIT>' , False ) <EOL> if boolean : <EOL> allow_tags = True <EOL> result_repr = _boolean_icon ( value ) <EOL> else : <EOL> result_repr = smart_unicode ( value ) <EOL> except ( AttributeError , ObjectDoesNotExist ) : <EOL> result_repr = EMPTY_CHANGELIST_VALUE <EOL> else : <EOL> if not allow_tags : <EOL> result_repr = escape ( result_repr ) <EOL> else : <EOL> result_repr = mark_safe ( result_repr ) <EOL> else : <EOL> field_val = getattr ( result , f . attname ) <EOL> if isinstance ( f . rel , models . ManyToOneRel ) : <EOL> if field_val is not None : <EOL> result_repr = escape ( getattr ( result , f . name ) ) <EOL> else : <EOL> result_repr = EMPTY_CHANGELIST_VALUE <EOL> elif isinstance ( f , models . DateField ) or isinstance ( f , models . TimeField ) : <EOL> if field_val : <EOL> ( date_format , datetime_format , time_format ) = get_date_formats ( ) <EOL> if isinstance ( f , models . DateTimeField ) : <EOL> result_repr = capfirst ( dateformat . format ( field_val , datetime_format ) ) <EOL> elif isinstance ( f , models . TimeField ) : <EOL> result_repr = capfirst ( dateformat . time_format ( field_val , time_format ) ) <EOL> else : <EOL> result_repr = capfirst ( dateformat . format ( field_val , date_format ) ) <EOL> else : <EOL> result_repr = EMPTY_CHANGELIST_VALUE <EOL> row_class = '<STR_LIT>' <EOL> elif isinstance ( f , models . BooleanField ) or isinstance ( f , models . NullBooleanField ) : <EOL> result_repr = _boolean_icon ( field_val ) <EOL> elif isinstance ( f , models . DecimalField ) : <EOL> if field_val is not None : <EOL> result_repr = ( '<STR_LIT>' % f . decimal_places ) % field_val <EOL> else : <EOL> result_repr = EMPTY_CHANGELIST_VALUE <EOL> elif f . choices : <EOL> result_repr = dict ( f . choices ) . get ( field_val , EMPTY_CHANGELIST_VALUE ) <EOL> else : <EOL> result_repr = escape ( field_val ) <EOL> if force_unicode ( result_repr ) == '<STR_LIT>' : <EOL> result_repr = mark_safe ( '<STR_LIT>' ) <EOL> if ( first and not cl . list_display_links ) or field_name in cl . list_display_links : <EOL> table_tag = { True : '<STR_LIT>' , False : '<STR_LIT>' } [ first ] <EOL> first = False <EOL> url = cl . url_for_result ( result ) <EOL> if cl . to_field : <EOL> attr = tuple ( cl . to_field ) <EOL> else : <EOL> attr = cl . pk . attnames <EOL> result_id = repr ( PRIMARY_KEY_URL_SEPARATOR . join ( [ force_unicode ( getattr ( result , pk ) ) for pk in attr ] ) ) [ <NUM_LIT:1> : ] <EOL> yield mark_safe ( u'<STR_LIT>' % ( table_tag , row_class , url , ( cl . is_popup and '<STR_LIT>' % result_id or '<STR_LIT>' ) , conditional_escape ( result_repr ) , table_tag ) ) <EOL> else : <EOL> yield mark_safe ( u'<STR_LIT>' % ( row_class , conditional_escape ( result_repr ) ) ) <EOL> def results ( cl ) : <EOL> for res in cl . result_list : <EOL> yield list ( items_for_result ( cl , res ) ) <EOL> def result_list ( cl ) : <EOL> return { '<STR_LIT>' : cl , <EOL> '<STR_LIT>' : list ( result_headers ( cl ) ) , <EOL> '<STR_LIT>' : list ( results ( cl ) ) } <EOL> result_list = register . inclusion_tag ( "<STR_LIT>" ) ( result_list ) <EOL> def date_hierarchy ( cl ) : <EOL> if cl . date_hierarchy : <EOL> field_name = cl . date_hierarchy <EOL> year_field = '<STR_LIT>' % field_name <EOL> month_field = '<STR_LIT>' % field_name <EOL> day_field = '<STR_LIT>' % field_name <EOL> field_generic = '<STR_LIT>' % field_name <EOL> year_lookup = cl . params . get ( year_field ) <EOL> month_lookup = cl . params . get ( month_field ) <EOL> day_lookup = cl . params . get ( day_field ) <EOL> year_month_format , month_day_format = get_partial_date_formats ( ) <EOL> link = lambda d : mark_safe ( cl . get_query_string ( d , [ field_generic ] ) ) <EOL> if year_lookup and month_lookup and day_lookup : <EOL> day = datetime . date ( int ( year_lookup ) , int ( month_lookup ) , int ( day_lookup ) ) <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : link ( { year_field : year_lookup , month_field : month_lookup } ) , <EOL> '<STR_LIT:title>' : dateformat . format ( day , year_month_format ) <EOL> } , <EOL> '<STR_LIT>' : [ { '<STR_LIT:title>' : dateformat . format ( day , month_day_format ) } ] <EOL> } <EOL> elif year_lookup and month_lookup : <EOL> days = cl . query_set . filter ( ** { year_field : year_lookup , month_field : month_lookup } ) . dates ( field_name , '<STR_LIT>' ) <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : link ( { year_field : year_lookup } ) , <EOL> '<STR_LIT:title>' : year_lookup <EOL> } , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : link ( { year_field : year_lookup , month_field : month_lookup , day_field : day . day } ) , <EOL> '<STR_LIT:title>' : dateformat . format ( day , month_day_format ) <EOL> } for day in days ] <EOL> } <EOL> elif year_lookup : <EOL> months = cl . query_set . filter ( ** { year_field : year_lookup } ) . dates ( field_name , '<STR_LIT>' ) <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : link ( { } ) , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) <EOL> } , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : link ( { year_field : year_lookup , month_field : month . month } ) , <EOL> '<STR_LIT:title>' : dateformat . format ( month , year_month_format ) <EOL> } for month in months ] <EOL> } <EOL> else : <EOL> years = cl . query_set . dates ( field_name , '<STR_LIT>' ) <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : link ( { year_field : year . year } ) , <EOL> '<STR_LIT:title>' : year . year <EOL> } for year in years ] <EOL> } <EOL> date_hierarchy = register . inclusion_tag ( '<STR_LIT>' ) ( date_hierarchy ) <EOL> def search_form ( cl ) : <EOL> return { <EOL> '<STR_LIT>' : cl , <EOL> '<STR_LIT>' : cl . result_count != cl . full_result_count , <EOL> '<STR_LIT>' : SEARCH_VAR <EOL> } <EOL> search_form = register . inclusion_tag ( '<STR_LIT>' ) ( search_form ) <EOL> def admin_list_filter ( cl , spec ) : <EOL> return { '<STR_LIT:title>' : spec . title ( ) , '<STR_LIT>' : list ( spec . choices ( cl ) ) } <EOL> admin_list_filter = register . inclusion_tag ( '<STR_LIT>' ) ( admin_list_filter ) </s>
