Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / project-list.py @ cb261eef

History | View | Annotate | Download (4.1 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"
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
    )
57

    
58
    def handle_noargs(self, **options):
59
        labels = ('ProjID', 'Name', 'Owner', 'Status', 'AppID')
60
        columns = (7, 23, 23, 20, 7)
61

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

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

    
72
        for info in chain_info(chain_dict):
73

    
74
            fields = [(format(elem) for elem in (
75
                (info['projectid'], False),
76
                (info['name'], True),
77
                (info['owner'], True),
78
                (info['status'], False),
79
                (info['appid'], False),
80
            )]
81

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

    
91
                line = ' '.join(output)
92

    
93
            self.stdout.write(line + '\n')
94

    
95
def do_skip(chain_dict):
96
    d = {}
97
    for chain, (state, project, app) in chain_dict.iteritems():
98
        if state not in Chain.SKIP_STATES:
99
            d[chain] = (state, project, app)
100
    return d
101

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

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