From fd7136712a4abf4c72e9304298410c46e515300d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 11 2019 15:36:07 +0000 Subject: Add support for !owner to the API listing projects When querying the list of projects one can filter the projects returned by their main maintainer, using the filter argument `owner. With this commit, if the filter argument starts with the `!`, instead of filtering for project having this person as maintainer, it will allow to filter for project not having this person as main maintainer. Fixes https://pagure.io/pagure/issue/4312 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/lib/query.py b/pagure/lib/query.py index 1327637..a9257f2 100644 --- a/pagure/lib/query.py +++ b/pagure/lib/query.py @@ -2271,7 +2271,14 @@ def search_projects( "as parameters in the `search_projects` function" ) elif owner is not None: - projects = projects.join(model.User).filter(model.User.user == owner) + if owner.startswith("!"): + projects = projects.join(model.User).filter( + model.User.user != owner[1:] + ) + else: + projects = projects.join(model.User).filter( + model.User.user == owner + ) elif username is not None: projects = projects.filter( # User created the project diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index 1175abb..80cea7b 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -362,6 +362,77 @@ class PagureFlaskApiProjecttests(tests.Modeltests): self.maxDiff = None self.assertDictEqual(data, expected_data) + def test_api_projects_owner(self): + """ Test the api_projects method of the flask api. """ + tests.create_projects(self.session) + + output = self.app.get('/api/0/projects?owner=foo') + self.assertEqual(output.status_code, 200) + data = json.loads(output.get_data(as_text=True)) + del data['pagination'] + expected_data = { + "args": { + "fork": None, + "namespace": None, + "owner": "foo", + "page": 1, + "pattern": None, + "per_page": 20, + "short": False, + "tags": [], + "username": None + }, + "projects": [], + "total_projects": 0 + } + self.maxDiff = None + self.assertDictEqual(data, expected_data) + + def test_api_projects_not_owner(self): + """ Test the api_projects method of the flask api. """ + tests.create_projects(self.session) + + output = self.app.get('/api/0/projects?owner=!foo&short=1') + self.assertEqual(output.status_code, 200) + data = json.loads(output.get_data(as_text=True)) + del data['pagination'] + expected_data = { + "args": { + "fork": None, + "namespace": None, + "owner": "!foo", + "page": 1, + "pattern": None, + "per_page": 20, + "short": True, + "tags": [], + "username": None + }, + "projects": [ + { + "description": "test project #1", + "fullname": "test", + "name": "test", + "namespace": None + }, + { + "description": "test project #2", + "fullname": "test2", + "name": "test2", + "namespace": None + }, + { + "description": "namespaced test project", + "fullname": "somenamespace/test3", + "name": "test3", + "namespace": "somenamespace" + } + ], + "total_projects": 3 + } + self.maxDiff = None + self.assertDictEqual(data, expected_data) + def test_api_projects(self): """ Test the api_projects method of the flask api. """ tests.create_projects(self.session)