Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: appengine/monorail/sitewide/sitewide_helpers.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is govered 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
8 import logging
9
10 from framework import permissions
11 from proto import project_pb2
12
13
14 def GetViewableStarredProjects(
15 cnxn, services, viewed_user_id, effective_ids, logged_in_user):
16 """Returns a list of viewable starred projects."""
17 starred_project_ids = services.project_star.LookupStarredItemIDs(
18 cnxn, viewed_user_id)
19 projects = services.project.GetProjects(cnxn, starred_project_ids).values()
20 viewable_projects = FilterViewableProjects(
21 projects, logged_in_user, effective_ids)
22 return viewable_projects
23
24
25 def FilterViewableProjects(project_list, logged_in_user, effective_ids):
26 """Return subset of LIVE project protobufs viewable by the given user."""
27 viewable_projects = []
28 for project in project_list:
29 if (project.state == project_pb2.ProjectState.LIVE and
30 permissions.UserCanViewProject(
31 logged_in_user, effective_ids, project)):
32 viewable_projects.append(project)
33
34 return viewable_projects
35
36
37 def GetUserProjects(
38 cnxn, services, user, effective_ids, viewed_user_effective_ids):
39 """Get the projects to display in the user's profile.
40
41 Args:
42 cnxn: connection to the SQL database.
43 services: An instance of services
44 user: The user doing the viewing.
45 effective_ids: set of int user IDs of the user viewing the projects
46 (including any user group IDs).
47 viewed_user_effective_ids: set of int user IDs of the user being viewed.
48
49 Returns:
50 A 4-tuple of lists of PBs:
51 - live projects the viewed user owns
52 - archived projects the viewed user owns
53 - live projects the viewed user is a member of
54 - live projects the viewed user is a contributor to
55
56 Any projects the viewing user should not be able to see are filtered out.
57 Admins can see everything, while other users can see all non-locked
58 projects they own or are a member of, as well as all live projects.
59 """
60 (owned_project_ids, membered_project_ids,
61 contrib_project_ids) = services.project.GetUserRolesInAllProjects(
62 cnxn, viewed_user_effective_ids)
63
64 # Each project should only be considered for at most one role category.
65 # We keep the highest ranking roles and discard lower-ranking ones.
66 membered_project_ids.difference_update(owned_project_ids)
67 contrib_project_ids.difference_update(owned_project_ids)
68 contrib_project_ids.difference_update(membered_project_ids)
69
70 # Build a dictionary of (project_id -> project)
71 # so that we can check permissions.
72 combined = owned_project_ids.union(membered_project_ids).union(
73 contrib_project_ids)
74 projects_dict = services.project.GetProjects(cnxn, combined)
75 projects_dict = _FilterProjectDict(user, effective_ids, projects_dict)
76
77 visible_ownership = _PickProjects(owned_project_ids, projects_dict)
78 visible_archived = _PickProjects(
79 owned_project_ids, projects_dict, archived=True)
80 visible_membership = _PickProjects(membered_project_ids, projects_dict)
81 visible_contrib = _PickProjects(contrib_project_ids, projects_dict)
82
83 return (_SortProjects(visible_ownership), _SortProjects(visible_archived),
84 _SortProjects(visible_membership), _SortProjects(visible_contrib))
85
86
87 def _SortProjects(projects):
88 return sorted(projects, key=lambda p: p.project_name)
89
90
91 def _PickProjects(project_ids, projects_dict, archived=False):
92 """Select the projects named in project_ids from a preloaded dictionary.
93
94 Args:
95 project_ids: list of project_ids for the desired projects.
96 projects_dict: dict {project_id: ProjectPB, ...} of a lot
97 of preloaded projects, including all the desired ones that exist.
98 archived: set to True if you want to return projects that are in a
99 ARCHIVED state instead of those that are not.
100
101 Returns:
102 A list of Project PBs for the desired projects. If one of them is
103 not found in projects_dict, it is ignored.
104 """
105 # Done in 3 steps: lookup all existing requested projects, filter out
106 # DELETABLE ones, then filter out ARCHIVED or non-ARCHIVED.
107 results = [projects_dict.get(pid) for pid in project_ids
108 if pid in projects_dict]
109 results = [proj for proj in results
110 if proj.state != project_pb2.ProjectState.DELETABLE]
111 results = [proj for proj in results
112 if archived == (proj.state == project_pb2.ProjectState.ARCHIVED)]
113 return results
114
115
116 def _FilterProjectDict(user, effective_ids, projects_dict):
117 """Return a new project dictionary which contains only viewable projects."""
118 return {
119 pid: project
120 for pid, project in projects_dict.iteritems()
121 if permissions.UserCanViewProject(user, effective_ids, project)
122 }
OLDNEW
« no previous file with comments | « appengine/monorail/sitewide/projectsearch.py ('k') | appengine/monorail/sitewide/sitewide_views.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698