|
1 """ |
|
2 set_fake_emails.py |
|
3 |
|
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 """ |
|
9 from optparse import make_option |
|
10 |
|
11 from django.conf import settings |
|
12 from django.core.management.base import NoArgsCommand, CommandError |
|
13 |
|
14 DEFAULT_FAKE_EMAIL = '%(username)s@example.com' |
|
15 |
|
16 class Command(NoArgsCommand): |
|
17 option_list = NoArgsCommand.option_list + ( |
|
18 make_option('--email', dest='default_email', default=DEFAULT_FAKE_EMAIL, |
|
19 help='Use this as the new email format.'), |
|
20 make_option('-a', '--no-admin', action="store_true", dest='no_admin', default=False, |
|
21 help='Do not change administrator accounts'), |
|
22 make_option('-s', '--no-staff', action="store_true", dest='no_staff', default=False, |
|
23 help='Do not change staff accounts'), |
|
24 make_option('--include', dest='include_regexp', default=None, |
|
25 help='Include usernames matching this regexp.'), |
|
26 make_option('--exclude', dest='exclude_regexp', default=None, |
|
27 help='Exclude usernames matching this regexp.'), |
|
28 make_option('--include-groups', dest='include_groups', default=None, |
|
29 help='Include users matching this group. (use comma seperation for multiple groups)'), |
|
30 make_option('--exclude-groups', dest='exclude_groups', default=None, |
|
31 help='Exclude users matching this group. (use comma seperation for multiple groups)'), |
|
32 ) |
|
33 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, ) |
|
34 requires_model_validation = False |
|
35 |
|
36 def handle_noargs(self, **options): |
|
37 if not settings.DEBUG: |
|
38 raise CommandError('Only available in debug mode') |
|
39 |
|
40 from django.contrib.auth.models import User, Group |
|
41 email = options.get('default_email', DEFAULT_FAKE_EMAIL) |
|
42 include_regexp = options.get('include_regexp', None) |
|
43 exclude_regexp = options.get('exclude_regexp', None) |
|
44 include_groups = options.get('include_groups', None) |
|
45 exclude_groups = options.get('exclude_groups', None) |
|
46 no_admin = options.get('no_admin', False) |
|
47 no_staff = options.get('no_staff', False) |
|
48 |
|
49 users = User.objects.all() |
|
50 if no_admin: |
|
51 users = users.exclude(is_superuser=True) |
|
52 if no_staff: |
|
53 users = users.exclude(is_staff=True) |
|
54 if exclude_groups: |
|
55 groups = Group.objects.filter(name__in=exclude_groups.split(",")) |
|
56 if groups: |
|
57 users = users.exclude(groups__in=groups) |
|
58 else: |
|
59 raise CommandError("No group matches filter: %s" % exclude_groups) |
|
60 if include_groups: |
|
61 groups = Group.objects.filter(name__in=include_groups.split(",")) |
|
62 if groups: |
|
63 users = users.filter(groups__in=groups) |
|
64 else: |
|
65 raise CommandError("No groups matches filter: %s" % include_groups) |
|
66 if exclude_regexp: |
|
67 users = users.exclude(username__regex=exclude_regexp) |
|
68 if include_regexp: |
|
69 users = users.filter(username__regex=include_regexp) |
|
70 for user in users: |
|
71 user.email = email % {'username': user.username, |
|
72 'first_name': user.first_name, |
|
73 'last_name': user.last_name} |
|
74 user.save() |
|
75 print 'Changed %d emails' % users.count() |