Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # 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.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | |
| 13 | from businesslogic import work_env |
| 14 | from framework import framework_helpers |
| 15 | from framework import paginate |
| 16 | from framework import permissions |
| 17 | |
| 18 | |
| 19 | DEFAULT_RESULTS_PER_PAGE = 100 |
| 20 | |
| 21 | |
| 22 | class 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 |