Revision 47fbfc1c snf-astakos-app/astakos/im/management/commands/project-list.py
b/snf-astakos-app/astakos/im/management/commands/project-list.py | ||
---|---|---|
1 |
# Copyright 2012 GRNET S.A. All rights reserved. |
|
1 |
# Copyright 2012-2013 GRNET S.A. All rights reserved.
|
|
2 | 2 |
# |
3 | 3 |
# Redistribution and use in source and binary forms, with or |
4 | 4 |
# without modification, are permitted provided that the following |
... | ... | |
35 | 35 |
|
36 | 36 |
from django.core.management.base import NoArgsCommand |
37 | 37 |
|
38 |
from astakos.im.models import ProjectApplication
|
|
38 |
from astakos.im.models import Chain
|
|
39 | 39 |
from ._common import format_bool |
40 |
from synnefo.util.text import uenc |
|
40 | 41 |
|
41 | 42 |
class Command(NoArgsCommand): |
42 |
help = "List resources"
|
|
43 |
help = "List projects"
|
|
43 | 44 |
|
44 | 45 |
option_list = NoArgsCommand.option_list + ( |
45 | 46 |
make_option('-c', |
... | ... | |
47 | 48 |
dest='csv', |
48 | 49 |
default=False, |
49 | 50 |
help="Use pipes to separate values"), |
51 |
make_option('--skip', |
|
52 |
action='store_true', |
|
53 |
dest='skip', |
|
54 |
default=False, |
|
55 |
help="Skip cancelled and terminated projects"), |
|
50 | 56 |
) |
51 | 57 |
|
52 | 58 |
def handle_noargs(self, **options): |
53 |
apps = ProjectApplication.objects.select_related('project').all().order_by('id') |
|
54 |
|
|
55 |
labels = ( |
|
56 |
'Application', 'Precursor', 'Status', 'Name', 'Project', 'Status' |
|
57 |
) |
|
58 |
columns = (11, 10, 14, 30, 10, 10) |
|
59 |
labels = ('ProjectID', 'Name', 'Owner', 'Status') |
|
60 |
columns = (9, 24, 24, 23) |
|
59 | 61 |
|
60 | 62 |
if not options['csv']: |
61 | 63 |
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns)) |
... | ... | |
63 | 65 |
sep = '-' * len(line) |
64 | 66 |
self.stdout.write(sep + '\n') |
65 | 67 |
|
66 |
for app in apps:
|
|
67 |
precursor = app.precursor_application
|
|
68 |
prec_id = precursor.id if precursor else ''
|
|
68 |
chain_dict = Chain.objects.all_full_state()
|
|
69 |
if options['skip']:
|
|
70 |
chain_dict = do_skip(chain_dict)
|
|
69 | 71 |
|
70 |
try: |
|
71 |
project = app.project |
|
72 |
project_id = project.id |
|
73 |
status = project.state_display() |
|
74 |
except: |
|
75 |
project_id = '' |
|
76 |
status = '' |
|
72 |
for info in chain_info(chain_dict): |
|
77 | 73 |
|
78 | 74 |
fields = ( |
79 |
str(app.id), |
|
80 |
str(prec_id), |
|
81 |
app.state_display(), |
|
82 |
app.name, |
|
83 |
str(project_id), |
|
84 |
status |
|
75 |
info['projectid'], |
|
76 |
info['name'], |
|
77 |
info['owner'], |
|
78 |
info['status'], |
|
85 | 79 |
) |
86 | 80 |
|
87 | 81 |
if options['csv']: |
... | ... | |
90 | 84 |
line = ' '.join(f.rjust(w) for f, w in zip(fields, columns)) |
91 | 85 |
|
92 | 86 |
self.stdout.write(line + '\n') |
87 |
|
|
88 |
def do_skip(chain_dict): |
|
89 |
d = {} |
|
90 |
for chain, (state, project, app) in chain_dict.iteritems(): |
|
91 |
if state not in Chain.SKIP_STATES: |
|
92 |
d[chain] = (state, project, app) |
|
93 |
return d |
|
94 |
|
|
95 |
def chain_info(chain_dict): |
|
96 |
l = [] |
|
97 |
for chain, (state, project, app) in chain_dict.iteritems(): |
|
98 |
status = Chain.state_display(state) |
|
99 |
if state in Chain.PENDING_STATES: |
|
100 |
status += " %s" % (app.id,) |
|
101 |
|
|
102 |
d = { |
|
103 |
'projectid' : str(chain), |
|
104 |
'name' : str(project.name if project else app.name), |
|
105 |
'owner' : uenc(app.owner.realname), |
|
106 |
'status': status, |
|
107 |
} |
|
108 |
l.append(d) |
|
109 |
return l |
Also available in: Unified diff