|
| 1 | +# coding: utf-8 |
| 2 | +from django.core.management.base import BaseCommand |
| 3 | +from django.contrib.auth.models import User |
| 4 | +from django.db.models import Q |
| 5 | + |
| 6 | +def display_users(users_list): |
| 7 | + print "-" * 80 |
| 8 | + print "| pk | username | first_name | last_name | email |" |
| 9 | + for user in users_list: |
| 10 | + print "| %s | %s | %s | %s | %s |" % (user.pk, user.username, user.first_name, user.last_name, user.email) |
| 11 | + print "-" * 80 |
| 12 | + print "total: %s users" % users_list.count() |
| 13 | + print "-" * 80 |
| 14 | + |
| 15 | + |
| 16 | +class Command(BaseCommand): |
| 17 | + help = 'replace sensible data to be anonymized, such as first_name, last_name, email, api tokens, etc' |
| 18 | + |
| 19 | + def handle(self, *args, **options): |
| 20 | + print "#" * 80 |
| 21 | + print "# THIS COMMAND WILL MODIFY USER DATA, AND SHOULD AFFECT THE LOGIN CREDENTIALS! #" |
| 22 | + print "# ----------- PLEASE BE SURE YOU HAVE A BACKUP TO AVOID DATA LOSS ------------ #" |
| 23 | + print "#" * 80 |
| 24 | + prompt_backup = raw_input('the database has a back up? [y/N]: ') |
| 25 | + if prompt_backup.lower() == 'y': |
| 26 | + users = User.objects.all() |
| 27 | + print "Found %s users!" % users.count() |
| 28 | + prompt_show_all_users = raw_input('want to list all these users? [y/N]: ') |
| 29 | + if prompt_show_all_users.lower() == 'y': |
| 30 | + display_users(users) |
| 31 | + |
| 32 | + # exclude non-scielo users |
| 33 | + non_scielo_users = users.exclude(email__endswith="@scielo.org") |
| 34 | + print "Found %s NON-scielo users!" % non_scielo_users.count() |
| 35 | + prompt_show_non_scielo_users = raw_input('want to list all these users? [y/N]: ') |
| 36 | + if prompt_show_non_scielo_users.lower() == 'y': |
| 37 | + display_users(non_scielo_users) |
| 38 | + |
| 39 | + # lookup to know if exists particular users to be excludes, such as: QAL1, QAL2, Produtor, etc |
| 40 | + has_special_users = non_scielo_users.filter( |
| 41 | + Q(first_name__iexact="Produtor") | Q(first_name__iexact="QAL1") | Q(first_name__iexact="QAL2") |
| 42 | + ).exists() |
| 43 | + |
| 44 | + if has_special_users: |
| 45 | + prompt_to_exclude_special_users = raw_input( |
| 46 | + 'Found at least one special user (QAL1 or QAL2 or Produtor). Do you want to ignore this users from modifications? [y/N]: ' |
| 47 | + ) |
| 48 | + if prompt_to_exclude_special_users.lower() == 'y': |
| 49 | + non_scielo_users = non_scielo_users.exclude( |
| 50 | + Q(first_name__iexact="Produtor") | Q(first_name__iexact="QAL1") | Q(first_name__iexact="QAL2") |
| 51 | + ) |
| 52 | + print "Now the list of NON-scielo users to be modified has %s users" % non_scielo_users.count() |
| 53 | + prompt_show_non_scielo_users = raw_input('want to list all these users? [y/N]: ') |
| 54 | + if prompt_show_non_scielo_users.lower() == 'y': |
| 55 | + display_users(non_scielo_users) |
| 56 | + |
| 57 | + print "#" * 80 |
| 58 | + print "# NOW WILL MODIFY USER DATA! #" |
| 59 | + print "# user.username will be set to user_<user.pk> #" |
| 60 | + print "# user.first_name will be set to user_fn_<user.pk> #" |
| 61 | + print "# user.last_name will be set to user_ln_<user.pk> #" |
| 62 | + print "# user.email will be set to user_<user.pk>@example.com #" |
| 63 | + print "# user.password will be set to 'test.scielo' [hashed] #" |
| 64 | + print "# user.api_key will be regenerated with a random uuid using tastypie.models > ApiKey > generate_key #" |
| 65 | + print "# ----------- BE SURE YOU HAVE A BACKUP TO AVOID DATA LOSS ------------ #" |
| 66 | + print "#" * 80 |
| 67 | + prompt_confirm_modify = raw_input('Are you sure? the process CAN NOT BE UNDONE [y/N]: ') |
| 68 | + if prompt_confirm_modify.lower() == 'y': |
| 69 | + print "Updating users ... (hold on, may take a while) ..." |
| 70 | + for user in non_scielo_users: |
| 71 | + user.username = "user_%s" % user.pk |
| 72 | + user.first_name = "user_fn_%s" % user.pk |
| 73 | + user.last_name = "user_ln_%s" % user.pk |
| 74 | + user. email = "user_%[email protected]" % user. pk |
| 75 | + user.set_password('test.scielo') |
| 76 | + # save new user field |
| 77 | + user.save() |
| 78 | + # generate a new api_key |
| 79 | + user.api_key.key = None |
| 80 | + user.api_key.save() |
| 81 | + # show updated user info |
| 82 | + display_users(non_scielo_users) |
| 83 | + print "done!" |
| 84 | + else: |
| 85 | + print "Nothing to do here! NON-scielo users were NOT changed!" |
| 86 | + else: |
| 87 | + print "Nothing to do here! Go and make a backup." |
0 commit comments