<s> """<STR_LIT>""" <EOL> from optparse import make_option <EOL> import django <EOL> from django . core . management . base import NoArgsCommand <EOL> from django . core . management . color import no_style <EOL> from django . db import connection , transaction <EOL> from django . utils . six import moves <EOL> from wagtail_modeltranslation . settings import AVAILABLE_LANGUAGES <EOL> from wagtail_modeltranslation . translator import translator <EOL> from wagtail_modeltranslation . utils import build_localized_fieldname <EOL> def ask_for_confirmation ( sql_sentences , model_full_name , interactive ) : <EOL> print ( '<STR_LIT>' % model_full_name ) <EOL> for sentence in sql_sentences : <EOL> print ( '<STR_LIT>' % sentence ) <EOL> while True : <EOL> prompt = '<STR_LIT>' <EOL> if interactive : <EOL> answer = moves . input ( prompt ) . strip ( ) <EOL> else : <EOL> answer = '<STR_LIT:y>' <EOL> if answer == '<STR_LIT>' : <EOL> return False <EOL> elif answer not in ( '<STR_LIT:y>' , '<STR_LIT:n>' , '<STR_LIT:yes>' , '<STR_LIT>' ) : <EOL> print ( '<STR_LIT>' ) <EOL> elif answer == '<STR_LIT:y>' or answer == '<STR_LIT:yes>' : <EOL> return True <EOL> else : <EOL> return False <EOL> def print_missing_langs ( missing_langs , field_name , model_name ) : <EOL> print ( '<STR_LIT>' % ( <EOL> field_name , model_name , "<STR_LIT:U+002CU+0020>" . join ( missing_langs ) ) ) <EOL> class Command ( NoArgsCommand ) : <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> option_list = NoArgsCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def handle_noargs ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> self . cursor = connection . cursor ( ) <EOL> self . introspection = connection . introspection <EOL> self . interactive = options [ '<STR_LIT>' ] <EOL> found_missing_fields = False <EOL> models = translator . get_registered_models ( abstract = False ) <EOL> for model in models : <EOL> db_table = model . _meta . db_table <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:8> ) : <EOL> model_name = model . _meta . module_name <EOL> else : <EOL> model_name = model . _meta . model_name <EOL> model_full_name = '<STR_LIT>' % ( model . _meta . app_label , model_name ) <EOL> opts = translator . get_options_for_model ( model ) <EOL> for field_name , fields in opts . local_fields . items ( ) : <EOL> field = list ( fields ) [ <NUM_LIT:0> ] <EOL> column_name = field . db_column if field . db_column else field_name <EOL> missing_langs = list ( self . get_missing_languages ( column_name , db_table ) ) <EOL> if missing_langs : <EOL> found_missing_fields = True <EOL> print_missing_langs ( missing_langs , field_name , model_full_name ) <EOL> sql_sentences = self . get_sync_sql ( field_name , missing_langs , model ) <EOL> execute_sql = ask_for_confirmation ( <EOL> sql_sentences , model_full_name , self . interactive ) <EOL> if execute_sql : <EOL> print ( '<STR_LIT>' ) <EOL> for sentence in sql_sentences : <EOL> self . cursor . execute ( sentence ) <EOL> print ( '<STR_LIT>' ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:6> ) : <EOL> transaction . commit_unless_managed ( ) <EOL> if not found_missing_fields : <EOL> print ( '<STR_LIT>' ) <EOL> def get_table_fields ( self , db_table ) : <EOL> """<STR_LIT>""" <EOL> db_table_desc = self . introspection . get_table_description ( self . cursor , db_table ) <EOL> return [ t [ <NUM_LIT:0> ] for t in db_table_desc ] <EOL> def get_missing_languages ( self , field_name , db_table ) : <EOL> """<STR_LIT>""" <EOL> db_table_fields = self . get_table_fields ( db_table ) <EOL> for lang_code in AVAILABLE_LANGUAGES : <EOL> if build_localized_fieldname ( field_name , lang_code ) not in db_table_fields : <EOL> yield lang_code <EOL> def get_sync_sql ( self , field_name , missing_langs , model ) : <EOL> """<STR_LIT>""" <EOL> qn = connection . ops . quote_name <EOL> style = no_style ( ) <EOL> sql_output = [ ] <EOL> db_table = model . _meta . db_table <EOL> for lang in missing_langs : <EOL> new_field = build_localized_fieldname ( field_name , lang ) <EOL> f = model . _meta . get_field ( new_field ) <EOL> col_type = f . db_type ( connection = connection ) <EOL> field_sql = [ style . SQL_FIELD ( qn ( f . column ) ) , style . SQL_COLTYPE ( col_type ) ] <EOL> stmt = "<STR_LIT>" % ( qn ( db_table ) , '<STR_LIT:U+0020>' . join ( field_sql ) ) <EOL> if not f . null : <EOL> stmt += "<STR_LIT:U+0020>" + style . SQL_KEYWORD ( '<STR_LIT>' ) <EOL> sql_output . append ( stmt + "<STR_LIT:;>" ) <EOL> return sql_output </s>
