Repository : http://git.fedorahosted.org/cgit/copr.git
On branch : search
commit 8d9fd2a1eedd682b41b9df015dedb5263d803e9f Author: Igor Gnatenko i.gnatenko.brain@gmail.com Date: Sun Feb 16 17:38:59 2014 +0400
api: implement search
Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1064118 Signed-off-by: Igor Gnatenko i.gnatenko.brain@gmail.com
coprs_frontend/coprs/logic/coprs_logic.py | 4 ++ coprs_frontend/coprs/templates/api.html | 39 ++++++++++++++++++++++ coprs_frontend/coprs/views/api_ns/api_general.py | 28 +++++++++++++++ 3 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/coprs_frontend/coprs/logic/coprs_logic.py b/coprs_frontend/coprs/logic/coprs_logic.py index 7cab264..19cb5b9 100644 --- a/coprs_frontend/coprs/logic/coprs_logic.py +++ b/coprs_frontend/coprs/logic/coprs_logic.py @@ -47,6 +47,7 @@ class CoprsLogic(object): def get_multiple(cls, user, **kwargs): user_relation = kwargs.get("user_relation", None) username = kwargs.get("username", None) + coprname = kwargs.get("coprname", None) with_mock_chroots = kwargs.get("with_mock_chroots", None) with_builds = kwargs.get("with_builds", None) incl_deleted = kwargs.get("incl_deleted", None) @@ -63,6 +64,9 @@ class CoprsLogic(object): if isinstance(ids, list): # can be an empty list query = query.filter(models.Copr.id.in_(ids))
+ if coprname: + query = query.filter(models.Copr.name == coprname) + if user_relation == "owned": query = query.filter( models.User.openid_name == models.User.openidize_name(username)) diff --git a/coprs_frontend/coprs/templates/api.html b/coprs_frontend/coprs/templates/api.html index e835d0f..1e9ee9a 100644 --- a/coprs_frontend/coprs/templates/api.html +++ b/coprs_frontend/coprs/templates/api.html @@ -333,5 +333,44 @@ copr_url = http://copr.fedoraproject.org } </pre>
+ <h3>Search for project</h3> + + <h4>URL:</h4> + <pre style="font-size:120%">/api/coprs/search/<project>/</pre> + <div>or</div> + <pre style="font-size:120%">/api/coprs/?search="<project>"</pre> + + <h4>URL parameters:</h4> + <ul> + <li><b>project</b> – The name of the project whose you'd like + to find</li> + </ul> + + <h4>Result:</h4> + <ul> + <li><b>"users"</b> – List of users in given format: + <ul> + <li><b>"name"</b> – Name of the user</li> + </ul> + </ul> + + <h4>Example call URL</h4> + <pre style="font-size:120%">https://copr.fedoraproject.org/api/coprs/search/tests/</pre> + + <h4>Example results</h4> + <pre> + { + "output": "ok", + "users": [ + { + "name": "ignatenkobrain" + }, + { + "name": "msuchy" + } + ] + } + </pre> + </div> {% endblock %} diff --git a/coprs_frontend/coprs/views/api_ns/api_general.py b/coprs_frontend/coprs/views/api_ns/api_general.py index 0a673d2..527f49b 100644 --- a/coprs_frontend/coprs/views/api_ns/api_general.py +++ b/coprs_frontend/coprs/views/api_ns/api_general.py @@ -340,3 +340,31 @@ def copr_chroot_details(username, coprname, chrootname): jsonout = flask.jsonify(output) jsonout.status_code = httpcode return jsonout + +@api_ns.route("/coprs/search/") +@api_ns.route("/coprs/search/<project>/") +def api_coprs_search_by_project(project=None): + """ Return the list of coprs found in search by the given project. + project is taken either from GET params or from the URL itself + (in this order). + + :arg project: the project one would like find for coprs. + + """ + project = flask.request.args.get("project", None) or project + httpcode = 200 + if project: + query = coprs_logic.CoprsLogic.get_multiple( + flask.g.user, coprname=project) + + repos = query.all() + output = {"output": "ok", "users": []} + for repo in repos: + output["users"].append({"name": repo.owner}) + else: + output = {"output": "notok", "error": "Invalid request"} + httpcode = 500 + + jsonout = flask.jsonify(output) + jsonout.status_code = httpcode + return jsonout