Revision 67cf14bf snf-astakos-app/astakos/im/management/commands/project-list.py
b/snf-astakos-app/astakos/im/management/commands/project-list.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
from optparse import make_option |
35 | 35 |
|
36 |
from astakos.im.models import Chain, Project |
|
37 |
from synnefo.webproject.management.commands import ListCommand |
|
36 |
from synnefo.webproject.management.commands import SynnefoCommand, CommandError |
|
38 | 37 |
|
38 |
from astakos.im.models import Chain |
|
39 |
from synnefo.webproject.management import utils |
|
40 |
from ._common import is_uuid, is_email |
|
39 | 41 |
|
40 |
def get_name(chain): |
|
41 |
try: |
|
42 |
p = Project.objects.get(pk=chain.pk) |
|
43 |
except Project.DoesNotExist: |
|
44 |
app = chain.last_application() |
|
45 |
return app.name |
|
46 |
else: |
|
47 |
return p.name |
|
48 | 42 |
|
49 |
|
|
50 |
def get_owner_name(chain): |
|
51 |
return chain.last_application().owner.realname |
|
52 |
|
|
53 |
|
|
54 |
def get_owner_email(chain): |
|
55 |
return chain.last_application().owner.email |
|
56 |
|
|
57 |
|
|
58 |
def get_state(chain): |
|
59 |
try: |
|
60 |
p = Project.objects.get(pk=chain.pk) |
|
61 |
except Project.DoesNotExist: |
|
62 |
p = None |
|
63 |
app = chain.last_application() |
|
64 |
return chain.get_state(p, app)[0] |
|
65 |
|
|
66 |
|
|
67 |
def get_state_display(chain): |
|
68 |
return Chain.state_display(get_state(chain)) |
|
69 |
|
|
70 |
|
|
71 |
def get_appid(chain): |
|
72 |
try: |
|
73 |
p = Project.objects.get(pk=chain.pk) |
|
74 |
except Project.DoesNotExist: |
|
75 |
p = None |
|
76 |
app = chain.last_application() |
|
77 |
state = chain.get_state(p, app)[0] |
|
78 |
if state in Chain.PENDING_STATES: |
|
79 |
return str(app.id) |
|
80 |
else: |
|
81 |
return "" |
|
82 |
|
|
83 |
|
|
84 |
class Command(ListCommand): |
|
43 |
class Command(SynnefoCommand): |
|
85 | 44 |
help = """ |
86 | 45 |
List projects and project status. |
87 | 46 |
|
... | ... | |
111 | 70 |
by a new project |
112 | 71 |
""" |
113 | 72 |
|
114 |
object_class = Chain |
|
115 |
|
|
116 |
FIELDS = { |
|
117 |
'ProjID': ('pk', 'The id of the project'), |
|
118 |
'Name': (get_name, 'The name of the project'), |
|
119 |
'Owner': (get_owner_name, 'The name of the project owner'), |
|
120 |
'Email': (get_owner_email, 'The email of the project owner'), |
|
121 |
'Status': (get_state_display, 'The status of the project'), |
|
122 |
'AppID': (get_appid, 'The project application identification'), |
|
123 |
} |
|
124 |
|
|
125 |
fields = ['ProjID', 'Name', 'Owner', 'Email', 'Status', 'AppID'] |
|
126 |
|
|
127 |
option_list = ListCommand.option_list + ( |
|
73 |
option_list = SynnefoCommand.option_list + ( |
|
128 | 74 |
make_option('--all', |
129 | 75 |
action='store_true', |
130 | 76 |
dest='all', |
... | ... | |
151 | 97 |
dest='skip', |
152 | 98 |
default=False, |
153 | 99 |
help="Skip cancelled and terminated projects"), |
154 |
make_option('--full', |
|
155 |
action='store_true', |
|
156 |
dest='full', |
|
157 |
default=False, |
|
158 |
help="Do not shorten long names"), |
|
100 |
make_option('--name', |
|
101 |
dest='name', |
|
102 |
help='Filter projects by name'), |
|
103 |
make_option('--owner', |
|
104 |
dest='owner', |
|
105 |
help='Filter projects by owner\'s email or uuid'), |
|
159 | 106 |
) |
160 | 107 |
|
161 |
def handle_db_objects(self, objects, **options): |
|
162 |
if options['all']: |
|
163 |
return |
|
164 |
|
|
165 |
f_states = [] |
|
166 |
if options['new']: |
|
167 |
f_states.append(Chain.PENDING) |
|
168 |
if options['modified']: |
|
169 |
f_states += Chain.MODIFICATION_STATES |
|
170 |
if options['pending']: |
|
171 |
f_states.append(Chain.PENDING) |
|
172 |
f_states += Chain.MODIFICATION_STATES |
|
173 |
if options['skip']: |
|
174 |
if not f_states: |
|
108 |
def handle(self, *args, **options): |
|
109 |
|
|
110 |
chain_dict = Chain.objects.all_full_state() |
|
111 |
|
|
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: |
|
175 | 123 |
f_states = Chain.RELEVANT_STATES |
176 | 124 |
|
177 |
if f_states: |
|
178 |
map(objects.remove, |
|
179 |
filter(lambda o: get_state(o) not in f_states, objects)) |
|
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) |
|
135 |
|
|
136 |
labels = ('ProjID', 'Name', 'Owner', 'Email', 'Status', 'AppID') |
|
137 |
|
|
138 |
info = chain_info(chain_dict) |
|
139 |
utils.pprint_table(self.stdout, info, labels, |
|
140 |
options["output_format"]) |
|
141 |
|
|
142 |
|
|
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 |
|
154 |
|
|
155 |
|
|
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 |
|
165 |
|
|
166 |
|
|
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 |
|
173 |
|
|
174 |
|
|
175 |
def chain_info(chain_dict): |
|
176 |
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, |
|
188 |
status, |
|
189 |
appid, |
|
190 |
) |
|
191 |
l.append(t) |
|
192 |
return l |
Also available in: Unified diff