Commit c7da4899 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

astakos: Add member mode in GET projects

Get only active projects where request user is an active member if mode is
set to `member'.
parent 09020f2f
......@@ -804,13 +804,15 @@ class AstakosClient(object):
# ----------------------------
# do a GET to ``API_PROJECTS``
def get_projects(self, name=None, state=None, owner=None):
def get_projects(self, name=None, state=None, owner=None, mode=None):
"""Retrieve all accessible projects
Arguments:
name -- filter by name (optional)
state -- filter by state (optional)
owner -- filter by owner (optional)
mode -- if value is 'member', return only active projects in which
the request user is an active member
In case of success, return a list of project descriptions.
"""
......@@ -821,6 +823,8 @@ class AstakosClient(object):
filters["state"] = state
if owner is not None:
filters["owner"] = owner
if mode is not None:
filters["mode"] = mode
req_headers = {'content-type': 'application/json'}
req_body = (parse_request({"filter": filters}, self.logger)
if filters else None)
......
......@@ -189,7 +189,7 @@ retry=0, use_pool=False, pool_size=8, logger=None\ **)**
rejected and which failed to resolved. Otherwise raise an
AstakosClientException exception.
**get_projects(**\ name=None, state=None, owner=None\ **)**
**get_projects(**\ name=None, state=None, owner=None, mode=None\ **)**
Retrieve all accessible projects
**get_project(**\ project_id\ **)**
......
......@@ -281,23 +281,33 @@ def get_projects(request):
user = request.user
input_data = read_json_body(request, default={})
filters = input_data.get("filter", {})
mode = input_data.get("mode", "default")
query = make_project_query(filters)
projects = _get_projects(query, request_user=user)
projects = _get_projects(query, mode=mode, request_user=user)
data = get_projects_details(projects, request_user=user)
return json_response(data)
def _get_projects(query, request_user=None):
def _get_projects(query, mode="default", request_user=None):
projects = Project.objects.filter(query)
if not request_user.is_project_admin():
membs = request_user.projectmembership_set.any_accepted()
if mode == "member":
membs = request_user.projectmembership_set.\
actually_accepted_and_active()
memb_projects = membs.values_list("project", flat=True)
is_memb = Q(id__in=memb_projects)
owned = Q(owner=request_user)
active = (Q(state=Project.NORMAL) &
Q(private=False))
projects = projects.filter(is_memb | owned | active)
projects = projects.filter(is_memb)
elif mode == "default":
if not request_user.is_project_admin():
membs = request_user.projectmembership_set.any_accepted()
memb_projects = membs.values_list("project", flat=True)
is_memb = Q(id__in=memb_projects)
owned = Q(owner=request_user)
active = (Q(state=Project.NORMAL) &
Q(private=False))
projects = projects.filter(is_memb | owned | active)
else:
raise faults.BadRequest("Unrecognized mode '%s'." % mode)
return projects.select_related("last_application")
......
......@@ -1892,6 +1892,11 @@ class ProjectMembershipManager(models.Manager):
q &= Q(project__in=projects)
return self.filter(q)
def actually_accepted_and_active(self):
q = self.model.Q_ACTUALLY_ACCEPTED
q &= Q(project__state=Project.NORMAL)
return self.filter(q)
def initialized(self, projects=None):
q = Q(initialized=True)
if projects is not None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment