Clean up the repository list API and loads stars with it

We load stars with the same list API now so that we get the extra metadata needed in the repo list (popularity and last modified)
This commit is contained in:
Joseph Schorr 2015-07-21 17:20:24 -04:00
parent bb269a56b6
commit a0c4e72f13
7 changed files with 107 additions and 56 deletions

View file

@ -102,45 +102,69 @@ class RepositoryList(ApiResource):
raise Unauthorized()
def _load_repositories(self, namespace=None, public=False, starred=False, limit=None, page=None):
""" Loads the filtered list of repositories and returns it and the star lookup set. """
# Load the starred repositories and username (if applicable)
username = get_authenticated_user().username if get_authenticated_user() else None
# If starred (and only starred) repositories were requested, then load them directly.
if starred and namespace is None and not public:
if not username:
return []
repositories = model.repository.get_user_starred_repositories(get_authenticated_user(),
limit=limit,
page=page)
return repositories, set([repo.id for repo in repositories])
# Otherwise, conduct a full filtered lookup and (optionally) filter by the starred repositories.
starred_repos = []
star_lookup = set()
if username:
starred_repos = model.repository.get_user_starred_repositories(get_authenticated_user())
star_lookup = set([repo.id for repo in starred_repos])
# Find the matching repositories.
repositories = model.repository.get_visible_repositories(username=username,
limit=limit,
page=page,
include_public=public,
namespace=namespace)
# Filter down to just the starred repositories, if asked.
if starred:
return [repo for repo in repositories if repo.id in star_lookup], star_lookup
return repositories, star_lookup
@require_scope(scopes.READ_REPO)
@nickname('listRepos')
@parse_args
@query_param('page', 'Offset page number. (int)', type=int)
@query_param('limit', 'Limit on the number of results (int)', type=int)
@query_param('namespace', 'Namespace to use when querying for org repositories.', type=str)
@query_param('public', 'Whether to include repositories not explicitly visible by the user.',
type=truthy_bool, default=True)
@query_param('private', 'Whether to include private repositories.', type=truthy_bool,
default=True)
@query_param('namespace_only', 'Whether to limit only to the given namespace.',
@query_param('namespace', 'Filters the repositories returned to this namespace', type=str)
@query_param('starred', 'Filters the repositories returned to those starred by the user',
type=truthy_bool, default=False)
@query_param('public', 'Adds any repositories visible to the user by virtue of being public',
type=truthy_bool, default=False)
@query_param('last_modified', 'Whether to include when the repository was last modified.',
type=truthy_bool, default=False)
@query_param('popularity', 'Whether to include the repository\'s popularity metric.',
type=truthy_bool, default=False)
def get(self, args):
"""Fetch the list of repositories under a variety of situations."""
username = None
if get_authenticated_user():
starred_repos = model.repository.get_user_starred_repositories(get_authenticated_user())
star_lookup = set([repo.id for repo in starred_repos])
""" Fetch the list of repositories visible to the current user under a variety of situations.
"""
if args['private']:
username = get_authenticated_user().username
response = {}
# Find the matching repositories.
repo_query = model.repository.get_visible_repositories(username,
limit=args['limit'],
page=args['page'],
include_public=args['public'],
namespace=args['namespace'],
namespace_only=args['namespace_only'])
repositories, star_lookup = self._load_repositories(args['namespace'], args['public'],
args['starred'], args['limit'],
args['page'])
# Collect the IDs of the repositories found for subequent lookup of popularity
# and/or last modified.
repository_ids = [repo.get(RepositoryTable.id) for repo in repo_query]
repository_ids = [repo.id for repo in repositories]
if args['last_modified']:
last_modified_map = model.repository.get_when_last_modified(repository_ids)
@ -150,13 +174,13 @@ class RepositoryList(ApiResource):
def repo_view(repo_obj):
repo = {
'namespace': repo_obj.get(Namespace.username),
'name': repo_obj.get(RepositoryTable.name),
'description': repo_obj.get(RepositoryTable.description),
'is_public': repo_obj.get(Visibility.name) == 'public'
'namespace': repo_obj.namespace_user.username,
'name': repo_obj.name,
'description': repo_obj.description,
'is_public': repo_obj.visibility.name == 'public'
}
repo_id = repo_obj.get(RepositoryTable.id)
repo_id = repo_obj.id
if args['last_modified']:
repo['last_modified'] = last_modified_map.get(repo_id)
@ -169,8 +193,9 @@ class RepositoryList(ApiResource):
return repo
response['repositories'] = [repo_view(repo) for repo in repo_query]
return response
return {
'repositories': [repo_view(repo) for repo in repositories]
}
@resource('/v1/repository/<repopath:repository>')