root / snf-astakos-app / astakos / im / management / commands / project-list.py @ 9d5cf81c
History | View | Annotate | Download (4.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 optparse import make_option |
35 |
|
36 |
from django.core.management.base import NoArgsCommand |
37 |
|
38 |
from astakos.im.models import Chain |
39 |
from ._common import format, shortened |
40 |
|
41 |
|
42 |
class Command(NoArgsCommand): |
43 |
help = "List projects and project status"
|
44 |
|
45 |
option_list = NoArgsCommand.option_list + ( |
46 |
make_option('-c',
|
47 |
action='store_true',
|
48 |
dest='csv',
|
49 |
default=False,
|
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"),
|
56 |
make_option('--full',
|
57 |
action='store_true',
|
58 |
dest='full',
|
59 |
default=False,
|
60 |
help="Do not shorten long names"),
|
61 |
) |
62 |
|
63 |
def handle_noargs(self, **options): |
64 |
labels = ('ProjID', 'Name', 'Applicant', 'Email', 'Status', 'AppID') |
65 |
columns = (7, 23, 20, 20, 17, 7) |
66 |
|
67 |
if not options['csv']: |
68 |
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns)) |
69 |
self.stdout.write(line + '\n') |
70 |
sep = '-' * len(line) |
71 |
self.stdout.write(sep + '\n') |
72 |
|
73 |
chain_dict = Chain.objects.all_full_state() |
74 |
if options['skip']: |
75 |
chain_dict = do_skip(chain_dict) |
76 |
|
77 |
allow_shorten = not options['full'] |
78 |
|
79 |
for info in chain_info(chain_dict): |
80 |
|
81 |
fields = [ |
82 |
(info['projectid'], False), |
83 |
(info['name'], True), |
84 |
(info['applicant'], True), |
85 |
(info['email'], True), |
86 |
(info['status'], False), |
87 |
(info['appid'], False), |
88 |
] |
89 |
|
90 |
fields = [(format(elem), flag) for (elem, flag) in fields] |
91 |
|
92 |
if options['csv']: |
93 |
line = '|'.join(fields)
|
94 |
else:
|
95 |
output = [] |
96 |
for (field, shorten), width in zip(fields, columns): |
97 |
s = (shortened(field, width) if shorten and allow_shorten |
98 |
else field)
|
99 |
s = s.rjust(width) |
100 |
output.append(s) |
101 |
|
102 |
line = ' '.join(output)
|
103 |
|
104 |
self.stdout.write(line + '\n') |
105 |
|
106 |
def do_skip(chain_dict): |
107 |
d = {} |
108 |
for chain, (state, project, app) in chain_dict.iteritems(): |
109 |
if state not in Chain.SKIP_STATES: |
110 |
d[chain] = (state, project, app) |
111 |
return d
|
112 |
|
113 |
def chain_info(chain_dict): |
114 |
l = [] |
115 |
for chain, (state, project, app) in chain_dict.iteritems(): |
116 |
status = Chain.state_display(state) |
117 |
if state in Chain.PENDING_STATES: |
118 |
appid = str(app.id)
|
119 |
else:
|
120 |
appid = ""
|
121 |
|
122 |
d = { |
123 |
'projectid' : str(chain), |
124 |
'name' : str(project.application.name if project else app.name), |
125 |
'applicant' : app.applicant.realname,
|
126 |
'email' : app.applicant.email,
|
127 |
'status': status,
|
128 |
'appid' : appid,
|
129 |
} |
130 |
l.append(d) |
131 |
return l
|