set_fake_emails.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. """
  3. set_fake_emails.py
  4. Give all users a new email account. Useful for testing in a
  5. development environment. As such, this command is only available when
  6. setting.DEBUG is True.
  7. """
  8. from django.conf import settings
  9. from django.contrib.auth import get_user_model
  10. from django.core.management.base import BaseCommand, CommandError
  11. from django_extensions.management.utils import signalcommand
  12. DEFAULT_FAKE_EMAIL = '%(username)s@example.com'
  13. class Command(BaseCommand):
  14. help = '''DEBUG only: give all users a new email based on their account data ("%s" by default). Possible parameters are: username, first_name, last_name''' % (DEFAULT_FAKE_EMAIL, )
  15. requires_system_checks = False
  16. def add_arguments(self, parser):
  17. super().add_arguments(parser)
  18. parser.add_argument(
  19. '--email', dest='default_email', default=DEFAULT_FAKE_EMAIL,
  20. help='Use this as the new email format.'
  21. )
  22. parser.add_argument(
  23. '-a', '--no-admin', action="store_true", dest='no_admin',
  24. default=False, help='Do not change administrator accounts'
  25. )
  26. parser.add_argument(
  27. '-s', '--no-staff', action="store_true", dest='no_staff',
  28. default=False, help='Do not change staff accounts'
  29. )
  30. parser.add_argument(
  31. '--include', dest='include_regexp', default=None,
  32. help='Include usernames matching this regexp.'
  33. )
  34. parser.add_argument(
  35. '--exclude', dest='exclude_regexp', default=None,
  36. help='Exclude usernames matching this regexp.'
  37. )
  38. parser.add_argument(
  39. '--include-groups', dest='include_groups', default=None,
  40. help='Include users matching this group. (use comma seperation for multiple groups)'
  41. )
  42. parser.add_argument(
  43. '--exclude-groups', dest='exclude_groups', default=None,
  44. help='Exclude users matching this group. (use comma seperation for multiple groups)'
  45. )
  46. @signalcommand
  47. def handle(self, *args, **options):
  48. if not settings.DEBUG:
  49. raise CommandError('Only available in debug mode')
  50. from django.contrib.auth.models import Group
  51. email = options['default_email']
  52. include_regexp = options['include_regexp']
  53. exclude_regexp = options['exclude_regexp']
  54. include_groups = options['include_groups']
  55. exclude_groups = options['exclude_groups']
  56. no_admin = options['no_admin']
  57. no_staff = options['no_staff']
  58. User = get_user_model()
  59. users = User.objects.all()
  60. if no_admin:
  61. users = users.exclude(is_superuser=True)
  62. if no_staff:
  63. users = users.exclude(is_staff=True)
  64. if exclude_groups:
  65. groups = Group.objects.filter(name__in=exclude_groups.split(","))
  66. if groups:
  67. users = users.exclude(groups__in=groups)
  68. else:
  69. raise CommandError("No groups matches filter: %s" % exclude_groups)
  70. if include_groups:
  71. groups = Group.objects.filter(name__in=include_groups.split(","))
  72. if groups:
  73. users = users.filter(groups__in=groups)
  74. else:
  75. raise CommandError("No groups matches filter: %s" % include_groups)
  76. if exclude_regexp:
  77. users = users.exclude(username__regex=exclude_regexp)
  78. if include_regexp:
  79. users = users.filter(username__regex=include_regexp)
  80. for user in users:
  81. user.email = email % {'username': user.username,
  82. 'first_name': user.first_name,
  83. 'last_name': user.last_name}
  84. user.save()
  85. print('Changed %d emails' % users.count())