Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.9 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 = """
44
    List projects and project status.
45

46
    Project status can be one of:
47
      Pending              an application <AppId> for a new project
48

49
      Active               an active project
50

51
      Active - Pending     an active project with
52
                           a pending modification <AppId>
53

54
      Denied               an application for a new project,
55
                           denied by the admin
56

57
      Dismissed            a denied project, dismissed by the applicant
58

59
      Cancelled            an application for a new project,
60
                           cancelled by the applicant
61

62
      Suspended            a project suspended by the admin;
63
                           it can later be resumed
64

65
      Suspended - Pending  a suspended project with
66
                           a pending modification <AppId>
67

68
      Terminated           a terminated project; its name can be claimed
69
                           by a new project
70
"""
71

    
72
    option_list = NoArgsCommand.option_list + (
73
        make_option('--all',
74
                    action='store_true',
75
                    dest='all',
76
                    default=False,
77
                    help="List all projects (default)"),
78
        make_option('--new',
79
                    action='store_true',
80
                    dest='new',
81
                    default=False,
82
                    help="List only new project applications"),
83
        make_option('--modified',
84
                    action='store_true',
85
                    dest='modified',
86
                    default=False,
87
                    help="List only projects with pending modification"),
88
        make_option('--pending',
89
                    action='store_true',
90
                    dest='pending',
91
                    default=False,
92
                    help=("Show only projects with a pending application "
93
                          "(equiv. --modified --new)")),
94
        make_option('--skip',
95
                    action='store_true',
96
                    dest='skip',
97
                    default=False,
98
                    help="Skip cancelled and terminated projects"),
99
        make_option('--full',
100
                    action='store_true',
101
                    dest='full',
102
                    default=False,
103
                    help="Do not shorten long names"),
104
        make_option('-c',
105
                    action='store_true',
106
                    dest='csv',
107
                    default=False,
108
                    help="Use pipes to separate values"),
109
    )
110

    
111
    def handle_noargs(self, **options):
112
        allow_shorten = not options['full']
113
        csv = options['csv']
114

    
115
        chain_dict = Chain.objects.all_full_state()
116

    
117
        if not options['all']:
118
            f_states = []
119
            if options['new']:
120
                f_states.append(Chain.PENDING)
121
            if options['modified']:
122
                f_states += Chain.MODIFICATION_STATES
123
            if options['pending']:
124
                f_states.append(Chain.PENDING)
125
                f_states += Chain.MODIFICATION_STATES
126
            if options['skip']:
127
                if not f_states:
128
                    f_states = Chain.RELEVANT_STATES
129

    
130
            if f_states:
131
                chain_dict = filter_by_state(chain_dict, f_states)
132

    
133
        self.show(csv, allow_shorten, chain_dict)
134

    
135
    def show(self, csv, allow_shorten, chain_dict):
136
        labels = ('ProjID', 'Name', 'Owner', 'Email', 'Status', 'AppID')
137
        columns = (7, 23, 20, 20, 17, 7)
138

    
139
        if not csv:
140
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
141
            self.stdout.write(line + '\n')
142
            sep = '-' * len(line)
143
            self.stdout.write(sep + '\n')
144

    
145
        for info in chain_info(chain_dict):
146

    
147
            fields = [
148
                (info['projectid'], False),
149
                (info['name'], True),
150
                (info['owner'], True),
151
                (info['email'], True),
152
                (info['status'], False),
153
                (info['appid'], False),
154
            ]
155

    
156
            fields = [(format(elem), flag) for (elem, flag) in fields]
157

    
158
            if csv:
159
                line = '|'.join(fields)
160
            else:
161
                output = []
162
                for (field, shorten), width in zip(fields, columns):
163
                    s = (shortened(field, width) if shorten and allow_shorten
164
                         else field)
165
                    s = s.rjust(width)
166
                    output.append(s)
167

    
168
                line = ' '.join(output)
169

    
170
            self.stdout.write(line + '\n')
171

    
172

    
173
def filter_by_state(chain_dict, states):
174
    d = {}
175
    for chain, (state, project, app) in chain_dict.iteritems():
176
        if state in states:
177
            d[chain] = (state, project, app)
178
    return d
179

    
180

    
181
def chain_info(chain_dict):
182
    l = []
183
    for chain, (state, project, app) in chain_dict.iteritems():
184
        status = Chain.state_display(state)
185
        if state in Chain.PENDING_STATES:
186
            appid = str(app.id)
187
        else:
188
            appid = ""
189

    
190
        d = {
191
            'projectid': str(chain),
192
            'name': project.application.name if project else app.name,
193
            'owner': app.owner.realname,
194
            'email': app.owner.email,
195
            'status': status,
196
            'appid': appid,
197
        }
198
        l.append(d)
199
    return l