<s> import django <EOL> from django . db import models <EOL> from django . template import Library <EOL> from django . contrib . admin . templatetags . admin_list import result_headers , _boolean_icon <EOL> from django . contrib . admin . utils import lookup_field <EOL> from categories . editor . utils import display_for_field <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . utils . encoding import smart_text , force_text <EOL> from django . utils . html import escape , conditional_escape <EOL> from django . utils . safestring import mark_safe <EOL> from categories . editor import settings <EOL> register = Library ( ) <EOL> TREE_LIST_RESULTS_TEMPLATE = '<STR_LIT>' <EOL> if settings . IS_GRAPPELLI_INSTALLED : <EOL> TREE_LIST_RESULTS_TEMPLATE = '<STR_LIT>' <EOL> def get_empty_value_display ( cl ) : <EOL> if hasattr ( cl . model_admin , '<STR_LIT>' ) : <EOL> return cl . model_admin . get_empty_value_display ( ) <EOL> else : <EOL> from django . contrib . admin . views . main import EMPTY_CHANGELIST_VALUE <EOL> return EMPTY_CHANGELIST_VALUE <EOL> def items_for_tree_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 ( AttributeError , ObjectDoesNotExist ) : <EOL> result_repr = get_empty_value_display ( cl ) <EOL> else : <EOL> if f is None : <EOL> if django . VERSION [ <NUM_LIT:1> ] == <NUM_LIT:4> : <EOL> if field_name == '<STR_LIT>' : <EOL> row_class = '<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 = _boolean_icon ( value ) <EOL> else : <EOL> result_repr = smart_text ( value ) <EOL> if not allow_tags : <EOL> result_repr = escape ( result_repr ) <EOL> else : <EOL> result_repr = mark_safe ( result_repr ) <EOL> else : <EOL> if value is None : <EOL> result_repr = get_empty_value_display ( cl ) <EOL> if isinstance ( f . rel , models . ManyToOneRel ) : <EOL> result_repr = escape ( getattr ( result , f . name ) ) <EOL> else : <EOL> result_repr = display_for_field ( value , f , '<STR_LIT>' ) <EOL> if isinstance ( f , models . DateField ) or isinstance ( f , models . TimeField ) : <EOL> row_class = '<STR_LIT>' <EOL> if first : <EOL> if django . VERSION [ <NUM_LIT:1> ] < <NUM_LIT:4> : <EOL> try : <EOL> f , attr , checkbox_value = lookup_field ( '<STR_LIT>' , result , cl . model_admin ) <EOL> if row_class : <EOL> row_class = "<STR_LIT>" % ( row_class [ : - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> else : <EOL> row_class = '<STR_LIT>' <EOL> except ( AttributeError , ObjectDoesNotExist ) : <EOL> pass <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> if django . VERSION [ <NUM_LIT:1> ] < <NUM_LIT:4> : <EOL> table_tag = '<STR_LIT>' <EOL> else : <EOL> table_tag = { True : '<STR_LIT>' , False : '<STR_LIT>' } [ first ] <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> first = False <EOL> if django . VERSION [ <NUM_LIT:1> ] < <NUM_LIT:4> : <EOL> yield mark_safe ( <EOL> '<STR_LIT>' % <EOL> ( table_tag , row_class , checkbox_value , url , ( cl . is_popup and '<STR_LIT>' % result_id or '<STR_LIT>' ) , conditional_escape ( result_repr ) , table_tag ) ) <EOL> else : <EOL> yield mark_safe ( <EOL> '<STR_LIT>' % <EOL> ( 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> if form and field_name in form . fields : <EOL> bf = form [ field_name ] <EOL> result_repr = mark_safe ( force_text ( bf . errors ) + force_text ( bf ) ) <EOL> else : <EOL> result_repr = conditional_escape ( result_repr ) <EOL> yield mark_safe ( '<STR_LIT>' % ( row_class , result_repr ) ) <EOL> if form and not form [ cl . model . _meta . pk . name ] . is_hidden : <EOL> yield mark_safe ( '<STR_LIT>' % force_text ( form [ cl . model . _meta . pk . name ] ) ) <EOL> class TreeList ( list ) : <EOL> pass <EOL> def tree_results ( cl ) : <EOL> if cl . formset : <EOL> for res , form in zip ( cl . result_list , cl . formset . forms ) : <EOL> result = TreeList ( items_for_tree_result ( cl , res , form ) ) <EOL> if hasattr ( res , '<STR_LIT>' ) : <EOL> result . pk = res . pk <EOL> if res . parent : <EOL> result . parent_pk = res . parent . pk <EOL> else : <EOL> res . parent_pk = None <EOL> yield result <EOL> else : <EOL> for res in cl . result_list : <EOL> result = TreeList ( items_for_tree_result ( cl , res , None ) ) <EOL> if hasattr ( res , '<STR_LIT>' ) : <EOL> result . pk = res . pk <EOL> if res . parent : <EOL> result . parent_pk = res . parent . pk <EOL> else : <EOL> res . parent_pk = None <EOL> yield result <EOL> def result_tree_list ( cl ) : <EOL> """<STR_LIT>""" <EOL> import django <EOL> result = { <EOL> '<STR_LIT>' : cl , <EOL> '<STR_LIT>' : list ( result_headers ( cl ) ) , <EOL> '<STR_LIT>' : list ( tree_results ( cl ) ) <EOL> } <EOL> if django . VERSION [ <NUM_LIT:1> ] > <NUM_LIT:2> : <EOL> from django . contrib . admin . templatetags . admin_list import result_hidden_fields <EOL> result [ '<STR_LIT>' ] = list ( result_hidden_fields ( cl ) ) <EOL> return result <EOL> result_tree_list = register . inclusion_tag ( TREE_LIST_RESULTS_TEMPLATE ) ( result_tree_list ) </s>
