Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / project-show.py @ 09f54ceb

History | View | Annotate | Download (7.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
from django.core.management.base import BaseCommand, CommandError
36

    
37
from synnefo.lib.ordereddict import OrderedDict
38
from astakos.im.models import Chain, ProjectApplication
39

    
40
from ._common import format_bool, format_date
41

    
42

    
43
class Command(BaseCommand):
44
    args = "<id or name>"
45
    help = "Show project details"
46

    
47
    option_list = BaseCommand.option_list + (
48
        make_option('--app',
49
                    action='store_true',
50
                    dest='app',
51
                    default=False,
52
                    help="Show application details instead"),
53
        make_option('--pending',
54
                    action='store_true',
55
                    dest='pending',
56
                    default=False,
57
                    help="Show pending modification too"),
58
    )
59

    
60
    def handle(self, *args, **options):
61
        if len(args) != 1:
62
            raise CommandError("Please provide project ID or name")
63

    
64
        show_pending = bool(options['pending'])
65
        search_apps  = options['app']
66

    
67
        name_or_id = args[0]
68
        is_id = name_or_id.isdigit()
69
        if is_id:
70
            name_or_id = int(name_or_id)
71

    
72
        if search_apps:
73
            infolist = app_info(name_or_id, is_id)
74
        else:
75
            chains = get_chains(name_or_id, is_id)
76
            infolist = collect_info(chains, show_pending)
77

    
78
        if not infolist:
79
            kind = 'project application' if search_apps else 'project'
80
            field = 'id' if is_id else 'name'
81
            msg = "Unknown %s with %s '%s'" % (kind, field, name_or_id)
82
            raise CommandError(msg)
83

    
84
        for info in infolist:
85
            self.show_info(info)
86

    
87
    def show_info(self, info):
88
        for key, val in info.items():
89
            line = '%s: %s\n' % (key.rjust(22), val)
90
            self.stdout.write(line)
91
        self.stdout.write('\n')
92

    
93

    
94
def app_info(name_or_id, is_id):
95
    try:
96
        apps = ([ProjectApplication.objects.get(id=name_or_id)]
97
                if is_id
98
                else ProjectApplication.objects.search_by_name(name_or_id))
99
        return [app_fields(app) for app in apps]
100
    except ProjectApplication.DoesNotExist:
101
            return []
102

    
103
def get_chains(name_or_id, is_id):
104
    if is_id:
105
        try:
106
            return [Chain.objects.get(chain=name_or_id)]
107
        except Chain.DoesNotExist:
108
            return []
109
    else:
110
        return Chain.objects.search_by_name(name_or_id)
111

    
112
def collect_info(chains, pending):
113
    states = [chain.full_state() for chain in chains]
114

    
115
    infolist = []
116
    for state in states:
117
        infolist += (chain_fields(state, pending))
118
    return infolist
119

    
120
def chain_fields((s, project, app), request=False):
121
    l = []
122
    if project:
123
        l = [project_fields(s, project, app)]
124
        if request and s in Chain.PENDING_STATES:
125
            l.append(app_fields(app))
126
    else:
127
        l = [app_fields(app)]
128
    return l
129

    
130
def app_fields(app):
131
    mem_limit = app.limit_on_members_number
132
    mem_limit_show = mem_limit if mem_limit is not None else "unlimited"
133

    
134
    d = OrderedDict([
135
            ('project id', app.chain),
136
            ('application id', app.id),
137
            ('name', app.name),
138
            ('status', app.state_display()),
139
            ('owner', app.owner),
140
            ('homepage', app.homepage),
141
            ('description', app.description),
142
            ('comments for review', app.comments),
143
            ('request issue date', format_date(app.issue_date)),
144
            ('request start date', format_date(app.start_date)),
145
            ('request end date', format_date(app.end_date)),
146
            ('resources', app.resource_policies),
147
            ('join policy', app.member_join_policy_display),
148
            ('leave policy', app.member_leave_policy_display),
149
            ('max members', mem_limit_show),
150
            ])
151

    
152
    return d
153

    
154

    
155
def project_fields(s, project, last_app):
156
    app = project.application
157

    
158
    d = OrderedDict([
159
            ('project id', project.id),
160
            ('application id', app.id),
161
            ('name', project.name),
162
            ('status', Chain.state_display(s)),
163
            ])
164
    if s in Chain.PENDING_STATES:
165
        d.update([('pending application', last_app.id)])
166

    
167
    d.update([('owner', app.owner),
168
              ('homepage', app.homepage),
169
              ('description', app.description),
170
              ('comments for review', app.comments),
171
              ('request issue date', format_date(app.issue_date)),
172
              ('request start date', format_date(app.start_date)),
173
              ('creation date', format_date(project.creation_date)),
174
              ('request end date', format_date(app.end_date)),
175
              ])
176

    
177
    deact_date = project.deactivation_date
178
    if deact_date is not None:
179
        d['deactivation date'] = format_date(deact_date)
180

    
181
    mem_limit = app.limit_on_members_number
182
    mem_limit_show = mem_limit if mem_limit is not None else "unlimited"
183

    
184
    d.update([
185
            ('resources', app.resource_policies),
186
            ('join policy', app.member_join_policy_display),
187
            ('leave policy', app.member_leave_policy_display),
188
            ('max members', mem_limit_show),
189
            ('total members', project.members_count()),
190
            ])
191

    
192
    memberships = project.projectmembership_set
193
    accepted  = [str(m.person) for m in memberships.any_accepted()]
194
    requested = [str(m.person) for m in memberships.requested()]
195
    suspended = [str(m.person) for m in memberships.suspended()]
196

    
197
    if accepted:
198
        d['accepted members'] = ', '.join(accepted)
199

    
200
    if suspended:
201
        d['suspended members'] = ', '.join(suspended)
202

    
203
    if requested:
204
        d['membership requests'] = ', '.join(requested)
205

    
206
    return d