@@ -0,0 +1,2132 @@
+from django.core.management.base import BaseCommand, CommandError%0Afrom optparse import make_option%0Afrom corehq.apps.sms.mixin import VerifiedNumber%0A%0A%0Aclass Command(BaseCommand):%0A    args = 'domain %5Bbackend_id%5D %5B--test%5D'%0A    help = ('Updates the backend_id on all VerifiedNumber entries for the '%0A        'given domain. If backend_id is not specified, it is set to None. '%0A        'VerifiedNumbers belonging to cases are not processed as the '%0A        'contact_backend_id case property must be updated to properly '%0A        'reflect that.')%0A    option_list = BaseCommand.option_list + (%0A        make_option('--test',%0A                    action='store_true',%0A                    dest='test',%0A                    default=False,%0A                    help=('Include this option to only print the backend_id '%0A                          'discrepancies and not update them.')),%0A    )%0A%0A    def handle(self, *args, **options):%0A        if len(args) == 0:%0A            raise CommandError('Usage: python manage.py set_backend_ids domain %5Bbackend_id%5D %5B--test%5D')%0A%0A        domain = args%5B0%5D%0A        if len(args) %3E 1:%0A            backend_id = args%5B1%5D%0A        else:%0A            backend_id = None%0A%0A        test_only = options%5B'test'%5D%0A%0A        for vn in VerifiedNumber.by_domain(domain):%0A            if (not vn.backend_id) and (not backend_id):%0A                pass%0A            elif vn.backend_id == backend_id:%0A                pass%0A            elif test_only:%0A                print '%25s %25s, number %25s has backend %25s instead of %25s' %25 %5C%0A                    (vn.owner_doc_type, vn.owner_id, vn.phone_number,%0A                     'None' if vn.backend_id is None else %22'%25s'%22 %25 vn.backend_id, backend_id)%0A            else:%0A                if vn.owner_doc_type == %22CommCareCase%22:%0A                    print 'Cannot update backend_id for %25s because it is a case' %25 vn.owner_id%0A                else:%0A                    print 'Updating backend_id from %25s to %25s for %25s %25s, number %25s' %25 %5C%0A                        (vn.backend_id, backend_id, vn.owner_doc_type, vn.owner_id, vn.phone_number)%0A                    vn.backend_id = backend_id%0A                    vn.save()%0A

Add script for setting contact-level backend ids in bulk