project-show.py 6.21 KB
Newer Older
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
1
# Copyright 2012-2013 GRNET S.A. All rights reserved.
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
#
# 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.

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
34
from optparse import make_option
35
36
from django.core.management.base import BaseCommand, CommandError

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
37
from synnefo.lib.ordereddict import OrderedDict
38
from astakos.im.models import Chain
39
40
41
42
43

from ._common import format_bool, format_date


class Command(BaseCommand):
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
44
45
46
47
    args = "<id or name>"
    help = "Show project details"

    option_list = BaseCommand.option_list + (
48
        make_option('--pending',
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
49
                    action='store_true',
50
                    dest='pending',
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
51
                    default=False,
52
                    help="Show pending modification too"),
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
53
    )
54
55
56

    def handle(self, *args, **options):
        if len(args) != 1:
57
58
59
            raise CommandError("Please provide project ID or name")

        show_pending = bool(options['pending'])
60
61

        name_or_id = args[0]
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
62
63
64
65
        is_id = name_or_id.isdigit()
        if is_id:
            name_or_id = int(name_or_id)

66
67
        chains = get_chains(name_or_id, is_id)
        infolist = collect_info(chains, show_pending)
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
68

69
70
71
72
73
        # if not infolist:
        #     kind = 'project application' if search_application else 'project'
        #     field = 'id' if is_id else 'name'
        #     msg = "Unknown %s with %s '%s'" % (kind, field, name_or_id)
        #     raise CommandError(msg)
74

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
75
76
77
78
79
80
        for info in infolist:
            self.show_info(info)

    def show_info(self, info):
        for key, val in info.items():
            line = '%s: %s\n' % (key.rjust(22), val)
81
            self.stdout.write(line)
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
82
83
84
        self.stdout.write('\n')


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
def get_chains(name_or_id, is_id):
    if is_id:
        try:
            return [Chain.objects.get(chain=name_or_id)]
        except Chain.DoesNotExist:
            return []
    else:
        return Chain.objects.search_by_name(name_or_id)

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

    infolist = []
    for state in states:
        infolist += (chain_fields(state, pending))
    return infolist

def chain_fields((s, project, app), request=False):
    l = []
    if project:
        l = [project_fields(s, project, app)]
        if request and s in Chain.PENDING_STATES:
            l.append(app_fields(app))
    else:
        l = [app_fields(app)]
    return l

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
112
113
114
def app_fields(app):
    d = OrderedDict([
            ('project id', app.chain),
115
            ('application id', app.id),
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
116
117
            ('name', app.name),
            ('status', app.state_display()),
118
            ('owner', app.owner),
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
119
120
            ('homepage', app.homepage),
            ('description', app.description),
121
122
123
124
            ('comments for review', app.comments),
            ('request issue date', format_date(app.issue_date)),
            ('request start date', format_date(app.start_date)),
            ('request end date', format_date(app.end_date)),
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
125
126
127
128
129
130
131
132
133
            ('resources', app.resource_policies),
            ('join policy', app.member_join_policy_display),
            ('leave policy', app.member_leave_policy_display),
            ('max members', app.limit_on_members_number),
            ])

    return d


134
def project_fields(s, project, last_app):
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
135
    app = project.application
136

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
137
138
139
140
    d = OrderedDict([
            ('project id', project.id),
            ('application id', app.id),
            ('name', project.name),
141
            ('status', Chain.state_display(s)),
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
142
            ])
143
144
145
146
147
148
149
150
151
152
153
154
155
    if s in Chain.PENDING_STATES:
        d.update([('pending application', last_app.id)])

    d.update([('owner', app.owner),
              ('homepage', app.homepage),
              ('description', app.description),
              ('comments for review', app.comments),
              ('request issue date', format_date(app.issue_date)),
              ('request start date', format_date(app.start_date)),
              ('creation date', format_date(project.creation_date)),
              ('request end date', format_date(app.end_date)),
              ])

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    deact_date = project.deactivation_date
    if deact_date is not None:
        d['deactivation date'] = format_date(deact_date)

    d.update([
            ('resources', app.resource_policies),
            ('join policy', app.member_join_policy_display),
            ('leave policy', app.member_leave_policy_display),
            ('max members', app.limit_on_members_number),
            ('total members', project.members_count()),
            ])

    memberships = project.projectmembership_set
    accepted  = [str(m.person) for m in memberships.any_accepted()]
    requested = [str(m.person) for m in memberships.requested()]
    suspended = [str(m.person) for m in memberships.suspended()]

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

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

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

    return d