Revision 6d583e07 snf-astakos-app/astakos/im/management/commands/project-list.py
b/snf-astakos-app/astakos/im/management/commands/project-list.py | ||
---|---|---|
35 | 35 |
|
36 | 36 |
from snf_django.management.commands import SynnefoCommand, CommandError |
37 | 37 |
|
38 |
from astakos.im.models import Chain |
|
38 |
from astakos.im.models import Project |
|
39 |
from django.db.models import Q |
|
39 | 40 |
from snf_django.management import utils |
40 | 41 |
from ._common import is_uuid, is_email |
41 | 42 |
|
... | ... | |
107 | 108 |
|
108 | 109 |
def handle(self, *args, **options): |
109 | 110 |
|
110 |
chain_dict = Chain.objects.all_full_state() |
|
111 |
flt = Q() |
|
112 |
owner = options['owner'] |
|
113 |
if owner: |
|
114 |
flt &= filter_by_owner(owner) |
|
111 | 115 |
|
112 |
if not options['all']: |
|
113 |
f_states = [] |
|
114 |
if options['new']: |
|
115 |
f_states.append(Chain.PENDING) |
|
116 |
if options['modified']: |
|
117 |
f_states += Chain.MODIFICATION_STATES |
|
118 |
if options['pending']: |
|
119 |
f_states.append(Chain.PENDING) |
|
120 |
f_states += Chain.MODIFICATION_STATES |
|
121 |
if options['skip']: |
|
122 |
if not f_states: |
|
123 |
f_states = Chain.RELEVANT_STATES |
|
124 |
|
|
125 |
if f_states: |
|
126 |
chain_dict = filter_by(in_states(f_states), chain_dict) |
|
127 |
|
|
128 |
name = options['name'] |
|
129 |
if name: |
|
130 |
chain_dict = filter_by(is_name(name), chain_dict) |
|
131 |
|
|
132 |
owner = options['owner'] |
|
133 |
if owner: |
|
134 |
chain_dict = filter_by(is_owner(owner), chain_dict) |
|
116 |
name = options['name'] |
|
117 |
if name: |
|
118 |
flt &= filter_by_name(name) |
|
135 | 119 |
|
136 |
labels = ('ProjID', 'Name', 'Owner', 'Email', 'Status', 'AppID')
|
|
120 |
chains = Project.objects.all_with_pending(flt)
|
|
137 | 121 |
|
138 |
info = chain_info(chain_dict) |
|
122 |
if not options['all']: |
|
123 |
if options['skip']: |
|
124 |
pred = lambda c: ( |
|
125 |
c[0].overall_state() not in Project.SKIP_STATES |
|
126 |
or c[1] is not None) |
|
127 |
chains = filter_preds([pred], chains) |
|
128 |
|
|
129 |
preds = [] |
|
130 |
if options['new'] or options['pending']: |
|
131 |
preds.append( |
|
132 |
lambda c: c[0].overall_state() == Project.O_PENDING) |
|
133 |
if options['modified'] or options['pending']: |
|
134 |
preds.append( |
|
135 |
lambda c: c[0].overall_state() != Project.O_PENDING |
|
136 |
and c[1] is not None) |
|
137 |
|
|
138 |
if preds: |
|
139 |
chains = filter_preds(preds, chains) |
|
140 |
|
|
141 |
labels = ('ProjID', 'Name', 'Owner', 'Email', 'Status', |
|
142 |
'Pending AppID') |
|
143 |
|
|
144 |
info = chain_info(chains) |
|
139 | 145 |
utils.pprint_table(self.stdout, info, labels, |
140 | 146 |
options["output_format"]) |
141 | 147 |
|
142 | 148 |
|
143 |
def is_name(name): |
|
144 |
def f(state, project, app): |
|
145 |
n = project.application.name if project else app.name |
|
146 |
return name == n |
|
147 |
return f |
|
148 |
|
|
149 |
|
|
150 |
def in_states(states): |
|
151 |
def f(state, project, app): |
|
152 |
return state in states |
|
153 |
return f |
|
149 |
def filter_preds(preds, chains): |
|
150 |
return [c for c in chains |
|
151 |
if any(map(lambda f: f(c), preds))] |
|
154 | 152 |
|
155 | 153 |
|
156 |
def is_owner(s): |
|
157 |
def f(state, project, app): |
|
158 |
owner = app.owner |
|
159 |
if is_email(s): |
|
160 |
return owner.email == s |
|
161 |
if is_uuid(s): |
|
162 |
return owner.uuid == s |
|
163 |
raise CommandError("Expecting either email or uuid.") |
|
164 |
return f |
|
154 |
def filter_by_name(name): |
|
155 |
return Q(application__name=name) |
|
165 | 156 |
|
166 | 157 |
|
167 |
def filter_by(f, chain_dict):
|
|
168 |
d = {}
|
|
169 |
for chain, tpl in chain_dict.iteritems():
|
|
170 |
if f(*tpl):
|
|
171 |
d[chain] = tpl
|
|
172 |
return d
|
|
158 |
def filter_by_owner(s):
|
|
159 |
if is_email(s):
|
|
160 |
return Q(application__owner__email=s)
|
|
161 |
if is_uuid(s):
|
|
162 |
return Q(application__owner__uuid=s)
|
|
163 |
raise CommandError("Expecting either email or uuid.")
|
|
173 | 164 |
|
174 | 165 |
|
175 |
def chain_info(chain_dict):
|
|
166 |
def chain_info(chains):
|
|
176 | 167 |
l = [] |
177 |
for chain, (state, project, app) in chain_dict.iteritems(): |
|
178 |
status = Chain.state_display(state) |
|
179 |
if state in Chain.PENDING_STATES: |
|
180 |
appid = str(app.id) |
|
181 |
else: |
|
182 |
appid = "" |
|
183 |
|
|
184 |
t = (chain, |
|
185 |
project.application.name if project else app.name, |
|
186 |
app.owner.realname, |
|
187 |
app.owner.email, |
|
168 |
for project, pending_app in chains: |
|
169 |
status = project.state_display() |
|
170 |
pending_appid = pending_app.id if pending_app is not None else "" |
|
171 |
application = project.application |
|
172 |
|
|
173 |
t = (project.pk, |
|
174 |
application.name, |
|
175 |
application.owner.realname, |
|
176 |
application.owner.email, |
|
188 | 177 |
status, |
189 |
appid, |
|
178 |
pending_appid,
|
|
190 | 179 |
) |
191 | 180 |
l.append(t) |
192 | 181 |
return l |
Also available in: Unified diff