<s> from django . contrib . contenttypes . models import ContentType <EOL> from django . db import DEFAULT_DB_ALIAS , router <EOL> from django . db . models import get_apps , get_model , get_models , signals , UnavailableApp <EOL> from django . utils . encoding import smart_text <EOL> from django . utils import six <EOL> from django . utils . six . moves import input <EOL> def update_contenttypes ( app , created_models , verbosity = <NUM_LIT:2> , db = DEFAULT_DB_ALIAS , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except UnavailableApp : <EOL> return <EOL> if not router . allow_syncdb ( db , ContentType ) : <EOL> return <EOL> ContentType . objects . clear_cache ( ) <EOL> app_models = get_models ( app ) <EOL> if not app_models : <EOL> return <EOL> app_label = app_models [ <NUM_LIT:0> ] . _meta . app_label <EOL> app_models = dict ( <EOL> ( model . _meta . model_name , model ) <EOL> for model in app_models <EOL> ) <EOL> content_types = dict ( <EOL> ( ct . model , ct ) <EOL> for ct in ContentType . objects . using ( db ) . filter ( app_label = app_label ) <EOL> ) <EOL> to_remove = [ <EOL> ct <EOL> for ( model_name , ct ) in six . iteritems ( content_types ) <EOL> if model_name not in app_models <EOL> ] <EOL> cts = [ <EOL> ContentType ( <EOL> name = smart_text ( model . _meta . verbose_name_raw ) , <EOL> app_label = app_label , <EOL> model = model_name , <EOL> ) <EOL> for ( model_name , model ) in six . iteritems ( app_models ) <EOL> if model_name not in content_types <EOL> ] <EOL> ContentType . objects . using ( db ) . bulk_create ( cts ) <EOL> if verbosity >= <NUM_LIT:2> : <EOL> for ct in cts : <EOL> print ( "<STR_LIT>" % ( ct . app_label , ct . model ) ) <EOL> if to_remove : <EOL> if kwargs . get ( '<STR_LIT>' , False ) : <EOL> content_type_display = '<STR_LIT:\n>' . join ( [ <EOL> '<STR_LIT>' % ( ct . app_label , ct . model ) <EOL> for ct in to_remove <EOL> ] ) <EOL> ok_to_delete = input ( """<STR_LIT>""" % content_type_display ) <EOL> else : <EOL> ok_to_delete = False <EOL> if ok_to_delete == '<STR_LIT:yes>' : <EOL> for ct in to_remove : <EOL> if verbosity >= <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" % ( ct . app_label , ct . model ) ) <EOL> ct . delete ( ) <EOL> else : <EOL> if verbosity >= <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" ) <EOL> def update_all_contenttypes ( verbosity = <NUM_LIT:2> , ** kwargs ) : <EOL> for app in get_apps ( ) : <EOL> update_contenttypes ( app , None , verbosity , ** kwargs ) <EOL> signals . post_syncdb . connect ( update_contenttypes ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> update_all_contenttypes ( ) </s>
