project-list.py 4.09 KB
Newer Older
1
# Copyright 2012-2013 GRNET S.A. All rights reserved.
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from optparse import make_option

Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
36
37
from django.core.management.base import NoArgsCommand

38
from astakos.im.models import Chain
39
from ._common import format, shortened
40

Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
41
42

class Command(NoArgsCommand):
43
    help = "List projects"
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
44
45
46
47
48
49
50

    option_list = NoArgsCommand.option_list + (
        make_option('-c',
                    action='store_true',
                    dest='csv',
                    default=False,
                    help="Use pipes to separate values"),
51
52
53
54
55
        make_option('--skip',
                    action='store_true',
                    dest='skip',
                    default=False,
                    help="Skip cancelled and terminated projects"),
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
56
57
58
    )

    def handle_noargs(self, **options):
59
60
        labels = ('ProjID', 'Name', 'Owner', 'Status', 'AppID')
        columns = (7, 23, 23, 20, 7)
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
61
62
63
64
65
66
67

        if not options['csv']:
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
            self.stdout.write(line + '\n')
            sep = '-' * len(line)
            self.stdout.write(sep + '\n')

68
69
70
        chain_dict = Chain.objects.all_full_state()
        if options['skip']:
            chain_dict = do_skip(chain_dict)
71

72
        for info in chain_info(chain_dict):
73

74
            fields = [(format(elem) for elem in (
75
76
77
78
79
                (info['projectid'], False),
                (info['name'], True),
                (info['owner'], True),
                (info['status'], False),
                (info['appid'], False),
80
            )]
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
81
82
83
84

            if options['csv']:
                line = '|'.join(fields)
            else:
85
86
87
88
89
90
91
                output = []
                for (field, shorten), width in zip(fields, columns):
                    s = shortened(field, width) if shorten else field
                    s = s.rjust(width)
                    output.append(s)

                line = ' '.join(output)
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
92

93
            self.stdout.write(line + '\n')
94
95
96
97
98
99
100
101
102
103
104
105
106

def do_skip(chain_dict):
    d = {}
    for chain, (state, project, app) in chain_dict.iteritems():
        if state not in Chain.SKIP_STATES:
            d[chain] = (state, project, app)
    return d

def chain_info(chain_dict):
    l = []
    for chain, (state, project, app) in chain_dict.iteritems():
        status = Chain.state_display(state)
        if state in Chain.PENDING_STATES:
107
108
109
            appid = str(app.id)
        else:
            appid = ""
110
111
112

        d = {
            'projectid' : str(chain),
113
            'name'  : str(project.application.name if project else app.name),
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
114
            'owner' : app.owner.realname,
115
            'status': status,
116
            'appid' : appid,
117
118
119
            }
        l.append(d)
    return l