from django.core.management.base import LabelCommand, CommandError
from couchexport.models import SavedExportSchema
from optparse import make_option

OLD_ROOT_INDEX = "#.#"
NEW_ROOT_INDEX = "#"

class Command(LabelCommand):
    help = "Migrates over custom exports from the old format to the new format."
    args = ""
    label = ""
    
    option_list = LabelCommand.option_list + \
        (make_option('--dryrun', action='store_true', dest='dryrun', default=False,
            help="Don't do the actual migration, just print the output"),)

    
    def handle(self, *args, **options):
        if len(args) != 0: raise CommandError("This command doesn't expect arguments!")
            
        for export in SavedExportSchema.view("couchexport/saved_export_schemas", include_docs=True):
            print "migrating %s" % export
            if not hasattr(export, 'type'):
                export.type = 'form'
                if not options['dryrun']:
                    export.save()
        print "Done!" 
                

@@ -147,53 +147,8 @@
 on%0A%0A
-OLD_ROOT_INDEX = %22#.#%22%0ANEW_ROOT_INDEX = %22#%22%0A%0A
 clas
@@ -217,44 +217,55 @@
 rts 
-from the old format to the new forma
+by adding a default type property if not presen
 t.%22%0A
@@ -272,24 +272,36 @@
     args = %22
+default_type
 %22%0A    label 
@@ -303,16 +303,28 @@
 abel = %22
+default type
 %22%0A    %0A 
@@ -602,9 +602,9 @@
  != 
-0
+1
 : ra
@@ -625,45 +625,63 @@
 or(%22
-This command doesn't expect arguments
+Syntax: ./manage.py migrate_export_types %5Bdefault type%5D
 !%22)%0A
@@ -688,20 +688,47 @@
         
+%0A
     
+    default_type = args%5B0%5D
 %0A       
@@ -837,81 +837,73 @@
     
-print %22migrating %25s%22 %25 export%0A            if not hasattr(export, 'type'):
+if not export.type:%0A                print %22migrating %25s%22 %25 export
 %0A   
@@ -933,14 +933,20 @@
 e = 
-'form'
+default_type
 %0A   

fix export type migration command