예제 #1
0
def _get_sorted_matching_repositories(lookup_value,
                                      repo_kind="image",
                                      include_private=False,
                                      search_fields=None,
                                      ids_only=False):
    """
    Returns a query of repositories matching the given lookup string, with optional inclusion of
    private repositories.

    Note that this method does *not* filter results based on visibility to users.
    """
    select_fields = [Repository.id] if ids_only else [Repository, Namespace]

    if not lookup_value:
        # This is a generic listing of repositories. Simply return the sorted repositories based
        # on RepositorySearchScore.
        query = (Repository.select(
            *select_fields).join(RepositorySearchScore).where(
                Repository.state != RepositoryState.MARKED_FOR_DELETION).
                 order_by(RepositorySearchScore.score.desc(),
                          RepositorySearchScore.id))
    else:
        if search_fields is None:
            search_fields = set(
                [SEARCH_FIELDS.description.name, SEARCH_FIELDS.name.name])

        # Always search at least on name (init clause)
        clause = Repository.name.match(lookup_value)
        computed_score = RepositorySearchScore.score.alias("score")

        # If the description field is in the search fields, then we need to compute a synthetic score
        # to discount the weight of the description more than the name.
        if SEARCH_FIELDS.description.name in search_fields:
            clause = Repository.description.match(lookup_value) | clause
            cases = [
                (Repository.name.match(lookup_value),
                 100 * RepositorySearchScore.score),
            ]
            computed_score = Case(None, cases,
                                  RepositorySearchScore.score).alias("score")

        select_fields.append(computed_score)
        query = (Repository.select(
            *select_fields).join(RepositorySearchScore).where(clause).where(
                Repository.state != RepositoryState.MARKED_FOR_DELETION).
                 order_by(SQL("score").desc(), RepositorySearchScore.id))

    if repo_kind is not None:
        query = query.where(
            Repository.kind == Repository.kind.get_id(repo_kind))

    if not include_private:
        query = query.where(
            Repository.visibility == _basequery.get_public_repo_visibility())

    if not ids_only:
        query = query.switch(Repository).join(
            Namespace, on=(Namespace.id == Repository.namespace_user))

    return query
예제 #2
0
def get_public_repo_visibility():
    return _basequery.get_public_repo_visibility()
예제 #3
0
def is_repository_public(repository):
    return repository.visibility_id == _basequery.get_public_repo_visibility(
    ).id