Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Helper functions and classes used when searching for projects.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import logging |
| 11 | |
| 12 | from businesslogic import work_env |
| 13 | from framework import framework_helpers |
| 14 | from framework import paginate |
| 15 | from framework import permissions |
| 16 | |
| 17 | |
| 18 | DEFAULT_RESULTS_PER_PAGE = 100 |
| 19 | |
| 20 | |
| 21 | class 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 |