@@ -0,0 +1,1325 @@
+from django.core.management.base import BaseCommand%0Afrom corehq.apps.es import UserES%0Afrom corehq.apps.users.models import CommCareUser%0Afrom corehq.util.couch import iter_update, DocUpdate%0Afrom corehq.util.log import with_progress_bar%0A%0A%0Aclass Command(BaseCommand):%0A    args = %22%22%0A    help = (%22Clears commcare_location_id and commtrack-supply-point on any %22%0A            %22users without location_id set%22)%0A%0A    def handle(self, *args, **options):%0A        clean_users()%0A%0A%0Adef get_bad_user_ids():%0A    res = (UserES()%0A           .mobile_users()%0A           .empty(%22location_id%22)%0A           .fields(%5B%22_id%22, %22domain%22, %22username%22, %22user_data.commcare_location_id%22%5D)%0A           .run().hits)%0A    return %5Bu%5B'_id'%5D for u in res%0A            if u.get('user_data', %7B%7D).get('commcare_location_id')%0A            or u.get('user_data', %7B%7D).get('commtrack-supply-point')%5D%0A%0A%0Adef clean_user(doc):%0A    %22%22%22Take any users with no location_id and clear the location user_data%22%22%22%0A    if doc%5B'location_id'%5D:%0A        return%0A%0A    if (%0A        doc.get('user_data', %7B%7D).pop('commcare_location_id', False)%0A        or doc.get('user_data', %7B%7D).pop('commtrack-supply-point', False)%0A    ):%0A        return DocUpdate(doc)%0A%0A%0Adef clean_users():%0A    all_ids = with_progress_bar(get_bad_user_ids())%0A    iter_update(CommCareUser.get_db(), clean_user, all_ids, verbose=True)%0A

Add mgmt cmd to clear bad location user data