Revision 0de7c5a7
b/snf-astakos-app/astakos/im/management/commands/project-list.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
from optparse import make_option |
35 | 35 |
|
36 |
from snf_django.management.commands import SynnefoCommand, CommandError
|
|
36 |
from snf_django.management.commands import ListCommand
|
|
37 | 37 |
|
38 | 38 |
from astakos.im.models import Project, ProjectApplication |
39 |
from django.db.models import Q |
|
40 |
from snf_django.management import utils |
|
41 |
from ._common import is_uuid, is_email |
|
39 |
from ._common import is_uuid |
|
42 | 40 |
|
43 | 41 |
|
44 |
class Command(SynnefoCommand):
|
|
42 |
class Command(ListCommand):
|
|
45 | 43 |
help = """List projects and project status. |
46 | 44 |
|
47 | 45 |
Project status can be one of: |
... | ... | |
66 | 64 |
|
67 | 65 |
Deleted an uninitialized, deleted project""" |
68 | 66 |
|
69 |
option_list = SynnefoCommand.option_list + ( |
|
67 |
object_class = Project |
|
68 |
select_related = ["last_application", "owner"] |
|
69 |
|
|
70 |
option_list = ListCommand.option_list + ( |
|
70 | 71 |
make_option('--new', |
71 | 72 |
action='store_true', |
72 | 73 |
dest='new', |
... | ... | |
88 | 89 |
dest='deleted', |
89 | 90 |
default=False, |
90 | 91 |
help="Also so cancelled/terminated projects"), |
91 |
make_option('--name', |
|
92 |
dest='name', |
|
93 |
help='Filter projects by name'), |
|
94 |
make_option('--owner', |
|
95 |
dest='owner', |
|
96 |
help='Filter projects by owner\'s email or uuid'), |
|
97 | 92 |
) |
98 | 93 |
|
99 |
def handle(self, *args, **options): |
|
94 |
def get_owner(project): |
|
95 |
return project.owner.email if project.owner else None |
|
100 | 96 |
|
101 |
flt = Q() |
|
102 |
owner = options['owner'] |
|
103 |
if owner: |
|
104 |
flt &= filter_by_owner(owner) |
|
97 |
def get_status(project): |
|
98 |
return project.state_display() |
|
105 | 99 |
|
106 |
name = options['name'] |
|
107 |
if name: |
|
108 |
flt &= Q(realname=name) |
|
100 |
def get_pending_app(project): |
|
101 |
app = project.last_application |
|
102 |
return app.id if app and app.state == app.PENDING else "" |
|
103 |
|
|
104 |
FIELDS = { |
|
105 |
"id": ("uuid", "Project ID"), |
|
106 |
"name": ("realname", "Project Name"), |
|
107 |
"owner": (get_owner, "Project Owner"), |
|
108 |
"status": (get_status, "Project Status"), |
|
109 |
"pending_app": (get_pending_app, |
|
110 |
"An application pending for the project"), |
|
111 |
} |
|
112 |
|
|
113 |
fields = ["id", "name", "owner", "status", "pending_app"] |
|
114 |
|
|
115 |
def handle_args(self, *args, **options): |
|
116 |
try: |
|
117 |
name_filter = self.filters.pop("name") |
|
118 |
self.filters["realname"] = name_filter |
|
119 |
except KeyError: |
|
120 |
pass |
|
121 |
|
|
122 |
try: |
|
123 |
owner_filter = self.filters.pop("owner") |
|
124 |
if owner_filter is not None: |
|
125 |
if is_uuid(owner_filter): |
|
126 |
self.filters["owner__uuid"] = owner_filter |
|
127 |
else: |
|
128 |
self.filters["owner__email"] = owner_filter |
|
129 |
except KeyError: |
|
130 |
pass |
|
109 | 131 |
|
110 | 132 |
if not options['deleted']: |
111 |
flt &= ~Q(state__in=Project.SKIP_STATES) |
|
112 |
|
|
113 |
pending = Q(last_application__isnull=False, |
|
114 |
last_application__state=ProjectApplication.PENDING) |
|
133 |
self.excludes["state__in"] = Project.SKIP_STATES |
|
115 | 134 |
|
116 |
if options['pending']:
|
|
117 |
flt &= pending
|
|
135 |
if options["pending"]:
|
|
136 |
self.filter_pending()
|
|
118 | 137 |
else: |
119 | 138 |
if options['new']: |
120 |
flt &= pending & Q(state=Project.UNINITIALIZED) |
|
139 |
self.filter_pending() |
|
140 |
self.filters["state"] = Project.UNINITIALIZED |
|
121 | 141 |
if options['modified']: |
122 |
flt &= pending & Q(state__in=Project.INITIALIZED_STATES) |
|
123 |
|
|
124 |
projects = Project.objects.\ |
|
125 |
select_related("last_application", "owner").filter(flt) |
|
126 |
|
|
127 |
labels = ('ProjID', 'Name', 'Owner', 'Status', 'Pending AppID') |
|
142 |
self.filter_pending() |
|
143 |
self.filters["state__in"] = Project.INITIALIZED_STATES |
|
128 | 144 |
|
129 |
info = project_info(projects) |
|
130 |
utils.pprint_table(self.stdout, info, labels, |
|
131 |
options["output_format"]) |
|
132 |
|
|
133 |
|
|
134 |
def filter_by_owner(s): |
|
135 |
if is_email(s): |
|
136 |
return Q(owner__email=s) |
|
137 |
if is_uuid(s): |
|
138 |
return Q(owner__uuid=s) |
|
139 |
raise CommandError("Expecting either email or uuid.") |
|
140 |
|
|
141 |
|
|
142 |
def project_info(projects): |
|
143 |
l = [] |
|
144 |
for project in projects: |
|
145 |
status = project.state_display() |
|
146 |
app = project.last_application |
|
147 |
pending_appid = app.id if app and app.state == app.PENDING else "" |
|
148 |
|
|
149 |
t = (project.uuid, |
|
150 |
project.realname, |
|
151 |
project.owner.email if project.owner else None, |
|
152 |
status, |
|
153 |
pending_appid, |
|
154 |
) |
|
155 |
l.append(t) |
|
156 |
return l |
|
145 |
def filter_pending(self): |
|
146 |
self.filters["last_application__state"] = ProjectApplication.PENDING |
Also available in: Unified diff