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