project-show.py 5.78 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
39
40
41
42
43
from astakos.im.models import ProjectApplication, Project

from ._common import format_bool, format_date


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

    option_list = BaseCommand.option_list + (
        make_option('--app',
                    action='store_true',
                    dest='app',
                    default=False,
                    help="Show application details instead"),
    )
54
55
56

    def handle(self, *args, **options):
        if len(args) != 1:
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
57
            raise CommandError("Please provide ID or name")
58
59

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

64
65
66
        search_application = True if options['app'] else False

        infolist = (app_info(name_or_id, is_id) if search_application
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
67
68
                    else project_info(name_or_id, is_id))

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

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
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
112
113
        self.stdout.write('\n')


def app_fields(app):
    d = OrderedDict([
            ('application id', app.id),
            ('project id', app.chain),
            ('name', app.name),
            ('owner', app.owner),
            ('status', app.state_display()),
            ('homepage', app.homepage),
            ('description', app.description),
            ('issue date', format_date(app.issue_date)),
            ('start date', format_date(app.start_date)),
            ('end date', format_date(app.end_date)),
            ('comments', app.comments),
            ('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


def project_fields(project):
    app = project.application
    d = OrderedDict([
            ('project id', project.id),
            ('application id', app.id),
            ('name', project.name),
            ('owner', app.owner),
114
            ('status', project.admin_state_display()),
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
            ('creation date', format_date(project.creation_date)),
            ])
    deact_date = project.deactivation_date
    if deact_date is not None:
        d['deactivation date'] = format_date(deact_date)

    d.update([
            ('homepage', app.homepage),
            ('description', app.description),
            ('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


def app_info(name_or_id, is_id):
    try:
        apps = ([ProjectApplication.objects.get(id=name_or_id)]
                if is_id
                else ProjectApplication.objects.search_by_name(name_or_id))
        return [app_fields(app) for app in apps]
    except ProjectApplication.DoesNotExist:
            return []


def project_info(name_or_id, is_id):
    try:
        projects = ([Project.objects.get(id=name_or_id)]
                    if is_id
                    else Project.objects.search_by_name(name_or_id))
        return [project_fields(project) for project in projects]
    except Project.DoesNotExist:
        return []