<s> from __future__ import absolute_import , print_function , unicode_literals <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from django . core . management import CommandError <EOL> from django . db import transaction <EOL> from cms . api import copy_plugins_to_language <EOL> from cms . management . commands . subcommands . base import SubcommandsCommand <EOL> from cms . models import Page , StaticPlaceholder , EmptyTitle <EOL> from cms . utils import get_language_list <EOL> from cms . utils . copy_plugins import copy_plugins_to <EOL> class CopyLangCommand ( SubcommandsCommand ) : <EOL> help_string = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> command_name = '<STR_LIT>' <EOL> label = '<STR_LIT>' <EOL> def add_arguments ( self , parser ) : <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = True , help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def handle ( self , * args , ** options ) : <EOL> verbose = options . get ( '<STR_LIT>' ) > <NUM_LIT:1> <EOL> only_empty = options . get ( '<STR_LIT>' ) <EOL> copy_content = options . get ( '<STR_LIT>' ) <EOL> from_lang = options . get ( '<STR_LIT>' ) <EOL> to_lang = options . get ( '<STR_LIT>' ) <EOL> try : <EOL> site = int ( options . get ( '<STR_LIT>' , None ) ) <EOL> except Exception : <EOL> site = settings . SITE_ID <EOL> try : <EOL> assert from_lang in get_language_list ( site ) <EOL> assert to_lang in get_language_list ( site ) <EOL> except AssertionError : <EOL> raise CommandError ( '<STR_LIT>' ) <EOL> for page in Page . objects . on_site ( site ) . drafts ( ) : <EOL> if from_lang in page . get_languages ( ) : <EOL> title = page . get_title_obj ( to_lang , fallback = False ) <EOL> if isinstance ( title , EmptyTitle ) : <EOL> title = page . get_title_obj ( from_lang ) <EOL> if verbose : <EOL> self . stdout . write ( '<STR_LIT>' % ( title . title , from_lang ) ) <EOL> title . id = None <EOL> title . publisher_public_id = None <EOL> title . publisher_state = <NUM_LIT:0> <EOL> title . language = to_lang <EOL> title . save ( ) <EOL> if copy_content : <EOL> if verbose : <EOL> self . stdout . write ( '<STR_LIT>' % ( page . get_page_title ( from_lang ) , from_lang ) ) <EOL> copy_plugins_to_language ( page , from_lang , to_lang , only_empty ) <EOL> else : <EOL> if verbose : <EOL> self . stdout . write ( '<STR_LIT>' % ( page . get_page_title ( page . get_languages ( ) [ <NUM_LIT:0> ] ) , from_lang ) ) <EOL> if copy_content : <EOL> for static_placeholder in StaticPlaceholder . objects . all ( ) : <EOL> plugin_list = [ ] <EOL> for plugin in static_placeholder . draft . get_plugins ( ) : <EOL> if plugin . language == from_lang : <EOL> plugin_list . append ( plugin ) <EOL> if plugin_list : <EOL> if verbose : <EOL> self . stdout . write ( <EOL> '<STR_LIT>' % ( <EOL> static_placeholder . name , from_lang , to_lang ) <EOL> ) <EOL> copy_plugins_to ( plugin_list , static_placeholder . draft , to_lang ) <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> class CopySiteCommand ( SubcommandsCommand ) : <EOL> help_string = '<STR_LIT>' <EOL> command_name = '<STR_LIT>' <EOL> def add_arguments ( self , parser ) : <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> def handle ( self , * args , ** options ) : <EOL> try : <EOL> from_site = int ( options . get ( '<STR_LIT>' , None ) ) <EOL> except Exception : <EOL> from_site = settings . SITE_ID <EOL> try : <EOL> to_site = int ( options . get ( '<STR_LIT>' , None ) ) <EOL> except Exception : <EOL> to_site = settings . SITE_ID <EOL> try : <EOL> assert from_site != to_site <EOL> except AssertionError : <EOL> raise CommandError ( '<STR_LIT>' ) <EOL> from_site = self . get_site ( from_site ) <EOL> to_site = self . get_site ( to_site ) <EOL> pages = Page . objects . drafts ( ) . filter ( site = from_site , depth = <NUM_LIT:1> ) <EOL> with transaction . atomic ( ) : <EOL> for page in pages : <EOL> page . copy_page ( None , to_site ) <EOL> self . stdout . write ( '<STR_LIT>' . format ( from_site . pk , to_site . pk ) ) <EOL> def get_site ( self , site_id ) : <EOL> if site_id : <EOL> try : <EOL> return Site . objects . get ( pk = site_id ) <EOL> except ( ValueError , Site . DoesNotExist ) : <EOL> raise CommandError ( '<STR_LIT>' ) <EOL> else : <EOL> return None <EOL> class CopyCommand ( SubcommandsCommand ) : <EOL> help_string = '<STR_LIT>' <EOL> command_name = '<STR_LIT>' <EOL> missing_args_message = '<STR_LIT>' <EOL> subcommands = { <EOL> '<STR_LIT>' : CopyLangCommand , <EOL> '<STR_LIT>' : CopySiteCommand <EOL> } </s>
