-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22788 from dimagi/rn_delete_telangana
Script to delete all users from telangana
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
custom/icds_reports/management/commands/delete_users_for_state.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|