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 used in sitewide servlets.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | |
| 13 | from framework import permissions |
| 14 | from proto import project_pb2 |
| 15 | |
| 16 | |
| 17 | def GetViewableStarredProjects( |
| 18 | cnxn, services, viewed_user_id, effective_ids, logged_in_user): |
| 19 | """Returns a list of viewable starred projects.""" |
| 20 | starred_project_ids = services.project_star.LookupStarredItemIDs( |
| 21 | cnxn, viewed_user_id) |
| 22 | projects = list( |
| 23 | services.project.GetProjects(cnxn, starred_project_ids).values()) |
| 24 | viewable_projects = FilterViewableProjects( |
| 25 | projects, logged_in_user, effective_ids) |
| 26 | return viewable_projects |
| 27 | |
| 28 | |
| 29 | def FilterViewableProjects(project_list, logged_in_user, effective_ids): |
| 30 | """Return subset of LIVE project protobufs viewable by the given user.""" |
| 31 | viewable_projects = [] |
| 32 | for project in project_list: |
| 33 | if (project.state == project_pb2.ProjectState.LIVE and |
| 34 | permissions.UserCanViewProject( |
| 35 | logged_in_user, effective_ids, project)): |
| 36 | viewable_projects.append(project) |
| 37 | |
| 38 | return viewable_projects |