from molo.core import models
from django.core.management.base import BaseCommand
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('old_lang_pk', type=str)
        parser.add_argument('new_lang_pk', type=str)

    def handle(self, *args, **options):
        old_lang_pk = options.get('old_lang_pk', None)
        new_lang_pk = options.get('new_lang_pk', None)
        if not old_lang_pk or not new_lang_pk or old_lang_pk == new_lang_pk:
            self.stdout.write(self.style.WARNING(
                "Invalid language primary keys were entered" +
                str(old_lang_pk) + " and " + str(new_lang_pk)))
        else:
            try:
                old_lang = models.SiteLanguage.objects.get(pk=old_lang_pk)
                new_lang = models.SiteLanguage.objects.get(pk=new_lang_pk)
                if not old_lang or not new_lang:
                    self.stdout.write(self.style.WARNING(
                        "Invalid language primary keys were entered" +
                        str(old_lang_pk) + " and " + str(new_lang_pk)))
                else:
                    pages = models.MoloPage.objects.all()
                    for page in pages:
                        if hasattr(page.specific, 'language'):
                            if page.specific.language == old_lang:
                                page.specific.language = new_lang
                                if not page.specific.has_unpublished_changes:
                                    page.specific.save_revision().publish()
                                else:
                                    page.specific.save_revision().publish()
                                    page.specific.unpublish()
            except (ObjectDoesNotExist, MultipleObjectsReturned):
                self.stdout.write(self.style.WARNING(
                    "Invalid language primary keys were entered" +
                    str(old_lang_pk) + " and " + str(new_lang_pk)))

@@ -131,33 +131,8 @@
 xist
-, MultipleObjectsReturned
 %0A%0A%0Ac
@@ -489,38 +489,8 @@
  not
- new_lang_pk or old_lang_pk ==
  new
@@ -570,53 +570,75 @@
    %22
-Invalid language primary keys were entered%22 +
+Please enter the old language pk followed by the new language pk%22))
 %0A   
@@ -638,32 +638,33 @@
 %22))%0A        
+%0A
         
 str(old_lang
@@ -643,36 +643,37 @@
        %0A        
-str(
+elif 
 old_lang_pk) + %22
@@ -671,40 +671,149 @@
 g_pk
-) + %22 and %22 + str(new_
+ == new_lang_pk:%0A            self.stdout.write(self.style.WARNING(%0A                %22The old language pk and new 
 lang
-_pk)))
+uage pk are the same.%22))%0A
 %0A   
@@ -994,284 +994,8 @@
 pk)%0A
-                if not old_lang or not new_lang:%0A                    self.stdout.write(self.style.WARNING(%0A                        %22Invalid language primary keys were entered%22 +%0A                        str(old_lang_pk) + %22 and %22 + str(new_lang_pk)))%0A                else:%0A    
     
@@ -1060,20 +1060,16 @@
         
-    
 for page
@@ -1075,28 +1075,24 @@
 e in pages:%0A
-    
             
@@ -1162,20 +1162,16 @@
         
-    
 if page.
@@ -1201,20 +1201,16 @@
 d_lang:%0A
-    
         
@@ -1291,26 +1291,18 @@
         
-    if not
+if
  page.sp
@@ -1312,37 +1312,33 @@
 fic.
-has_unpublished_changes:%0A    
+status_string == 'live':%0A
     
@@ -1397,36 +1397,32 @@
 ion().publish()%0A
-    
                 
@@ -1463,36 +1463,32 @@
                 
-    
 page.specific.sa
@@ -1503,36 +1503,32 @@
 ion().publish()%0A
-    
                 
@@ -1611,33 +1611,8 @@
 xist
-, MultipleObjectsReturned
 ):%0A 
@@ -1689,50 +1689,45 @@
    %22
-Invalid language primary keys were entered
+One or more of the languages with PKs
 %22 +%0A
@@ -1791,11 +1791,32 @@
 lang_pk)
+ + %22 does not exist.%22
 ))%0A

use status_string