blob: 8ef5feeb42c351dcb84a8618d8f760197d182d8f [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Helper functions and classes used when searching for projects."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import logging
12
13from businesslogic import work_env
14from framework import framework_helpers
15from framework import paginate
16from framework import permissions
17
18
19DEFAULT_RESULTS_PER_PAGE = 100
20
21
22class ProjectSearchPipeline(object):
23 """Manage the process of project search, filter, fetch, and pagination."""
24
25 def __init__(self, mr, services,
26 default_results_per_page=DEFAULT_RESULTS_PER_PAGE):
27
28 self.mr = mr
29 self.services = services
30 self.default_results_per_page = default_results_per_page
31 self.pagination = None
32 self.allowed_project_ids = None
33 self.visible_results = None
34
35 def SearchForIDs(self, domain=None):
36 """Get project IDs the user has permission to view."""
37 with work_env.WorkEnv(self.mr, self.services) as we:
38 self.allowed_project_ids = we.ListProjects(domain=domain)
39 logging.info('allowed_project_ids is %r', self.allowed_project_ids)
40
41 def GetProjectsAndPaginate(self, cnxn, list_page_url):
42 """Paginate the filtered list of project names and retrieve Project PBs.
43
44 Args:
45 cnxn: connection to SQL database.
46 list_page_url: string page URL for prev and next links.
47 """
48 with self.mr.profiler.Phase('getting all projects'):
49 project_dict = self.services.project.GetProjects(
50 cnxn, self.allowed_project_ids)
51 project_list = sorted(
52 project_dict.values(),
53 key=lambda p: p.project_name)
54 logging.info('project_list is %r', project_list)
55
56 url_params = [(name, self.mr.GetParam(name)) for name in
57 framework_helpers.RECOGNIZED_PARAMS]
58 self.pagination = paginate.ArtifactPagination(
59 project_list,
60 self.mr.GetPositiveIntParam('num', self.default_results_per_page),
61 self.mr.GetPositiveIntParam('start'), self.mr.project_name,
62 list_page_url, url_params=url_params)
63 self.visible_results = self.pagination.visible_results