<s> import re <EOL> import sys <EOL> import optparse <EOL> from django . db . models . loading import get_models <EOL> from django . db import models <EOL> from django . core . management . base import BaseCommand <EOL> from django . conf import settings <EOL> from django_extensions . management . color import color_style <EOL> from django_extensions . management . utils import signalcommand <EOL> MAX_LINE_WIDTH = getattr ( settings , '<STR_LIT>' , <NUM_LIT> ) <EOL> INDENT_WIDTH = getattr ( settings , '<STR_LIT>' , <NUM_LIT:4> ) <EOL> LIST_FILTER_THRESHOLD = getattr ( settings , '<STR_LIT>' , <NUM_LIT> ) <EOL> RAW_ID_THRESHOLD = getattr ( settings , '<STR_LIT>' , <NUM_LIT:100> ) <EOL> LIST_FILTER = getattr ( settings , '<STR_LIT>' , ( <EOL> models . DateField , <EOL> models . DateTimeField , <EOL> models . ForeignKey , <EOL> models . BooleanField , <EOL> ) ) <EOL> SEARCH_FIELD_NAMES = getattr ( settings , '<STR_LIT>' , ( <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> ) ) <EOL> DATE_HIERARCHY_NAMES = getattr ( settings , '<STR_LIT>' , ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) ) <EOL> PREPOPULATED_FIELD_NAMES = getattr ( settings , '<STR_LIT>' , ( <EOL> '<STR_LIT>' , <EOL> ) ) <EOL> PRINT_IMPORTS = getattr ( settings , '<STR_LIT>' , '''<STR_LIT>''' ) <EOL> PRINT_ADMIN_CLASS = getattr ( settings , '<STR_LIT>' , '''<STR_LIT>''' ) <EOL> PRINT_ADMIN_PROPERTY = getattr ( settings , '<STR_LIT>' , '''<STR_LIT>''' ) <EOL> class UnicodeMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> def __str__ ( self ) : <EOL> return self . __unicode__ ( ) <EOL> else : <EOL> def __str__ ( self ) : <EOL> return self . __unicode__ ( ) . encode ( '<STR_LIT:utf8>' ) <EOL> class AdminApp ( UnicodeMixin ) : <EOL> def __init__ ( self , app , model_res , ** options ) : <EOL> self . app = app <EOL> self . model_res = model_res <EOL> self . options = options <EOL> def __iter__ ( self ) : <EOL> for model in get_models ( self . app ) : <EOL> admin_model = AdminModel ( model , ** self . options ) <EOL> for model_re in self . model_res : <EOL> if model_re . search ( admin_model . name ) : <EOL> break <EOL> else : <EOL> if self . model_res : <EOL> continue <EOL> yield admin_model <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' . join ( self . _unicode_generator ( ) ) <EOL> def _unicode_generator ( self ) : <EOL> models_list = [ admin_model . name for admin_model in self ] <EOL> yield PRINT_IMPORTS % dict ( models = '<STR_LIT:U+002CU+0020>' . join ( models_list ) ) <EOL> admin_model_names = [ ] <EOL> for admin_model in self : <EOL> yield PRINT_ADMIN_CLASS % dict ( <EOL> name = admin_model . name , <EOL> class_ = admin_model , <EOL> ) <EOL> admin_model_names . append ( admin_model . name ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> self . app , <EOL> ) <EOL> class AdminModel ( UnicodeMixin ) : <EOL> PRINTABLE_PROPERTIES = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> def __init__ ( self , model , raw_id_threshold = RAW_ID_THRESHOLD , <EOL> list_filter_threshold = LIST_FILTER_THRESHOLD , <EOL> search_field_names = SEARCH_FIELD_NAMES , <EOL> date_hierarchy_names = DATE_HIERARCHY_NAMES , <EOL> prepopulated_field_names = PREPOPULATED_FIELD_NAMES , ** options ) : <EOL> self . model = model <EOL> self . list_display = [ ] <EOL> self . list_filter = [ ] <EOL> self . raw_id_fields = [ ] <EOL> self . search_fields = [ ] <EOL> self . prepopulated_fields = { } <EOL> self . date_hierarchy = None <EOL> self . search_field_names = search_field_names <EOL> self . raw_id_threshold = raw_id_threshold <EOL> self . list_filter_threshold = list_filter_threshold <EOL> self . date_hierarchy_names = date_hierarchy_names <EOL> self . prepopulated_field_names = prepopulated_field_names <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> self . name , <EOL> ) <EOL> @ property <EOL> def name ( self ) : <EOL> return self . model . __name__ <EOL> def _process_many_to_many ( self , meta ) : <EOL> raw_id_threshold = self . raw_id_threshold <EOL> for field in meta . local_many_to_many : <EOL> related_objects = field . related . parent_model . objects . all ( ) <EOL> if ( related_objects [ : raw_id_threshold ] . count ( ) < raw_id_threshold ) : <EOL> yield field . name <EOL> def _process_fields ( self , meta ) : <EOL> parent_fields = meta . parents . values ( ) <EOL> for field in meta . fields : <EOL> name = self . _process_field ( field , parent_fields ) <EOL> if name : <EOL> yield name <EOL> def _process_foreign_key ( self , field ) : <EOL> raw_id_threshold = self . raw_id_threshold <EOL> list_filter_threshold = self . list_filter_threshold <EOL> max_count = max ( list_filter_threshold , raw_id_threshold ) <EOL> related_count = field . related . parent_model . objects . all ( ) <EOL> related_count = related_count [ : max_count ] . count ( ) <EOL> if related_count >= raw_id_threshold : <EOL> self . raw_id_fields . append ( field . name ) <EOL> elif related_count < list_filter_threshold : <EOL> self . list_filter . append ( field . name ) <EOL> else : <EOL> pass <EOL> def _process_field ( self , field , parent_fields ) : <EOL> if field in parent_fields : <EOL> return <EOL> self . list_display . append ( field . name ) <EOL> if isinstance ( field , LIST_FILTER ) : <EOL> if isinstance ( field , models . ForeignKey ) : <EOL> self . _process_foreign_key ( field ) <EOL> else : <EOL> self . list_filter . append ( field . name ) <EOL> if field . name in self . search_field_names : <EOL> self . search_fields . append ( field . name ) <EOL> return field . name <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' . join ( self . _unicode_generator ( ) ) <EOL> def _yield_value ( self , key , value ) : <EOL> if isinstance ( value , ( list , set , tuple ) ) : <EOL> return self . _yield_tuple ( key , tuple ( value ) ) <EOL> elif isinstance ( value , dict ) : <EOL> return self . _yield_dict ( key , value ) <EOL> elif isinstance ( value , str ) : <EOL> return self . _yield_string ( key , value ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % ( type ( value ) , value ) ) <EOL> def _yield_string ( self , key , value , converter = repr ) : <EOL> return PRINT_ADMIN_PROPERTY % dict ( <EOL> key = key , <EOL> value = converter ( value ) , <EOL> ) <EOL> def _yield_dict ( self , key , value ) : <EOL> row_parts = [ ] <EOL> row = self . _yield_string ( key , value ) <EOL> if len ( row ) > MAX_LINE_WIDTH : <EOL> row_parts . append ( self . _yield_string ( key , '<STR_LIT:{>' , str ) ) <EOL> for k , v in value . items ( ) : <EOL> row_parts . append ( '<STR_LIT>' % ( <NUM_LIT:2> * INDENT_WIDTH * '<STR_LIT:U+0020>' , k , v ) ) <EOL> row_parts . append ( INDENT_WIDTH * '<STR_LIT:U+0020>' + '<STR_LIT:}>' ) <EOL> row = '<STR_LIT:\n>' . join ( row_parts ) <EOL> return row <EOL> def _yield_tuple ( self , key , value ) : <EOL> row_parts = [ ] <EOL> row = self . _yield_string ( key , value ) <EOL> if len ( row ) > MAX_LINE_WIDTH : <EOL> row_parts . append ( self . _yield_string ( key , '<STR_LIT:(>' , str ) ) <EOL> for v in value : <EOL> row_parts . append ( <NUM_LIT:2> * INDENT_WIDTH * '<STR_LIT:U+0020>' + repr ( v ) + '<STR_LIT:U+002C>' ) <EOL> row_parts . append ( INDENT_WIDTH * '<STR_LIT:U+0020>' + '<STR_LIT:)>' ) <EOL> row = '<STR_LIT:\n>' . join ( row_parts ) <EOL> return row <EOL> def _unicode_generator ( self ) : <EOL> self . _process ( ) <EOL> for key in self . PRINTABLE_PROPERTIES : <EOL> value = getattr ( self , key ) <EOL> if value : <EOL> yield self . _yield_value ( key , value ) <EOL> def _process ( self ) : <EOL> meta = self . model . _meta <EOL> self . raw_id_fields += list ( self . _process_many_to_many ( meta ) ) <EOL> field_names = list ( self . _process_fields ( meta ) ) <EOL> for field_name in self . date_hierarchy_names [ : : - <NUM_LIT:1> ] : <EOL> if field_name in field_names and not self . date_hierarchy : <EOL> self . date_hierarchy = field_name <EOL> break <EOL> for k in sorted ( self . prepopulated_field_names ) : <EOL> k , vs = k . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> vs = vs . split ( '<STR_LIT:U+002C>' ) <EOL> if k in field_names : <EOL> incomplete = False <EOL> for v in vs : <EOL> if v not in field_names : <EOL> incomplete = True <EOL> break <EOL> if not incomplete : <EOL> self . prepopulated_fields [ k ] = vs <EOL> self . processed = True <EOL> class Command ( BaseCommand ) : <EOL> help = '''<STR_LIT>''' <EOL> option_list = BaseCommand . option_list + ( <EOL> optparse . make_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , <EOL> default = SEARCH_FIELD_NAMES , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> optparse . make_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , <EOL> default = DATE_HIERARCHY_NAMES , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> optparse . make_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , <EOL> default = PREPOPULATED_FIELD_NAMES , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> optparse . make_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , type = '<STR_LIT:int>' , <EOL> default = LIST_FILTER_THRESHOLD , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> optparse . make_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , type = '<STR_LIT:int>' , <EOL> default = RAW_ID_THRESHOLD , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ) <EOL> can_import_settings = True <EOL> @ signalcommand <EOL> def handle ( self , * args , ** kwargs ) : <EOL> self . style = color_style ( ) <EOL> installed_apps = dict ( ( a . __name__ . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] , a ) for a in models . get_apps ( ) ) <EOL> if not args : <EOL> args = [ False ] <EOL> app = installed_apps . get ( args [ <NUM_LIT:0> ] ) <EOL> if not app : <EOL> print ( self . style . WARN ( '<STR_LIT>' ) ) <EOL> print ( self . style . WARN ( '<STR_LIT>' ) ) <EOL> for app in sorted ( installed_apps ) : <EOL> print ( self . style . WARN ( '<STR_LIT>' % app ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> model_res = [ ] <EOL> for arg in args [ <NUM_LIT:1> : ] : <EOL> model_res . append ( re . compile ( arg , re . IGNORECASE ) ) <EOL> self . handle_app ( app , model_res , ** kwargs ) <EOL> def handle_app ( self , app , model_res , ** options ) : <EOL> print ( AdminApp ( app , model_res , ** options ) ) </s>
