Skip to content

Commit

Permalink
Merge pull request #22788 from dimagi/rn_delete_telangana
Browse files Browse the repository at this point in the history
Script to delete all users from telangana
  • Loading branch information
Rohit25negi committed Dec 19, 2018
2 parents c84460e + 2323b6e commit 182a143
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions custom/icds_reports/management/commands/delete_users_for_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function

from django.core.management.base import BaseCommand

from corehq.apps.locations.models import SQLLocation
from corehq.apps.users.models import CommCareUser


class Command(BaseCommand):

def add_arguments(self, parser):
parser.add_argument(
'user_list_file'
)
parser.add_argument(
'site_code'
)

def handle(self, user_list_file, site_code, *args, **kwargs):

with open(user_list_file) as fin:
users_to_delete = fin.readlines()
users_to_delete = [user.strip() for user in users_to_delete]
choice = raw_input('Total {} users will be deleted: (Y/N)'.format(len(users_to_delete)))
if not choice.startswith('y'):
print("Nice!!! Its good to be safe than to be sorry")
return

print("Deleting users")
users_submitted_forms = list()
for user in users_to_delete:
user_name = user + '@icds-cas.commcarehq.org'
mobile_user = CommCareUser.get_by_username(user_name)
if not mobile_user:
continue
if not mobile_user.reporting_metadata.last_submission_for_user.submission_date and\
not mobile_user.reporting_metadata.last_sync_for_user.sync_date:
mobile_user.retire()
else:
users_submitted_forms.append(mobile_user)

print("users submitted forms:")
print('\n'.join([user.username for user in users_submitted_forms]))

print('\n{} users deleted. '
'Deleting Location with site_code {}'.format(len(users_to_delete)-len(users_submitted_forms),
site_code))

location_to_delete = SQLLocation.objects.get(domain='icds-cas', site_code=site_code)

location_to_delete.delete()

print("Deleted Location as well. Thank you for Using our services")


0 comments on commit 182a143

Please sign in to comment.