Remove old search API and switch V1 search to use the new search system
This commit is contained in:
parent
23d85409a6
commit
8ca92d6828
5 changed files with 25 additions and 112 deletions
|
@ -341,27 +341,6 @@ def get_sorted_matching_repositories(prefix, only_public, checker, limit=10):
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def get_matching_repositories(repo_term, username=None, limit=10, include_public=True):
|
|
||||||
namespace_term = repo_term
|
|
||||||
name_term = repo_term
|
|
||||||
|
|
||||||
visible = _visible_repository_query(username, include_public=include_public)
|
|
||||||
|
|
||||||
search_clauses = (Repository.name ** ('%' + name_term + '%') |
|
|
||||||
Namespace.username ** ('%' + namespace_term + '%'))
|
|
||||||
|
|
||||||
# Handle the case where the user has already entered a namespace path.
|
|
||||||
if repo_term.find('/') > 0:
|
|
||||||
parts = repo_term.split('/', 1)
|
|
||||||
namespace_term = '/'.join(parts[:-1])
|
|
||||||
name_term = parts[-1]
|
|
||||||
|
|
||||||
search_clauses = (Repository.name ** ('%' + name_term + '%') &
|
|
||||||
Namespace.username ** ('%' + namespace_term + '%'))
|
|
||||||
|
|
||||||
return visible.where(search_clauses).limit(limit)
|
|
||||||
|
|
||||||
|
|
||||||
def lookup_repository(repo_id):
|
def lookup_repository(repo_id):
|
||||||
try:
|
try:
|
||||||
return Repository.get(Repository.id == repo_id)
|
return Repository.get(Repository.id == repo_id)
|
||||||
|
|
|
@ -95,38 +95,6 @@ class EntitySearch(ApiResource):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@resource('/v1/find/repository')
|
|
||||||
class FindRepositories(ApiResource):
|
|
||||||
""" Resource for finding repositories. """
|
|
||||||
@parse_args
|
|
||||||
@query_param('query', 'The prefix to use when querying for repositories.', type=str, default='')
|
|
||||||
@require_scope(scopes.READ_REPO)
|
|
||||||
@nickname('findRepos')
|
|
||||||
def get(self, args):
|
|
||||||
""" Get a list of repositories that match the specified prefix query. """
|
|
||||||
prefix = args['query']
|
|
||||||
|
|
||||||
def repo_view(repo):
|
|
||||||
return {
|
|
||||||
'namespace': repo.namespace_user.username,
|
|
||||||
'name': repo.name,
|
|
||||||
'description': repo.description
|
|
||||||
}
|
|
||||||
|
|
||||||
username = None
|
|
||||||
user = get_authenticated_user()
|
|
||||||
if user is not None:
|
|
||||||
username = user.username
|
|
||||||
|
|
||||||
matching = model.repository.get_matching_repositories(prefix, username)
|
|
||||||
return {
|
|
||||||
'repositories': [repo_view(repo) for repo in matching
|
|
||||||
if (repo.visibility.name == 'public' or
|
|
||||||
ReadRepositoryPermission(repo.namespace_user.username, repo.name).can())]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def search_entity_view(username, entity, get_short_name=None):
|
def search_entity_view(username, entity, get_short_name=None):
|
||||||
kind = 'user'
|
kind = 'user'
|
||||||
avatar_data = avatar.get_data_for_user(entity)
|
avatar_data = avatar.get_data_for_user(entity)
|
||||||
|
|
|
@ -296,16 +296,31 @@ def put_repository_auth(namespace, repository):
|
||||||
abort(501, 'Not Implemented', issue='not-implemented')
|
abort(501, 'Not Implemented', issue='not-implemented')
|
||||||
|
|
||||||
|
|
||||||
|
def conduct_repo_search(username, query, results):
|
||||||
|
""" Finds matching repositories. """
|
||||||
|
def can_read(repo):
|
||||||
|
if repo.is_public:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return ReadRepositoryPermission(repo.namespace_user.username, repo.name).can()
|
||||||
|
|
||||||
|
only_public = username is None
|
||||||
|
matching_repos = model.repository.get_sorted_matching_repositories(query, only_public, can_read,
|
||||||
|
limit=5)
|
||||||
|
|
||||||
|
for repo in matching_repos:
|
||||||
|
results.append({
|
||||||
|
'name': repo.name,
|
||||||
|
'description': repo.description,
|
||||||
|
'is_public': repo.is_public,
|
||||||
|
'href': '/repository/' + repo.namespace_user.username + '/' + repo.name
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@v1_bp.route('/search', methods=['GET'])
|
@v1_bp.route('/search', methods=['GET'])
|
||||||
@process_auth
|
@process_auth
|
||||||
@anon_protect
|
@anon_protect
|
||||||
def get_search():
|
def get_search():
|
||||||
def result_view(repo):
|
|
||||||
return {
|
|
||||||
"name": repo.namespace_user.username + '/' + repo.name,
|
|
||||||
"description": repo.description
|
|
||||||
}
|
|
||||||
|
|
||||||
query = request.args.get('q')
|
query = request.args.get('q')
|
||||||
|
|
||||||
username = None
|
username = None
|
||||||
|
@ -313,14 +328,8 @@ def get_search():
|
||||||
if user is not None:
|
if user is not None:
|
||||||
username = user.username
|
username = user.username
|
||||||
|
|
||||||
if query:
|
results = []
|
||||||
matching = model.repository.get_matching_repositories(query, username)
|
conduct_repo_search(username, query, results)
|
||||||
else:
|
|
||||||
matching = []
|
|
||||||
|
|
||||||
results = [result_view(repo) for repo in matching
|
|
||||||
if (repo.visibility.name == 'public' or
|
|
||||||
ReadRepositoryPermission(repo.namespace_user.username, repo.name).can())]
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"query": query,
|
"query": query,
|
||||||
|
|
|
@ -12,7 +12,7 @@ from endpoints.api import api_bp, api
|
||||||
|
|
||||||
from endpoints.api.team import TeamMember, TeamMemberList, OrganizationTeam, TeamMemberInvite
|
from endpoints.api.team import TeamMember, TeamMemberList, OrganizationTeam, TeamMemberInvite
|
||||||
from endpoints.api.tag import RepositoryTagImages, RepositoryTag, ListRepositoryTags, RevertTag
|
from endpoints.api.tag import RepositoryTagImages, RepositoryTag, ListRepositoryTags, RevertTag
|
||||||
from endpoints.api.search import FindRepositories, EntitySearch
|
from endpoints.api.search import EntitySearch
|
||||||
from endpoints.api.image import RepositoryImageChanges, RepositoryImage, RepositoryImageList
|
from endpoints.api.image import RepositoryImageChanges, RepositoryImage, RepositoryImageList
|
||||||
from endpoints.api.build import (FileDropResource, RepositoryBuildStatus, RepositoryBuildLogs,
|
from endpoints.api.build import (FileDropResource, RepositoryBuildStatus, RepositoryBuildLogs,
|
||||||
RepositoryBuildList, RepositoryBuildResource)
|
RepositoryBuildList, RepositoryBuildResource)
|
||||||
|
@ -118,25 +118,6 @@ class ApiTestCase(unittest.TestCase):
|
||||||
finished_database_for_testing(self)
|
finished_database_for_testing(self)
|
||||||
|
|
||||||
|
|
||||||
class TestFindRepositories(ApiTestCase):
|
|
||||||
def setUp(self):
|
|
||||||
ApiTestCase.setUp(self)
|
|
||||||
self._set_url(FindRepositories)
|
|
||||||
|
|
||||||
def test_get_anonymous(self):
|
|
||||||
self._run_test('GET', 200, None, None)
|
|
||||||
|
|
||||||
def test_get_freshuser(self):
|
|
||||||
self._run_test('GET', 200, 'freshuser', None)
|
|
||||||
|
|
||||||
def test_get_reader(self):
|
|
||||||
self._run_test('GET', 200, 'reader', None)
|
|
||||||
|
|
||||||
def test_get_devtable(self):
|
|
||||||
self._run_test('GET', 200, 'devtable', None)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestUserStarredRepositoryList(ApiTestCase):
|
class TestUserStarredRepositoryList(ApiTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
ApiTestCase.setUp(self)
|
ApiTestCase.setUp(self)
|
||||||
|
|
|
@ -20,7 +20,7 @@ from data.database import RepositoryActionCount
|
||||||
|
|
||||||
from endpoints.api.team import TeamMember, TeamMemberList, TeamMemberInvite, OrganizationTeam
|
from endpoints.api.team import TeamMember, TeamMemberList, TeamMemberInvite, OrganizationTeam
|
||||||
from endpoints.api.tag import RepositoryTagImages, RepositoryTag, RevertTag, ListRepositoryTags
|
from endpoints.api.tag import RepositoryTagImages, RepositoryTag, RevertTag, ListRepositoryTags
|
||||||
from endpoints.api.search import FindRepositories, EntitySearch, ConductSearch
|
from endpoints.api.search import EntitySearch, ConductSearch
|
||||||
from endpoints.api.image import RepositoryImage, RepositoryImageList
|
from endpoints.api.image import RepositoryImage, RepositoryImageList
|
||||||
from endpoints.api.build import (RepositoryBuildStatus, RepositoryBuildLogs, RepositoryBuildList,
|
from endpoints.api.build import (RepositoryBuildStatus, RepositoryBuildLogs, RepositoryBuildList,
|
||||||
RepositoryBuildResource)
|
RepositoryBuildResource)
|
||||||
|
@ -1323,30 +1323,6 @@ class TestCreateRepo(ApiTestCase):
|
||||||
self.assertEquals('newrepo', json['name'])
|
self.assertEquals('newrepo', json['name'])
|
||||||
|
|
||||||
|
|
||||||
class TestFindRepos(ApiTestCase):
|
|
||||||
def test_findrepos_asguest(self):
|
|
||||||
json = self.getJsonResponse(FindRepositories, params=dict(query='p'))
|
|
||||||
self.assertEquals(len(json['repositories']), 1)
|
|
||||||
|
|
||||||
self.assertEquals(json['repositories'][0]['namespace'], 'public')
|
|
||||||
self.assertEquals(json['repositories'][0]['name'], 'publicrepo')
|
|
||||||
|
|
||||||
def test_findrepos_asuser(self):
|
|
||||||
self.login(NO_ACCESS_USER)
|
|
||||||
|
|
||||||
json = self.getJsonResponse(FindRepositories, params=dict(query='p'))
|
|
||||||
self.assertEquals(len(json['repositories']), 1)
|
|
||||||
|
|
||||||
self.assertEquals(json['repositories'][0]['namespace'], 'public')
|
|
||||||
self.assertEquals(json['repositories'][0]['name'], 'publicrepo')
|
|
||||||
|
|
||||||
def test_findrepos_orgmember(self):
|
|
||||||
self.login(READ_ACCESS_USER)
|
|
||||||
|
|
||||||
json = self.getJsonResponse(FindRepositories, params=dict(query='p'))
|
|
||||||
self.assertGreater(len(json['repositories']), 1)
|
|
||||||
|
|
||||||
|
|
||||||
class TestListRepos(ApiTestCase):
|
class TestListRepos(ApiTestCase):
|
||||||
def test_listrepos_asguest(self):
|
def test_listrepos_asguest(self):
|
||||||
# Queries: Base + the list query
|
# Queries: Base + the list query
|
||||||
|
|
Reference in a new issue