Switch the search to use SQL query.

This commit is contained in:
yackob03 2013-09-27 23:25:57 -04:00
parent a0adffdc8e
commit 540dfd5343
2 changed files with 23 additions and 11 deletions

View file

@ -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')

View file

@ -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)