Revision 398a9604
b/snf-astakos-app/astakos/im/management/commands/project-show.py | ||
---|---|---|
41 | 41 |
|
42 | 42 |
|
43 | 43 |
class Command(SynnefoCommand): |
44 |
args = "<id or name>" |
|
45 |
help = """ |
|
46 |
Show project details. |
|
47 |
|
|
48 |
Command comes in two forms: |
|
49 |
project-show <id> Look up project by id |
|
50 |
|
|
51 |
project-show <name> Look up all projects whose name |
|
52 |
contains the given string |
|
53 |
""" |
|
44 |
args = "<id>" |
|
45 |
help = "Show details for project (or application) <id>" |
|
54 | 46 |
|
55 | 47 |
option_list = SynnefoCommand.option_list + ( |
56 | 48 |
make_option('--app', |
... | ... | |
63 | 55 |
action='store_true', |
64 | 56 |
dest='pending', |
65 | 57 |
default=False, |
66 |
help=("For a given project, show also pending modifications" |
|
67 |
"(applications), if any") |
|
58 |
help=("For a given project, show also pending " |
|
59 |
"modifications (applications), if any") |
|
60 |
), |
|
61 |
make_option('--members', |
|
62 |
action='store_true', |
|
63 |
dest='members', |
|
64 |
default=False, |
|
65 |
help=("Show a list of project memberships") |
|
68 | 66 |
), |
69 | 67 |
) |
70 | 68 |
|
... | ... | |
73 | 71 |
raise CommandError("Please provide project ID or name") |
74 | 72 |
|
75 | 73 |
show_pending = bool(options['pending']) |
74 |
show_members = bool(options['members']) |
|
76 | 75 |
search_apps = options['app'] |
76 |
self.output_format = options['output_format'] |
|
77 | 77 |
|
78 |
name_or_id = args[0] |
|
79 |
is_id = name_or_id.isdigit() |
|
80 |
if is_id: |
|
81 |
name_or_id = int(name_or_id) |
|
78 |
id_ = args[0] |
|
79 |
try: |
|
80 |
id_ = int(id_) |
|
81 |
except ValueError: |
|
82 |
raise CommandError("id should be an integer value.") |
|
82 | 83 |
|
83 | 84 |
if search_apps: |
84 |
infolist = app_info(name_or_id, is_id)
|
|
85 |
self.pprint_dict(app_info(id_))
|
|
85 | 86 |
else: |
86 |
chains = get_chains(name_or_id, is_id) |
|
87 |
infolist = collect_info(chains, show_pending) |
|
88 |
|
|
89 |
if not infolist: |
|
90 |
kind = 'project application' if search_apps else 'project' |
|
91 |
field = 'id' if is_id else 'name' |
|
92 |
msg = "Unknown %s with %s '%s'" % (kind, field, name_or_id) |
|
93 |
raise CommandError(msg) |
|
94 |
|
|
95 |
first = True |
|
96 |
for info in infolist: |
|
97 |
if not first: |
|
87 |
state, project, app = get_chain_state(id_) |
|
88 |
self.pprint_dict(chain_fields(state, project, app)) |
|
89 |
if show_members and project is not None: |
|
98 | 90 |
self.stdout.write("\n") |
99 |
else:
|
|
100 |
first = False
|
|
101 |
utils.pprint_table(self.stdout, [info.values()], info.keys(),
|
|
102 |
options["output_format"], vertical=True)
|
|
103 |
|
|
91 |
fields, labels = members_fields(project)
|
|
92 |
self.pprint_table(fields, labels)
|
|
93 |
if show_pending and state in Chain.PENDING_STATES:
|
|
94 |
self.stdout.write("\n")
|
|
95 |
self.pprint_dict(app_fields(app)) |
|
104 | 96 |
|
105 |
def app_info(name_or_id, is_id): |
|
106 |
try: |
|
107 |
apps = ([ProjectApplication.objects.get(id=name_or_id)] |
|
108 |
if is_id |
|
109 |
else ProjectApplication.objects.search_by_name(name_or_id)) |
|
110 |
return [app_fields(app) for app in apps] |
|
111 |
except ProjectApplication.DoesNotExist: |
|
112 |
return [] |
|
97 |
def pprint_dict(self, d, vertical=True): |
|
98 |
utils.pprint_table(self.stdout, [d.values()], d.keys(), |
|
99 |
self.output_format, vertical=vertical) |
|
113 | 100 |
|
101 |
def pprint_table(self, tbl, labels): |
|
102 |
utils.pprint_table(self.stdout, tbl, labels, |
|
103 |
self.output_format) |
|
114 | 104 |
|
115 |
def get_chains(name_or_id, is_id): |
|
116 |
if is_id: |
|
117 |
try: |
|
118 |
return [Chain.objects.get(chain=name_or_id)] |
|
119 |
except Chain.DoesNotExist: |
|
120 |
return [] |
|
121 |
else: |
|
122 |
return Chain.objects.search_by_name(name_or_id) |
|
123 | 105 |
|
106 |
def app_info(app_id): |
|
107 |
try: |
|
108 |
app = ProjectApplication.objects.get(id=app_id) |
|
109 |
return app_fields(app) |
|
110 |
except ProjectApplication.DoesNotExist: |
|
111 |
raise CommandError("Application with id %s not found." % app_id) |
|
124 | 112 |
|
125 |
def collect_info(chains, pending): |
|
126 |
states = [chain.full_state() for chain in chains] |
|
127 | 113 |
|
128 |
infolist = [] |
|
129 |
for state in states: |
|
130 |
infolist += (chain_fields(state, pending)) |
|
131 |
return infolist |
|
114 |
def get_chain_state(project_id): |
|
115 |
try: |
|
116 |
chain = Chain.objects.get(chain=project_id) |
|
117 |
return chain.full_state() |
|
118 |
except Chain.DoesNotExist: |
|
119 |
raise CommandError("Project with id %s not found." % project_id) |
|
132 | 120 |
|
133 | 121 |
|
134 |
def chain_fields((s, project, app), request=False): |
|
135 |
l = [] |
|
136 |
if project: |
|
137 |
l = [project_fields(s, project, app)] |
|
138 |
if request and s in Chain.PENDING_STATES: |
|
139 |
l.append(app_fields(app)) |
|
122 |
def chain_fields(state, project, app): |
|
123 |
if project is not None: |
|
124 |
return project_fields(state, project, app) |
|
140 | 125 |
else: |
141 |
l = [app_fields(app)] |
|
142 |
return l |
|
126 |
return app_fields(app) |
|
143 | 127 |
|
144 | 128 |
|
145 | 129 |
def app_fields(app): |
... | ... | |
206 | 190 |
('total members', project.members_count()), |
207 | 191 |
]) |
208 | 192 |
|
209 |
memberships = project.projectmembership_set |
|
210 |
accepted = [str(m.person) for m in memberships.any_accepted()] |
|
211 |
requested = [str(m.person) for m in memberships.requested()] |
|
212 |
suspended = [str(m.person) for m in memberships.suspended()] |
|
213 |
|
|
214 |
if accepted: |
|
215 |
d['accepted members'] = ', '.join(accepted) |
|
193 |
return d |
|
216 | 194 |
|
217 |
if suspended: |
|
218 |
d['suspended members'] = ', '.join(suspended) |
|
219 | 195 |
|
220 |
if requested: |
|
221 |
d['membership requests'] = ', '.join(requested) |
|
196 |
def members_fields(project): |
|
197 |
labels = ('member uuid', 'email', 'status') |
|
198 |
objs = project.projectmembership_set.select_related('person') |
|
199 |
memberships = objs.all().order_by('state', 'person__email') |
|
200 |
collect = [] |
|
201 |
for m in memberships: |
|
202 |
user = m.person |
|
203 |
collect.append((user.uuid, |
|
204 |
user.email, |
|
205 |
m.state_display())) |
|
222 | 206 |
|
223 |
return d |
|
207 |
return collect, labels |
b/snf-astakos-app/astakos/im/management/commands/user-show.py | ||
---|---|---|
34 | 34 |
from django.core.management.base import CommandError |
35 | 35 |
from optparse import make_option |
36 | 36 |
|
37 |
from astakos.im.models import AstakosUser, get_latest_terms |
|
37 |
from astakos.im.models import AstakosUser, get_latest_terms, Chain
|
|
38 | 38 |
from astakos.im.quotas import list_user_quotas |
39 | 39 |
|
40 | 40 |
from synnefo.lib.ordereddict import OrderedDict |
... | ... | |
56 | 56 |
dest='list_quotas', |
57 | 57 |
default=False, |
58 | 58 |
help="Also list user quota"), |
59 |
make_option('--projects', |
|
60 |
action='store_true', |
|
61 |
dest='list_projects', |
|
62 |
default=False, |
|
63 |
help="Also list project memberships"), |
|
59 | 64 |
) |
60 | 65 |
|
61 | 66 |
def handle(self, *args, **options): |
... | ... | |
122 | 127 |
print_data, labels = show_quotas(quotas, initial) |
123 | 128 |
utils.pprint_table(self.stdout, print_data, labels, |
124 | 129 |
options["output_format"]) |
130 |
|
|
131 |
if options["list_projects"]: |
|
132 |
print_data, labels = ownerships(user) |
|
133 |
if print_data: |
|
134 |
self.stdout.write("\n") |
|
135 |
utils.pprint_table(self.stdout, print_data, labels, |
|
136 |
options["output_format"], |
|
137 |
title="Owned Projects") |
|
138 |
|
|
139 |
print_data, labels = memberships(user) |
|
140 |
if print_data: |
|
141 |
self.stdout.write("\n") |
|
142 |
utils.pprint_table(self.stdout, print_data, labels, |
|
143 |
options["output_format"], |
|
144 |
title="Project Memberships") |
|
145 |
|
|
146 |
|
|
147 |
def memberships(user): |
|
148 |
ms = user.projectmembership_set.all() |
|
149 |
print_data = [] |
|
150 |
labels = ('project id', 'project name', 'status') |
|
151 |
|
|
152 |
for m in ms: |
|
153 |
project = m.project |
|
154 |
print_data.append((project.id, |
|
155 |
project.application.name, |
|
156 |
m.state_display(), |
|
157 |
)) |
|
158 |
return print_data, labels |
|
159 |
|
|
160 |
|
|
161 |
def ownerships(user): |
|
162 |
chain_dict = Chain.objects.all_full_state() |
|
163 |
chain_dict = filter_by(is_owner(user), chain_dict) |
|
164 |
return chain_info(chain_dict) |
|
165 |
|
|
166 |
|
|
167 |
def is_owner(user): |
|
168 |
def f(state, project, app): |
|
169 |
return user == app.owner |
|
170 |
return f |
|
171 |
|
|
172 |
|
|
173 |
def filter_by(f, chain_dict): |
|
174 |
d = {} |
|
175 |
for chain, tpl in chain_dict.iteritems(): |
|
176 |
if f(*tpl): |
|
177 |
d[chain] = tpl |
|
178 |
return d |
|
179 |
|
|
180 |
|
|
181 |
def chain_info(chain_dict): |
|
182 |
labels = ('project id', 'project name', 'status', 'pending app id') |
|
183 |
l = [] |
|
184 |
for chain, (state, project, app) in chain_dict.iteritems(): |
|
185 |
status = Chain.state_display(state) |
|
186 |
if state in Chain.PENDING_STATES: |
|
187 |
appid = str(app.id) |
|
188 |
else: |
|
189 |
appid = "" |
|
190 |
|
|
191 |
t = (chain, |
|
192 |
project.application.name if project else app.name, |
|
193 |
status, |
|
194 |
appid, |
|
195 |
) |
|
196 |
l.append(t) |
|
197 |
return l, labels |
Also available in: Unified diff