From 540dfd5343d2d5da435b1f13a298ad3c0e78ab0e Mon Sep 17 00:00:00 2001 From: yackob03 Date: Fri, 27 Sep 2013 23:25:57 -0400 Subject: [PATCH] Switch the search to use SQL query. --- data/model.py | 27 ++++++++++++++++++++------- endpoints/api.py | 7 +++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/data/model.py b/data/model.py index 6e5dd0c17..9ef8d8ce9 100644 --- a/data/model.py +++ b/data/model.py @@ -1,6 +1,7 @@ import bcrypt import logging import dateutil.parser +import operator from database import * from util.validation import (validate_email, validate_username, @@ -95,11 +96,23 @@ def get_token(code): return AccessToken.get(AccessToken.code == code) -def get_matching_repositories(repo_term): - clauses = (Repository.name ** ('%' + repo_term + '%') | - Repository.namespace ** ('%' + repo_term + '%') | - Repository.description ** ('%' + repo_term + '%')) - return list(Repository.select().where(clauses).limit(10)) +def get_matching_repositories(repo_term, username=None): + query = Repository.select().distinct().limit(10).join(Visibility) + or_clauses = [(Visibility.name == 'public')] + + if username: + with_perms = query.switch(Repository).join(RepositoryPermission, + JOIN_LEFT_OUTER) + query = with_perms.join(User) + or_clauses.append(User.username == username) + + search_clauses = (Repository.name ** ('%' + repo_term + '%') | + Repository.namespace ** ('%' + repo_term + '%') | + Repository.description ** ('%' + repo_term + '%')) + + + final = query.where(search_clauses).where(reduce(operator.or_, or_clauses)) + return list(final) def change_password(user, new_password): @@ -174,8 +187,8 @@ def set_image_metadata(image_id, namespace_name, repository_name, created_date_str, comment): joined = Image.select().join(Repository) image_list = list(joined.where(Repository.name == repository_name, - Repository.namespace == namespace_name, - Image.image_id == image_id)) + Repository.namespace == namespace_name, + Image.image_id == image_id)) if not image_list: raise DataModelException('No image with specified id and repository') diff --git a/endpoints/api.py b/endpoints/api.py index fc07f3a5e..1954f8ff9 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -64,11 +64,10 @@ def match_repos_api(prefix): 'description': repo.description, } - matching = model.get_matching_repositories(prefix) - repos = [repo_view(repo) for repo in matching - if ReadRepositoryPermission(repo.namespace, repo.name).can()] + username = current_user.db_user.username + matching = model.get_matching_repositories(prefix, username) response = { - 'repositories': repos + 'repositories': [repo_view(repo) for repo in matching] } return jsonify(response)