root / snf-astakos-app / astakos / im / management / commands / user-add.py @ 21e0fdad
History | View | Annotate | Download (4.4 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import socket |
35 |
|
36 |
from optparse import make_option |
37 |
|
38 |
from django.core.management.base import BaseCommand, CommandError |
39 |
from django.core.validators import validate_email |
40 |
from django.core.exceptions import ValidationError |
41 |
|
42 |
from astakos.im.models import AstakosUser |
43 |
from astakos.im.api.callpoint import AstakosCallpoint |
44 |
|
45 |
def filter_custom_options(options): |
46 |
base_dests = list(
|
47 |
getattr(o, 'dest', None) for o in BaseCommand.option_list) |
48 |
return dict((k, v) for k, v in options.iteritems() if k not in base_dests) |
49 |
|
50 |
|
51 |
class Command(BaseCommand): |
52 |
args = "<email> <first name> <last name>"
|
53 |
help = "Create a user"
|
54 |
|
55 |
option_list = BaseCommand.option_list + ( |
56 |
make_option('--affiliation',
|
57 |
dest='affiliation',
|
58 |
metavar='AFFILIATION',
|
59 |
help="Set user's affiliation"),
|
60 |
make_option('--password',
|
61 |
dest='password',
|
62 |
metavar='PASSWORD',
|
63 |
help="Set user's password"),
|
64 |
make_option('--active',
|
65 |
action='store_true',
|
66 |
dest='is_active',
|
67 |
default=False,
|
68 |
help="Activate user"),
|
69 |
make_option('--admin',
|
70 |
action='store_true',
|
71 |
dest='is_superuser',
|
72 |
default=False,
|
73 |
help="Give user admin rights"),
|
74 |
make_option('-g',
|
75 |
action='append',
|
76 |
dest='groups',
|
77 |
help="Add user group (may be used multiple times)"),
|
78 |
make_option('-p',
|
79 |
action='append',
|
80 |
dest='permissions',
|
81 |
help="Add user permission (may be used multiple times)")
|
82 |
) |
83 |
|
84 |
def handle(self, *args, **options): |
85 |
if len(args) != 3: |
86 |
raise CommandError("Invalid number of arguments") |
87 |
|
88 |
email, first_name, last_name = (args[i].decode('utf8') for i in range(3)) |
89 |
|
90 |
try:
|
91 |
validate_email(email) |
92 |
except ValidationError:
|
93 |
raise CommandError("Invalid email") |
94 |
|
95 |
u = {'email': email,
|
96 |
'first_name':first_name,
|
97 |
'last_name':last_name
|
98 |
} |
99 |
u.update(filter_custom_options(options)) |
100 |
if not u.get('password'): |
101 |
u['password'] = AstakosUser.objects.make_random_password()
|
102 |
|
103 |
try:
|
104 |
c = AstakosCallpoint() |
105 |
r = c.create_users((u,)).next() |
106 |
except socket.error, e:
|
107 |
raise CommandError(e)
|
108 |
except ValidationError, e:
|
109 |
raise CommandError(e)
|
110 |
else:
|
111 |
if not r.is_success: |
112 |
raise CommandError(r.reason)
|
113 |
else:
|
114 |
self.stdout.write('User created successfully ') |
115 |
if not options.get('password'): |
116 |
self.stdout.write('with password: %s\n' % u['password']) |
117 |
else:
|
118 |
self.stdout.write('\n') |