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): ...@@ -804,13 +804,15 @@ class AstakosClient(object):
# ---------------------------- # ----------------------------
# do a GET to ``API_PROJECTS`` # 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 """Retrieve all accessible projects
Arguments: Arguments:
name -- filter by name (optional) name -- filter by name (optional)
state -- filter by state (optional) state -- filter by state (optional)
owner -- filter by owner (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. In case of success, return a list of project descriptions.
""" """
...@@ -821,6 +823,8 @@ class AstakosClient(object): ...@@ -821,6 +823,8 @@ class AstakosClient(object):
filters["state"] = state filters["state"] = state
if owner is not None: if owner is not None:
filters["owner"] = owner filters["owner"] = owner
if mode is not None:
filters["mode"] = mode
req_headers = {'content-type': 'application/json'} req_headers = {'content-type': 'application/json'}
req_body = (parse_request({"filter": filters}, self.logger) req_body = (parse_request({"filter": filters}, self.logger)
if filters else None) if filters else None)
......
...@@ -189,7 +189,7 @@ retry=0, use_pool=False, pool_size=8, logger=None\ **)** ...@@ -189,7 +189,7 @@ retry=0, use_pool=False, pool_size=8, logger=None\ **)**
rejected and which failed to resolved. Otherwise raise an rejected and which failed to resolved. Otherwise raise an
AstakosClientException exception. AstakosClientException exception.
**get_projects(**\ name=None, state=None, owner=None\ **)** **get_projects(**\ name=None, state=None, owner=None, mode=None\ **)**
Retrieve all accessible projects Retrieve all accessible projects
**get_project(**\ project_id\ **)** **get_project(**\ project_id\ **)**
......
...@@ -281,23 +281,33 @@ def get_projects(request): ...@@ -281,23 +281,33 @@ def get_projects(request):
user = request.user user = request.user
input_data = read_json_body(request, default={}) input_data = read_json_body(request, default={})
filters = input_data.get("filter", {}) filters = input_data.get("filter", {})
mode = input_data.get("mode", "default")
query = make_project_query(filters) 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) data = get_projects_details(projects, request_user=user)
return json_response(data) 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) projects = Project.objects.filter(query)
if not request_user.is_project_admin(): if mode == "member":
membs = request_user.projectmembership_set.any_accepted() membs = request_user.projectmembership_set.\
actually_accepted_and_active()
memb_projects = membs.values_list("project", flat=True) memb_projects = membs.values_list("project", flat=True)
is_memb = Q(id__in=memb_projects) is_memb = Q(id__in=memb_projects)
owned = Q(owner=request_user) projects = projects.filter(is_memb)
active = (Q(state=Project.NORMAL) & elif mode == "default":
Q(private=False)) if not request_user.is_project_admin():
projects = projects.filter(is_memb | owned | active) 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") return projects.select_related("last_application")
......
...@@ -1892,6 +1892,11 @@ class ProjectMembershipManager(models.Manager): ...@@ -1892,6 +1892,11 @@ class ProjectMembershipManager(models.Manager):
q &= Q(project__in=projects) q &= Q(project__in=projects)
return self.filter(q) 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): def initialized(self, projects=None):
q = Q(initialized=True) q = Q(initialized=True)
if projects is not None: 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