@@ -0,0 +1,3045 @@
+from corehq.apps.app_manager.models import Application%0Afrom corehq.apps.app_manager.xform import XForm, parse_xml%0Afrom couchforms.models import XFormInstance%0Afrom dimagi.utils.couch.database import iter_docs%0Afrom django.core.management.base import BaseCommand%0Afrom optparse import make_option%0A%0A%0Aclass Command(BaseCommand):%0A    help = 'fix all of the things'%0A%0A    option_list = BaseCommand.option_list + (%0A        make_option(%0A            '--dry-run',%0A            action='store_true',%0A            dest='dry_run',%0A            default=False,%0A            help=%22Don't do the actual modifications, just print what would be affected%22%0A        ),%0A    )%0A%0A    def handle(self, *args, **options):%0A%0A        dry_run = options.get(%22dry_run%22, True)%0A        new_xmlnss = %7B%7D%0A%0A        for xform_instance in get_submissions_without_xmlns():%0A            if xform_instance.app_id in new_xmlnss:%0A                set_xmlns_on_submission(xform_instance, new_xmlnss%5Bxform_instance.app_id%5D, dry_run)%0A            else:%0A                app = Application.get(xform_instance.app_id)%0A                forms_without_xmlns = get_forms_without_xmlns(app)%0A                if len(forms_without_xmlns) == 1:%0A                    new_xmlns = generate_random_xmlns()%0A                    new_xmlnss%5Bxform_instance.app_id%5D = new_xmlns%0A                    set_xmlns_on_form(forms_without_xmlns%5B0%5D, new_xmlns, dry_run)%0A                    set_xmlns_on_submission(xform_instance, new_xmlnss%5Bxform_instance.app_id%5D, dry_run)%0A                else:%0A                    # This will require more advanced logic%0A                    pass%0A%0A%0Adef get_submissions_without_xmlns():%0A    # TODO: This view won't get archived forms etc%0A    submission_ids = XFormInstance.get_db().view(%0A        'couchforms/by_xmlns',%0A        key=%22undefined%22,  # TODO: Is this the right key?%0A        include_docs=False%0A    ).all()%0A    return iter_docs(XFormInstance.get_db(), submission_ids)%0A    # Myabe all_forms/_view/view is better view to use?%0A%0A%0Adef set_xmlns_on_submission(xform_instance, xmlns, dry_run):%0A    %22%22%22%0A    Set the xmlns on an XFormInstance, and the save the document.%0A    %22%22%22%0A    xform_instance.xmlns = xmlns%0A    xform_instance.form%5B'@xmlns'%5D = xmlns%0A    # TODO: Modify the form.xml attachment%0A    if not dry_run:%0A        xform_instance.save()%0A%0A%0Adef set_xmlns_on_form(form, xmlns, dry_run):%0A    %22%22%22%0A    Set the xmlns on an app_manager.models.Form, and save the document.%0A    %22%22%22%0A    xml = form.source%0A    wrapped_xml = XForm(xml)%0A%0A    data = wrapped_xml.data_node.render()%0A    # TODO: Possible values for the first arg:%0A    #   %22undefined%22%0A    #   None%0A    #   %22http://www.w3.org/2002/xforms%22%0A    data = data.replace(%22undefined%22, xmlns, 1)%0A    wrapped_xml.instance_node.remove(wrapped_xml.data_node.xml)%0A    wrapped_xml.instance_node.append(parse_xml(data))%0A    new_xml = wrapped_xml.render()%0A%0A    form.source = new_xml%0A    if not dry_run:%0A        form.save()%0A%0A%0Adef get_forms_without_xmlns(app):%0A    # TODO%0A    raise NotImplementedError%0A%0Adef generate_random_xmlns():%0A    # TODO%0A    raise NotImplementedError%0A

Add management command