root / snf-astakos-app / astakos / im / management / commands / user-show.py @ 9b3081a6
History | View | Annotate | Download (7.5 kB)
1 |
# Copyright 2012, 2013 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 |
from django.core.management.base import CommandError |
35 |
from optparse import make_option |
36 |
|
37 |
from django.db.models import Q |
38 |
from astakos.im.models import AstakosUser, get_latest_terms, Project |
39 |
from astakos.im.quotas import list_user_quotas |
40 |
|
41 |
from synnefo.lib.ordereddict import OrderedDict |
42 |
from snf_django.management.commands import SynnefoCommand |
43 |
from snf_django.management import utils |
44 |
|
45 |
from ._common import show_user_quotas, style_options, check_style |
46 |
|
47 |
import uuid |
48 |
|
49 |
|
50 |
class Command(SynnefoCommand): |
51 |
args = "<user ID or email or uuid>"
|
52 |
help = "Show user info"
|
53 |
|
54 |
option_list = SynnefoCommand.option_list + ( |
55 |
make_option('--quota',
|
56 |
action='store_true',
|
57 |
dest='list_quotas',
|
58 |
default=False,
|
59 |
help="Also list user quota"),
|
60 |
make_option('--unit-style',
|
61 |
default='mb',
|
62 |
help=("Specify display unit for resource values "
|
63 |
"(one of %s); defaults to mb") % style_options),
|
64 |
make_option('--projects',
|
65 |
action='store_true',
|
66 |
dest='list_projects',
|
67 |
default=False,
|
68 |
help="Also list project memberships"),
|
69 |
) |
70 |
|
71 |
def handle(self, *args, **options): |
72 |
if len(args) != 1: |
73 |
raise CommandError("Please provide a user ID or email") |
74 |
|
75 |
identifier = args[0]
|
76 |
if identifier.isdigit():
|
77 |
users = AstakosUser.objects.filter(id=int(identifier))
|
78 |
else:
|
79 |
try:
|
80 |
uuid.UUID(identifier) |
81 |
except:
|
82 |
users = AstakosUser.objects.filter(email__iexact=identifier) |
83 |
else:
|
84 |
users = AstakosUser.objects.filter(uuid=identifier) |
85 |
if users.count() == 0: |
86 |
field = 'id' if identifier.isdigit() else 'email' |
87 |
msg = "Unknown user with %s '%s'" % (field, identifier)
|
88 |
raise CommandError(msg)
|
89 |
|
90 |
for user in users: |
91 |
kv = OrderedDict( |
92 |
[ |
93 |
('id', user.id),
|
94 |
('uuid', user.uuid),
|
95 |
('status', user.status_display),
|
96 |
('email', user.email),
|
97 |
('first name', user.first_name),
|
98 |
('last name', user.last_name),
|
99 |
('active', user.is_active),
|
100 |
('admin', user.is_superuser),
|
101 |
('last login', user.last_login),
|
102 |
('date joined', user.date_joined),
|
103 |
('last update', user.updated),
|
104 |
#('token', user.auth_token),
|
105 |
('token expiration', user.auth_token_expires),
|
106 |
('providers', user.auth_providers_display),
|
107 |
('verified', user.is_verified),
|
108 |
('groups', [elem.name for elem in user.groups.all()]), |
109 |
('permissions', [elem.codename
|
110 |
for elem in user.user_permissions.all()]), |
111 |
('group permissions', user.get_group_permissions()),
|
112 |
('email verified', user.email_verified),
|
113 |
('username', user.username),
|
114 |
('activation_sent_date', user.activation_sent),
|
115 |
]) |
116 |
|
117 |
if get_latest_terms():
|
118 |
has_signed_terms = user.signed_terms |
119 |
kv['has_signed_terms'] = has_signed_terms
|
120 |
if has_signed_terms:
|
121 |
kv['date_signed_terms'] = user.date_signed_terms
|
122 |
|
123 |
utils.pprint_table(self.stdout, [kv.values()], kv.keys(),
|
124 |
options["output_format"], vertical=True) |
125 |
|
126 |
if options["list_quotas"] and user.is_accepted(): |
127 |
unit_style = options["unit_style"]
|
128 |
check_style(unit_style) |
129 |
|
130 |
quotas, initial = list_user_quotas([user]) |
131 |
h_quotas = quotas[user.uuid] |
132 |
h_initial = initial[user.uuid] |
133 |
if quotas:
|
134 |
self.stdout.write("\n") |
135 |
print_data, labels = show_user_quotas(h_quotas, h_initial, |
136 |
style=unit_style) |
137 |
utils.pprint_table(self.stdout, print_data, labels,
|
138 |
options["output_format"],
|
139 |
title="User Quota")
|
140 |
|
141 |
if options["list_projects"]: |
142 |
print_data, labels = ownerships(user) |
143 |
if print_data:
|
144 |
self.stdout.write("\n") |
145 |
utils.pprint_table(self.stdout, print_data, labels,
|
146 |
options["output_format"],
|
147 |
title="Owned Projects")
|
148 |
|
149 |
print_data, labels = memberships(user) |
150 |
if print_data:
|
151 |
self.stdout.write("\n") |
152 |
utils.pprint_table(self.stdout, print_data, labels,
|
153 |
options["output_format"],
|
154 |
title="Project Memberships")
|
155 |
|
156 |
|
157 |
def memberships(user): |
158 |
ms = user.projectmembership_set.all() |
159 |
print_data = [] |
160 |
labels = ('project id', 'project name', 'status') |
161 |
|
162 |
for m in ms: |
163 |
project = m.project |
164 |
print_data.append((project.id, |
165 |
project.application.name, |
166 |
m.state_display(), |
167 |
)) |
168 |
return print_data, labels
|
169 |
|
170 |
|
171 |
def ownerships(user): |
172 |
chains = Project.objects.all_with_pending(Q(application__owner=user)) |
173 |
return chain_info(chains)
|
174 |
|
175 |
|
176 |
def chain_info(chains): |
177 |
labels = ('project id', 'project name', 'status', 'pending app id') |
178 |
l = [] |
179 |
for project, pending_app in chains: |
180 |
status = project.state_display() |
181 |
pending_appid = pending_app.id if pending_app is not None else "" |
182 |
application = project.application |
183 |
|
184 |
t = (project.pk, |
185 |
application.name, |
186 |
status, |
187 |
pending_appid, |
188 |
) |
189 |
l.append(t) |
190 |
return l, labels
|