root / snf-astakos-app / astakos / im / management / commands / project-show.py @ 6e212c04
History | View | Annotate | Download (5.8 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 ProjectApplication, Project |
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 |
) |
54 |
|
55 |
def handle(self, *args, **options): |
56 |
if len(args) != 1: |
57 |
raise CommandError("Please provide ID or name") |
58 |
|
59 |
name_or_id = args[0]
|
60 |
is_id = name_or_id.isdigit() |
61 |
if is_id:
|
62 |
name_or_id = int(name_or_id)
|
63 |
|
64 |
search_application = True if options['app'] else False |
65 |
|
66 |
infolist = (app_info(name_or_id, is_id) if search_application
|
67 |
else project_info(name_or_id, is_id))
|
68 |
|
69 |
if not infolist: |
70 |
kind = 'project application' if search_application else 'project' |
71 |
field = 'id' if is_id else 'name' |
72 |
msg = "Unknown %s with %s '%s'" % (kind, field, name_or_id)
|
73 |
raise CommandError(msg)
|
74 |
|
75 |
for info in infolist: |
76 |
self.show_info(info)
|
77 |
|
78 |
def show_info(self, info): |
79 |
for key, val in info.items(): |
80 |
line = '%s: %s\n' % (key.rjust(22), val) |
81 |
self.stdout.write(line.encode('utf8')) |
82 |
self.stdout.write('\n') |
83 |
|
84 |
|
85 |
def app_fields(app): |
86 |
d = OrderedDict([ |
87 |
('application id', app.id),
|
88 |
('project id', app.chain),
|
89 |
('name', app.name),
|
90 |
('owner', app.owner),
|
91 |
('status', app.state_display()),
|
92 |
('homepage', app.homepage),
|
93 |
('description', app.description),
|
94 |
('issue date', format_date(app.issue_date)),
|
95 |
('start date', format_date(app.start_date)),
|
96 |
('end date', format_date(app.end_date)),
|
97 |
('comments', app.comments),
|
98 |
('resources', app.resource_policies),
|
99 |
('join policy', app.member_join_policy_display),
|
100 |
('leave policy', app.member_leave_policy_display),
|
101 |
('max members', app.limit_on_members_number),
|
102 |
]) |
103 |
|
104 |
return d
|
105 |
|
106 |
|
107 |
def project_fields(project): |
108 |
app = project.application |
109 |
d = OrderedDict([ |
110 |
('project id', project.id),
|
111 |
('application id', app.id),
|
112 |
('name', project.name),
|
113 |
('owner', app.owner),
|
114 |
('status', project.admin_state_display()),
|
115 |
('creation date', format_date(project.creation_date)),
|
116 |
]) |
117 |
deact_date = project.deactivation_date |
118 |
if deact_date is not None: |
119 |
d['deactivation date'] = format_date(deact_date)
|
120 |
|
121 |
d.update([ |
122 |
('homepage', app.homepage),
|
123 |
('description', app.description),
|
124 |
('resources', app.resource_policies),
|
125 |
('join policy', app.member_join_policy_display),
|
126 |
('leave policy', app.member_leave_policy_display),
|
127 |
('max members', app.limit_on_members_number),
|
128 |
('total members', project.members_count()),
|
129 |
]) |
130 |
|
131 |
memberships = project.projectmembership_set |
132 |
accepted = [str(m.person) for m in memberships.any_accepted()] |
133 |
requested = [str(m.person) for m in memberships.requested()] |
134 |
suspended = [str(m.person) for m in memberships.suspended()] |
135 |
|
136 |
if accepted:
|
137 |
d['accepted members'] = ', '.join(accepted) |
138 |
|
139 |
if suspended:
|
140 |
d['suspended members'] = ', '.join(suspended) |
141 |
|
142 |
if requested:
|
143 |
d['membership requests'] = ', '.join(requested) |
144 |
|
145 |
return d
|
146 |
|
147 |
|
148 |
def app_info(name_or_id, is_id): |
149 |
try:
|
150 |
apps = ([ProjectApplication.objects.get(id=name_or_id)] |
151 |
if is_id
|
152 |
else ProjectApplication.objects.search_by_name(name_or_id))
|
153 |
return [app_fields(app) for app in apps] |
154 |
except ProjectApplication.DoesNotExist:
|
155 |
return []
|
156 |
|
157 |
|
158 |
def project_info(name_or_id, is_id): |
159 |
try:
|
160 |
projects = ([Project.objects.get(id=name_or_id)] |
161 |
if is_id
|
162 |
else Project.objects.search_by_name(name_or_id))
|
163 |
return [project_fields(project) for project in projects] |
164 |
except Project.DoesNotExist:
|
165 |
return []
|