Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.3 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 synnefo.webproject.management.commands import SynnefoCommand, CommandError
37

    
38
from astakos.im.models import Chain
39
from synnefo.webproject.management import utils
40
from ._common import is_uuid, is_email
41

    
42

    
43
class Command(SynnefoCommand):
44
    help = """
45
    List projects and project status.
46

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

50
      Active               an active project
51

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

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

58
      Dismissed            a denied project, dismissed by the applicant
59

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

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

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

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

    
73
    option_list = SynnefoCommand.option_list + (
74
        make_option('--all',
75
                    action='store_true',
76
                    dest='all',
77
                    default=False,
78
                    help="List all projects (default)"),
79
        make_option('--new',
80
                    action='store_true',
81
                    dest='new',
82
                    default=False,
83
                    help="List only new project applications"),
84
        make_option('--modified',
85
                    action='store_true',
86
                    dest='modified',
87
                    default=False,
88
                    help="List only projects with pending modification"),
89
        make_option('--pending',
90
                    action='store_true',
91
                    dest='pending',
92
                    default=False,
93
                    help=("Show only projects with a pending application "
94
                          "(equiv. --modified --new)")),
95
        make_option('--skip',
96
                    action='store_true',
97
                    dest='skip',
98
                    default=False,
99
                    help="Skip cancelled and terminated projects"),
100
        make_option('--name',
101
                    dest='name',
102
                    help='Filter projects by name'),
103
        make_option('--owner',
104
                    dest='owner',
105
                    help='Filter projects by owner\'s email or uuid'),
106
    )
107

    
108
    def handle(self, *args, **options):
109

    
110
        chain_dict = Chain.objects.all_full_state()
111

    
112
        if not options['all']:
113
            f_states = []
114
            if options['new']:
115
                f_states.append(Chain.PENDING)
116
            if options['modified']:
117
                f_states += Chain.MODIFICATION_STATES
118
            if options['pending']:
119
                f_states.append(Chain.PENDING)
120
                f_states += Chain.MODIFICATION_STATES
121
            if options['skip']:
122
                if not f_states:
123
                    f_states = Chain.RELEVANT_STATES
124

    
125
            if f_states:
126
                chain_dict = filter_by(in_states(f_states), chain_dict)
127

    
128
            name = options['name']
129
            if name:
130
                chain_dict = filter_by(is_name(name), chain_dict)
131

    
132
            owner = options['owner']
133
            if owner:
134
                chain_dict = filter_by(is_owner(owner), chain_dict)
135

    
136
        labels = ('ProjID', 'Name', 'Owner', 'Email', 'Status', 'AppID')
137

    
138
        info = chain_info(chain_dict)
139
        utils.pprint_table(self.stdout, info, labels,
140
                           options["output_format"])
141

    
142

    
143
def is_name(name):
144
    def f(state, project, app):
145
        n = project.application.name if project else app.name
146
        return name == n
147
    return f
148

    
149

    
150
def in_states(states):
151
    def f(state, project, app):
152
        return state in states
153
    return f
154

    
155

    
156
def is_owner(s):
157
    def f(state, project, app):
158
        owner = app.owner
159
        if is_email(s):
160
            return owner.email == s
161
        if is_uuid(s):
162
            return owner.uuid == s
163
        raise CommandError("Expecting either email or uuid.")
164
    return f
165

    
166

    
167
def filter_by(f, chain_dict):
168
    d = {}
169
    for chain, tpl in chain_dict.iteritems():
170
        if f(*tpl):
171
            d[chain] = tpl
172
    return d
173

    
174

    
175
def chain_info(chain_dict):
176
    l = []
177
    for chain, (state, project, app) in chain_dict.iteritems():
178
        status = Chain.state_display(state)
179
        if state in Chain.PENDING_STATES:
180
            appid = str(app.id)
181
        else:
182
            appid = ""
183

    
184
        t = (chain,
185
             project.application.name if project else app.name,
186
             app.owner.realname,
187
             app.owner.email,
188
             status,
189
             appid,
190
             )
191
        l.append(t)
192
    return l