Exemplo n.º 1
0
    def get(self, parsed_args):
        """
        Get a list of apps and repositories that match the specified query.
        """
        query = parsed_args["query"]
        page = min(max(1, parsed_args["page"]), MAX_RESULT_PAGE_COUNT)
        offset = (page - 1) * MAX_PER_PAGE
        limit = MAX_PER_PAGE + 1

        username = get_authenticated_user().username if get_authenticated_user(
        ) else None

        # Lookup matching repositories.
        matching_repos = list(
            model.repository.get_filtered_matching_repositories(query,
                                                                username,
                                                                repo_kind=None,
                                                                limit=limit,
                                                                offset=offset))

        assert len(matching_repos) <= limit
        has_additional = len(matching_repos) > MAX_PER_PAGE
        matching_repos = matching_repos[0:MAX_PER_PAGE]

        # Load secondary information such as last modified time, star count and action count.
        last_modified_map = None
        star_map = None
        action_sum_map = None
        if parsed_args["includeUsage"]:
            repository_ids = [repo.id for repo in matching_repos]
            last_modified_map = registry_model.get_most_recent_tag_lifetime_start(
                matching_repos)
            star_map = model.repository.get_stars(repository_ids)
            action_sum_map = model.log.get_repositories_action_sums(
                repository_ids)

        # Build the results list.
        results = [
            repo_result_view(
                repo,
                username,
                last_modified_map.get(repo.id)
                if last_modified_map is not None else None,
                star_map.get(repo.id, 0) if star_map is not None else None,
                float(action_sum_map.get(repo.id, 0))
                if action_sum_map is not None else None,
            ) for repo in matching_repos
        ]

        return {
            "results": results,
            "has_additional": has_additional,
            "page": page,
            "page_size": MAX_PER_PAGE,
            "start_index": offset,
        }
Exemplo n.º 2
0
    def get(self, parsed_args):
        """ Get a list of apps and repositories that match the specified query. """
        query = parsed_args['query']
        page = min(max(1, parsed_args['page']), MAX_RESULT_PAGE_COUNT)
        offset = (page - 1) * MAX_PER_PAGE
        limit = offset + MAX_PER_PAGE + 1

        username = get_authenticated_user().username if get_authenticated_user(
        ) else None

        # Lookup matching repositories.
        matching_repos = list(
            model.repository.get_filtered_matching_repositories(query,
                                                                username,
                                                                repo_kind=None,
                                                                limit=limit,
                                                                offset=offset))

        # Load secondary information such as last modified time, star count and action count.
        repository_ids = [repo.id for repo in matching_repos]
        last_modified_map = registry_model.get_most_recent_tag_lifetime_start(
            matching_repos)
        star_map = model.repository.get_stars(repository_ids)
        action_sum_map = model.log.get_repositories_action_sums(repository_ids)

        # Build the results list.
        results = [
            repo_result_view(repo, username, last_modified_map.get(repo.id),
                             star_map.get(repo.id, 0),
                             float(action_sum_map.get(repo.id, 0)))
            for repo in matching_repos
        ]

        return {
            'results': results[0:MAX_PER_PAGE],
            'has_additional': len(results) > MAX_PER_PAGE,
            'page': page,
            'page_size': MAX_PER_PAGE,
            'start_index': offset,
        }
Exemplo n.º 3
0
    def get_repo_list(
        self,
        starred,
        user,
        repo_kind,
        namespace,
        username,
        public,
        page_token,
        last_modified,
        popularity,
    ):
        next_page_token = None

        # Lookup the requested repositories (either starred or non-starred.)
        if starred:
            # Return the full list of repos starred by the current user that are still visible to them.
            def can_view_repo(repo):
                assert repo.state != RepositoryState.MARKED_FOR_DELETION
                can_view = ReadRepositoryPermission(repo.namespace_user.username, repo.name).can()
                return can_view or model.repository.is_repository_public(repo)

            unfiltered_repos = model.repository.get_user_starred_repositories(
                user, kind_filter=repo_kind
            )
            repos = [repo for repo in unfiltered_repos if can_view_repo(repo)]
        else:
            # Determine the starting offset for pagination. Note that we don't use the normal
            # model.modelutil.paginate method here, as that does not operate over UNION queries, which
            # get_visible_repositories will return if there is a logged-in user (for performance reasons).
            #
            # Also note the +1 on the limit, as paginate_query uses the extra result to determine whether
            # there is a next page.
            start_id = model.modelutil.pagination_start(page_token)
            repo_query = model.repository.get_visible_repositories(
                username=username,
                include_public=public,
                start_id=start_id,
                limit=REPOS_PER_PAGE + 1,
                kind_filter=repo_kind,
                namespace=namespace,
            )

            repos, next_page_token = model.modelutil.paginate_query(
                repo_query, limit=REPOS_PER_PAGE, sort_field_name="rid"
            )

        repos = list(repos)
        assert len(repos) <= REPOS_PER_PAGE

        # Collect the IDs of the repositories found for subsequent lookup of popularity
        # and/or last modified.
        last_modified_map = {}
        action_sum_map = {}
        if last_modified or popularity:
            repository_refs = [RepositoryReference.for_id(repo.rid) for repo in repos]
            repository_ids = [repo.rid for repo in repos]

            if last_modified:
                last_modified_map = (
                    registry_model.get_most_recent_tag_lifetime_start(repository_refs)
                    if repo_kind == "image"
                    else apprtags_model.get_most_recent_tag_lifetime_start(
                        repository_ids, appr_model.models_ref
                    )
                )

            if popularity:
                action_sum_map = model.log.get_repositories_action_sums(repository_ids)

        # Collect the IDs of the repositories that are starred for the user, so we can mark them
        # in the returned results.
        star_set = set()
        if username:
            starred_repos = model.repository.get_user_starred_repositories(user, repo_kind)
            star_set = {starred.id for starred in starred_repos}

        return (
            [
                RepositoryBaseElement(
                    repo.namespace_user.username,
                    repo.name,
                    repo.rid in star_set,
                    model.repository.is_repository_public(repo),
                    repo_kind,
                    repo.description,
                    repo.namespace_user.organization,
                    repo.namespace_user.removed_tag_expiration_s,
                    last_modified_map.get(repo.rid),
                    action_sum_map.get(repo.rid),
                    last_modified,
                    popularity,
                    username,
                    None,
                    repo.state,
                )
                for repo in repos
            ],
            next_page_token,
        )