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

Side by Side Diff: appengine/monorail/project/project_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
« no previous file with comments | « appengine/monorail/project/peoplelist.py ('k') | appengine/monorail/project/project_views.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 and classes used by the project pages."""
7
8 import logging
9 import re
10
11 import settings
12 from framework import framework_bizobj
13 from framework import framework_views
14 from project import project_views
15 from proto import project_pb2
16
17
18 _RE_EMAIL_SEPARATORS = re.compile(r'\s|,|;')
19
20
21 def BuildProjectMembers(cnxn, project, user_service):
22 """Gather data for the members section of a project page.
23
24 Args:
25 cnxn: connection to SQL database.
26 project: Project PB of current project.
27 user_service: an instance of UserService for user persistence.
28
29 Returns:
30 A dictionary suitable for use with EZT.
31 """
32 # First, get all needed info on all users in one batch of requests.
33 users_by_id = framework_views.MakeAllUserViews(
34 cnxn, user_service, framework_bizobj.AllProjectMembers(project))
35
36 # Second, group the user proxies by role for display.
37 owner_proxies = [users_by_id[owner_id]
38 for owner_id in project.owner_ids]
39 committer_proxies = [users_by_id[committer_id]
40 for committer_id in project.committer_ids]
41 contributor_proxies = [users_by_id[contrib_id]
42 for contrib_id in project.contributor_ids]
43
44 return {
45 'owners': owner_proxies,
46 'committers': committer_proxies,
47 'contributors': contributor_proxies,
48 'all_members': users_by_id.values(),
49 }
50
51
52 def BuildProjectAccessOptions(project):
53 """Return a list of project access values for use in an HTML menu.
54
55 Args:
56 project: current Project PB, or None when creating a new project.
57
58 Returns:
59 A list of ProjectAccessView objects that can be used in EZT.
60 """
61 access_levels = [project_pb2.ProjectAccess.ANYONE,
62 project_pb2.ProjectAccess.MEMBERS_ONLY]
63 access_views = []
64 for access in access_levels:
65 # Offer the allowed access levels. When editing an existing project,
66 # its current access level may always be kept, even if it is no longer
67 # in the list of allowed access levels for new projects.
68 if (access in settings.allowed_access_levels or
69 (project and access == project.access)):
70 access_views.append(project_views.ProjectAccessView(access))
71
72 return access_views
73
74
75 def ParseUsernames(cnxn, user_service, usernames_text):
76 """Parse all usernames from a text field and return a list of user IDs.
77
78 Args:
79 cnxn: connection to SQL database.
80 user_service: an instance of UserService for user persistence.
81 usernames_text: string that the user entered into a form field for a list
82 of email addresses. Or, None if the browser did not send that value.
83
84 Returns:
85 A set of user IDs for the users named. Or, an empty set if the
86 usernames_field was not in post_data.
87 """
88 if not usernames_text: # The user did not enter any addresses.
89 return set()
90
91 email_list = _RE_EMAIL_SEPARATORS.split(usernames_text)
92 # skip empty strings between consecutive separators
93 email_list = [email for email in email_list if email]
94
95 id_dict = user_service.LookupUserIDs(cnxn, email_list, autocreate=True)
96 return set(id_dict.values())
97
98
99 def ParseProjectAccess(project, access_num_str):
100 """Parse and validate the "access" field out of post_data.
101
102 Args:
103 project: Project PB for the project that was edited, or None if the
104 user is creating a new project.
105 access_num_str: string of digits from the users POST that identifies
106 the desired project access level. Or, None if that widget was not
107 offered to the user.
108
109 Returns:
110 An enum project access level, or None if the user did not specify
111 any value or if the value specified was invalid.
112 """
113 access = None
114 if access_num_str:
115 access_number = int(access_num_str)
116 available_access_levels = BuildProjectAccessOptions(project)
117 allowed_access_choices = [access_view.key for access_view
118 in available_access_levels]
119 if access_number in allowed_access_choices:
120 access = project_pb2.ProjectAccess(access_number)
121
122 return access
123
124
125 def MembersWithout(project, exclude_ids):
126 """Return three lists of member user IDs, with member_ids not in them."""
127 owner_ids = [user_id for user_id in project.owner_ids
128 if user_id not in exclude_ids]
129 committer_ids = [user_id for user_id in project.committer_ids
130 if user_id not in exclude_ids]
131 contributor_ids = [user_id for user_id in project.contributor_ids
132 if user_id not in exclude_ids]
133
134 return owner_ids, committer_ids, contributor_ids
135
136
137 def MembersWith(project, new_member_ids, role):
138 """Return three lists of member IDs with the new IDs in the right one.
139
140 Args:
141 project: Project PB for the project to get current members from.
142 new_member_ids: set of user IDs for members being added.
143 role: string name of the role that new_member_ids should be granted.
144
145 Returns:
146 Three lists of member IDs with new_member_ids added to the appropriate
147 list and removed from any other role.
148
149 Raises:
150 ValueError: if the role is not one of owner, committer, or contributor.
151 """
152 owner_ids, committer_ids, contributor_ids = MembersWithout(
153 project, new_member_ids)
154
155 if role == 'owner':
156 owner_ids.extend(new_member_ids)
157 elif role == 'committer':
158 committer_ids.extend(new_member_ids)
159 elif role == 'contributor':
160 contributor_ids.extend(new_member_ids)
161 else:
162 raise ValueError()
163
164 return owner_ids, committer_ids, contributor_ids
165
166
167 def UsersInvolvedInProject(project):
168 """Return a set of all user IDs referenced in the Project."""
169 result = set()
170 result.update(project.owner_ids)
171 result.update(project.committer_ids)
172 result.update(project.contributor_ids)
173 result.update([perm.member_id for perm in project.extra_perms])
174 return result
OLDNEW
« no previous file with comments | « appengine/monorail/project/peoplelist.py ('k') | appengine/monorail/project/project_views.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698