Revision aae2103e snf-astakos-app/astakos/im/management/commands/user-add.py
b/snf-astakos-app/astakos/im/management/commands/user-add.py | ||
---|---|---|
37 | 37 |
|
38 | 38 |
from django.core.management.base import BaseCommand, CommandError |
39 | 39 |
from django.core.validators import validate_email |
40 |
from django.core.exceptions import ValidationError
|
|
40 |
from snf_django.lib.db import transaction
|
|
41 | 41 |
|
42 | 42 |
from astakos.im.models import AstakosUser |
43 |
from astakos.im.functions import activate |
|
44 | 43 |
|
45 | 44 |
|
46 | 45 |
class Command(BaseCommand): |
... | ... | |
52 | 51 |
dest='password', |
53 | 52 |
metavar='PASSWORD', |
54 | 53 |
help="Set user's password"), |
55 |
make_option('--active', |
|
56 |
action='store_true', |
|
57 |
dest='active', |
|
58 |
default=False, |
|
59 |
help="Set active"), |
|
60 | 54 |
make_option('--admin', |
61 | 55 |
action='store_true', |
62 | 56 |
dest='is_superuser', |
... | ... | |
65 | 59 |
make_option('-g', |
66 | 60 |
action='append', |
67 | 61 |
dest='groups', |
62 |
default=[], |
|
68 | 63 |
help="Add user group (may be used multiple times)"), |
69 | 64 |
make_option('-p', |
70 | 65 |
action='append', |
71 | 66 |
dest='permissions', |
67 |
default=[], |
|
72 | 68 |
help="Add user permission (may be used multiple times)") |
73 | 69 |
) |
74 | 70 |
|
71 |
@transaction.commit_on_success_strict() |
|
75 | 72 |
def handle(self, *args, **options): |
76 | 73 |
if len(args) != 3: |
77 | 74 |
raise CommandError("Invalid number of arguments") |
... | ... | |
79 | 76 |
email, first_name, last_name = map(lambda arg: arg.decode('utf8'), |
80 | 77 |
args[:3]) |
81 | 78 |
|
79 |
password = options['password'] or \ |
|
80 |
AstakosUser.objects.make_random_password() |
|
81 |
|
|
82 | 82 |
try: |
83 | 83 |
validate_email(email) |
84 | 84 |
except ValidationError: |
85 | 85 |
raise CommandError("Invalid email") |
86 | 86 |
|
87 |
password = options['password'] or \ |
|
88 |
AstakosUser.objects.make_random_password() |
|
89 |
|
|
90 | 87 |
try: |
91 | 88 |
u = AstakosUser(email=email, |
92 | 89 |
first_name=first_name, |
... | ... | |
108 | 105 |
u.add_auth_provider('local') |
109 | 106 |
map(u.add_permission, options['permissions']) |
110 | 107 |
map(u.add_group, options['groups']) |
111 |
|
|
112 |
if options['active']: |
|
113 |
activate(u) |
|
114 | 108 |
except BaseException, e: |
115 | 109 |
import traceback |
116 | 110 |
traceback.print_exc() |
Also available in: Unified diff