<s> from django . conf import settings <EOL> from django . db . models import * <EOL> from . django_helper import * <EOL> from . django_admin_helper import * <EOL> def is_suitable_for_raw_id_fields ( field ) : <EOL> return is_relationship_field ( field ) <EOL> def is_suitable_for_list_display ( field ) : <EOL> return is_string ( field ) or is_boolean ( field ) or is_number ( field ) or is_datetime ( field ) <EOL> def is_suitable_for_list_display_links ( field ) : <EOL> return is_suitable_for_list_display ( field ) <EOL> def is_suitable_for_list_filter ( field ) : <EOL> return field_has_choices ( field ) or is_boolean ( field ) <EOL> def is_suitable_for_search_fields ( field ) : <EOL> return is_string ( field ) <EOL> try : <EOL> DSA_LIST_PER_PAGE = int ( settings . DSA_LIST_PER_PAGE ) if hasattr ( settings , '<STR_LIT>' ) else <NUM_LIT:5> <EOL> DSA_LIST_MAX_SHOW_ALL = int ( settings . DSA_LIST_MAX_SHOW_ALL ) if hasattr ( settings , '<STR_LIT>' ) else <NUM_LIT:50> <EOL> DSA_FIELD_STRATEGY = settings . DSA_FIELD_STRATEGY if hasattr ( settings , '<STR_LIT>' ) else { } <EOL> DSA_FULL_STRATEGY = settings . DSA_FULL_STRATEGY if hasattr ( settings , '<STR_LIT>' ) else { } <EOL> except Exception as e : <EOL> raise Exception ( '<STR_LIT>' % str ( e ) ) <EOL> FIELD_STRATEGY = { <EOL> '<STR_LIT>' : is_suitable_for_raw_id_fields , <EOL> '<STR_LIT>' : is_suitable_for_list_display , <EOL> '<STR_LIT>' : is_suitable_for_list_display_links , <EOL> '<STR_LIT>' : is_suitable_for_list_filter , <EOL> '<STR_LIT>' : is_suitable_for_search_fields , <EOL> '<STR_LIT>' : DSA_LIST_PER_PAGE , <EOL> '<STR_LIT>' : DSA_LIST_MAX_SHOW_ALL , <EOL> } <EOL> def confirm_list_display ( value ) : <EOL> if not value : <EOL> return [ '<STR_LIT:id>' , '<STR_LIT>' ] <EOL> if len ( value ) < <NUM_LIT:2> : <EOL> new_value = [ '<STR_LIT:id>' , '<STR_LIT>' ] <EOL> new_value . extend ( value ) <EOL> return new_value <EOL> if len ( value ) > <NUM_LIT:15> : <EOL> return value [ <NUM_LIT:0> : <NUM_LIT:15> ] <EOL> return value <EOL> def confirm_list_display_links ( value ) : <EOL> return confirm_list_display ( value ) <EOL> def confirm_search_fields ( value ) : <EOL> if not value : <EOL> return [ '<STR_LIT:id>' ] <EOL> return value <EOL> FULL_STRATEGY = { <EOL> '<STR_LIT>' : confirm_list_display , <EOL> '<STR_LIT>' : confirm_list_display_links , <EOL> '<STR_LIT>' : confirm_search_fields , <EOL> } <EOL> def get_strategy ( dsa_default_settings , user_custom_settings ) : <EOL> strategy = { } <EOL> strategy . update ( dsa_default_settings ) <EOL> strategy . update ( user_custom_settings ) <EOL> return strategy <EOL> STRATEGY = get_strategy ( FIELD_STRATEGY , DSA_FIELD_STRATEGY ) <EOL> FINAL_STRATEGY = get_strategy ( FULL_STRATEGY , DSA_FULL_STRATEGY ) <EOL> def add_fk_links ( model , fields , admin_class ) : <EOL> for field in fields : <EOL> if is_relationship_field ( field ) : <EOL> try : <EOL> method = staticmethod ( lambda obj_id : get_admin_change_link ( model , obj_id ) ) <EOL> setattr ( admin_class , field . name + '<STR_LIT>' , method ) <EOL> except Exception as e : <EOL> print ( str ( e ) ) <EOL> def auto_configure_admin_for_model ( model , override = False , ** kwargs ) : <EOL> fields = get_fields_from_model ( model ) <EOL> config = { } <EOL> for admin_field in ADMIN_FIELDS : <EOL> dev_value = kwargs . get ( admin_field , None ) <EOL> if dev_value : <EOL> config [ admin_field ] = dev_value <EOL> continue <EOL> strategy = STRATEGY . get ( admin_field , None ) <EOL> if strategy : <EOL> if callable ( strategy ) : <EOL> field_list = [ field . name for field in fields if strategy ( field ) ] <EOL> config [ admin_field ] = field_list <EOL> else : <EOL> config [ admin_field ] = strategy <EOL> strategy = FINAL_STRATEGY . get ( admin_field , None ) <EOL> if strategy and callable ( strategy ) : <EOL> config [ admin_field ] = strategy ( config [ admin_field ] ) <EOL> admin_class = create_admin_class ( model , ** config ) <EOL> add_fk_links ( model , fields , admin_class ) <EOL> current_admin_class = register_admin_class ( model , admin_class , override = override ) <EOL> return current_admin_class <EOL> def auto_configure_admin_for_app ( app , override = False ) : <EOL> models = get_models_of_an_app ( app ) <EOL> for model in models : <EOL> auto_configure_admin_for_model ( model , override = override ) <EOL> def auto_configure_admin ( applications = [ ] , exclude_applications = [ ] , override = False ) : <EOL> apps = get_apps ( applications , exclude_applications ) <EOL> for app in apps : <EOL> auto_configure_admin_for_app ( app , override = override ) </s>
