<s> from __future__ import unicode_literals <EOL> import datetime <EOL> from django . contrib . admin . util import ( lookup_field , display_for_field , <EOL> display_for_value , label_for_field ) <EOL> from django . contrib . admin . views . main import ( ALL_VAR , EMPTY_CHANGELIST_VALUE , <EOL> ORDER_VAR , PAGE_VAR , SEARCH_VAR ) <EOL> from django . contrib . admin . templatetags . admin_static import static <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db import models <EOL> from django . utils import formats <EOL> from django . utils . html import format_html <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils import six <EOL> from django . utils . text import capfirst <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . encoding import smart_text , force_text <EOL> from django . template import Library <EOL> from django . template . loader import get_template <EOL> from django . template . context import Context <EOL> register = Library ( ) <EOL> DOT = '<STR_LIT:.>' <EOL> @ register . simple_tag <EOL> def paginator_number ( cl , i ) : <EOL> """<STR_LIT>""" <EOL> if i == DOT : <EOL> return '<STR_LIT>' <EOL> elif i == cl . page_num : <EOL> return format_html ( '<STR_LIT>' , i + <NUM_LIT:1> ) <EOL> else : <EOL> return format_html ( '<STR_LIT>' , <EOL> cl . get_query_string ( { PAGE_VAR : i } ) , <EOL> mark_safe ( '<STR_LIT>' if i == cl . paginator . num_pages - <NUM_LIT:1> else '<STR_LIT>' ) , <EOL> i + <NUM_LIT:1> ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def pagination ( cl ) : <EOL> """<STR_LIT>""" <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> def result_headers ( cl ) : <EOL> """<STR_LIT>""" <EOL> ordering_field_columns = cl . get_ordering_field_columns ( ) <EOL> for i , field_name in enumerate ( cl . list_display ) : <EOL> text , attr = label_for_field ( field_name , cl . model , <EOL> model_admin = cl . model_admin , <EOL> return_attr = True <EOL> ) <EOL> if attr : <EOL> if field_name == '<STR_LIT>' : <EOL> yield { <EOL> "<STR_LIT:text>" : text , <EOL> "<STR_LIT>" : mark_safe ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : False , <EOL> } <EOL> continue <EOL> admin_order_field = getattr ( attr , "<STR_LIT>" , None ) <EOL> if not admin_order_field : <EOL> yield { <EOL> "<STR_LIT:text>" : text , <EOL> "<STR_LIT>" : False , <EOL> } <EOL> continue <EOL> th_classes = [ '<STR_LIT>' ] <EOL> order_type = '<STR_LIT>' <EOL> new_order_type = '<STR_LIT>' <EOL> sort_priority = <NUM_LIT:0> <EOL> sorted = False <EOL> if i in ordering_field_columns : <EOL> sorted = True <EOL> order_type = ordering_field_columns . get ( i ) . lower ( ) <EOL> sort_priority = list ( ordering_field_columns ) . index ( i ) + <NUM_LIT:1> <EOL> th_classes . append ( '<STR_LIT>' % order_type ) <EOL> new_order_type = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } [ order_type ] <EOL> o_list_primary = [ ] <EOL> o_list_remove = [ ] <EOL> o_list_toggle = [ ] <EOL> make_qs_param = lambda t , n : ( '<STR_LIT:->' if t == '<STR_LIT>' else '<STR_LIT>' ) + str ( n ) <EOL> for j , ot in ordering_field_columns . items ( ) : <EOL> if j == i : <EOL> param = make_qs_param ( new_order_type , j ) <EOL> o_list_primary . insert ( <NUM_LIT:0> , param ) <EOL> o_list_toggle . append ( param ) <EOL> else : <EOL> param = make_qs_param ( ot , j ) <EOL> o_list_primary . append ( param ) <EOL> o_list_toggle . append ( param ) <EOL> o_list_remove . append ( param ) <EOL> if i not in ordering_field_columns : <EOL> o_list_primary . insert ( <NUM_LIT:0> , make_qs_param ( new_order_type , i ) ) <EOL> yield { <EOL> "<STR_LIT:text>" : text , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : sorted , <EOL> "<STR_LIT>" : order_type == "<STR_LIT>" , <EOL> "<STR_LIT>" : sort_priority , <EOL> "<STR_LIT>" : cl . get_query_string ( { ORDER_VAR : '<STR_LIT:.>' . join ( o_list_primary ) } ) , <EOL> "<STR_LIT>" : cl . get_query_string ( { ORDER_VAR : '<STR_LIT:.>' . join ( o_list_remove ) } ) , <EOL> "<STR_LIT>" : cl . get_query_string ( { ORDER_VAR : '<STR_LIT:.>' . join ( o_list_toggle ) } ) , <EOL> "<STR_LIT>" : format_html ( '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( th_classes ) ) <EOL> if th_classes else '<STR_LIT>' , <EOL> } <EOL> def _boolean_icon ( field_val ) : <EOL> icon_url = static ( '<STR_LIT>' % <EOL> { True : '<STR_LIT:yes>' , False : '<STR_LIT>' , None : '<STR_LIT>' } [ field_val ] ) <EOL> return format_html ( '<STR_LIT>' , icon_url , field_val ) <EOL> def items_for_result ( cl , result , form ) : <EOL> """<STR_LIT>""" <EOL> first = True <EOL> pk = cl . lookup_opts . pk . attname <EOL> for field_name in cl . list_display : <EOL> row_class = '<STR_LIT>' <EOL> try : <EOL> f , attr , value = lookup_field ( field_name , result , cl . model_admin ) <EOL> except ObjectDoesNotExist : <EOL> result_repr = EMPTY_CHANGELIST_VALUE <EOL> else : <EOL> if f is None : <EOL> if field_name == '<STR_LIT>' : <EOL> row_class = mark_safe ( '<STR_LIT>' ) <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 = display_for_value ( value , boolean ) <EOL> if allow_tags : <EOL> result_repr = mark_safe ( result_repr ) <EOL> if isinstance ( value , ( datetime . date , datetime . time ) ) : <EOL> row_class = mark_safe ( '<STR_LIT>' ) <EOL> else : <EOL> if isinstance ( f . rel , models . ManyToOneRel ) : <EOL> field_val = getattr ( result , f . name ) <EOL> if field_val is None : <EOL> result_repr = EMPTY_CHANGELIST_VALUE <EOL> else : <EOL> result_repr = field_val <EOL> else : <EOL> result_repr = display_for_field ( value , f ) <EOL> if isinstance ( f , ( models . DateField , models . TimeField , models . ForeignKey ) ) : <EOL> row_class = mark_safe ( '<STR_LIT>' ) <EOL> if force_text ( 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 = str ( cl . to_field ) <EOL> else : <EOL> attr = pk <EOL> value = result . serializable_value ( attr ) <EOL> result_id = repr ( force_text ( value ) ) [ <NUM_LIT:1> : ] <EOL> yield format_html ( '<STR_LIT>' , <EOL> table_tag , <EOL> row_class , <EOL> url , <EOL> format_html ( '<STR_LIT>' , result_id ) <EOL> if cl . is_popup else '<STR_LIT>' , <EOL> result_repr , <EOL> table_tag ) <EOL> else : <EOL> if ( form and field_name in form . fields and not ( <EOL> field_name == cl . model . _meta . pk . name and <EOL> form [ cl . model . _meta . pk . name ] . is_hidden ) ) : <EOL> bf = form [ field_name ] <EOL> result_repr = mark_safe ( force_text ( bf . errors ) + force_text ( bf ) ) <EOL> yield format_html ( '<STR_LIT>' , row_class , result_repr ) <EOL> if form and not form [ cl . model . _meta . pk . name ] . is_hidden : <EOL> yield format_html ( '<STR_LIT>' , force_text ( form [ cl . model . _meta . pk . name ] ) ) <EOL> class ResultList ( list ) : <EOL> def __init__ ( self , form , * items ) : <EOL> self . form = form <EOL> super ( ResultList , self ) . __init__ ( * items ) <EOL> def results ( cl ) : <EOL> if cl . formset : <EOL> for res , form in zip ( cl . result_list , cl . formset . forms ) : <EOL> yield ResultList ( form , items_for_result ( cl , res , form ) ) <EOL> else : <EOL> for res in cl . result_list : <EOL> yield ResultList ( None , items_for_result ( cl , res , None ) ) <EOL> def result_hidden_fields ( cl ) : <EOL> if cl . formset : <EOL> for res , form in zip ( cl . result_list , cl . formset . forms ) : <EOL> if form [ cl . model . _meta . pk . name ] . is_hidden : <EOL> yield mark_safe ( force_text ( form [ cl . model . _meta . pk . name ] ) ) <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def result_list ( cl ) : <EOL> """<STR_LIT>""" <EOL> headers = list ( result_headers ( cl ) ) <EOL> num_sorted_fields = <NUM_LIT:0> <EOL> for h in headers : <EOL> if h [ '<STR_LIT>' ] and h [ '<STR_LIT>' ] : <EOL> num_sorted_fields += <NUM_LIT:1> <EOL> return { '<STR_LIT>' : cl , <EOL> '<STR_LIT>' : list ( result_hidden_fields ( cl ) ) , <EOL> '<STR_LIT>' : headers , <EOL> '<STR_LIT>' : num_sorted_fields , <EOL> '<STR_LIT>' : list ( results ( cl ) ) } <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def date_hierarchy ( cl ) : <EOL> """<STR_LIT>""" <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> link = lambda d : cl . get_query_string ( d , [ field_generic ] ) <EOL> if not ( year_lookup or month_lookup or day_lookup ) : <EOL> date_range = cl . query_set . aggregate ( first = models . Min ( field_name ) , <EOL> last = models . Max ( field_name ) ) <EOL> if date_range [ '<STR_LIT>' ] and date_range [ '<STR_LIT>' ] : <EOL> if date_range [ '<STR_LIT>' ] . year == date_range [ '<STR_LIT>' ] . year : <EOL> year_lookup = date_range [ '<STR_LIT>' ] . year <EOL> if date_range [ '<STR_LIT>' ] . month == date_range [ '<STR_LIT>' ] . month : <EOL> month_lookup = date_range [ '<STR_LIT>' ] . month <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>' : capfirst ( formats . date_format ( day , '<STR_LIT>' ) ) <EOL> } , <EOL> '<STR_LIT>' : [ { '<STR_LIT:title>' : capfirst ( formats . date_format ( day , '<STR_LIT>' ) ) } ] <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>' : str ( 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>' : capfirst ( formats . date_format ( day , '<STR_LIT>' ) ) <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>' : capfirst ( formats . date_format ( month , '<STR_LIT>' ) ) <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 : str ( year . year ) } ) , <EOL> '<STR_LIT:title>' : str ( year . year ) , <EOL> } for year in years ] <EOL> } <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def search_form ( cl ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : cl , <EOL> '<STR_LIT>' : cl . result_count != cl . full_result_count , <EOL> '<STR_LIT>' : SEARCH_VAR <EOL> } <EOL> @ register . simple_tag <EOL> def admin_list_filter ( cl , spec ) : <EOL> tpl = get_template ( spec . template ) <EOL> return tpl . render ( Context ( { <EOL> '<STR_LIT:title>' : spec . title , <EOL> '<STR_LIT>' : list ( spec . choices ( cl ) ) , <EOL> '<STR_LIT>' : spec , <EOL> } ) ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) <EOL> def admin_actions ( context ) : <EOL> """<STR_LIT>""" <EOL> context [ '<STR_LIT>' ] = context . get ( '<STR_LIT>' , - <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> return context </s>
