blob: 17dead840125f342cd144755da6e3c948f9495a9 [file] [log] [blame]
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
"""View classes to make it easy to display framework objects in EZT."""
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import logging
import time
import ezt
from framework import framework_bizobj
from framework import framework_constants
from framework import framework_helpers
from framework import permissions
from framework import template_helpers
from framework import timestr
from proto import user_pb2
from services import client_config_svc
import settings
class LabelView(object):
"""Wrapper class that makes it easier to display a label via EZT."""
def __init__(self, label, config):
"""Make several values related to this label available as attrs.
label: artifact label string. E.g., 'Priority-High' or 'Frontend'.
config: PB with a well_known_labels list, or None.
""" = label
self.is_restrict = ezt.boolean(permissions.IsRestrictLabel(label))
self.docstring = ''
if config:
for wkl in config.well_known_labels:
if label.lower() == wkl.label.lower():
self.docstring = wkl.label_docstring
if '-' in label:
self.prefix, self.value = label.split('-', 1)
self.prefix, self.value = '', label
class StatusView(object):
"""Wrapper class that makes it easier to display a status via EZT."""
def __init__(self, status, config):
"""Make several values related to this status available as attrs.
status: artifact status string. E.g., 'New' or 'Accepted'.
config: PB with a well_known_statuses list, or None.
""" = status
self.docstring = ''
self.means_open = ezt.boolean(True)
if config:
for wks in config.well_known_statuses:
if status.lower() == wks.status.lower():
self.docstring = wks.status_docstring
self.means_open = ezt.boolean(wks.means_open)
class UserView(object):
"""Wrapper class to easily display basic user information in a template."""
def __init__(self, user, is_group=False):
self.user = user
self.is_group = is_group
email = or ''
self.user_id = user.user_id = email
if user.obscure_email:
self.profile_url = '/u/%s/' % user.user_id
self.profile_url = '/u/%s/' % email
self.obscure_email = user.obscure_email
self.banned = ''
(self.username, self.domain, self.obscured_username,
obscured_email) = framework_bizobj.ParseAndObscureAddress(email)
# No need to obfuscate or reveal client email.
# Instead display a human-readable username.
if self.user_id == framework_constants.DELETED_USER_ID:
self.display_name = framework_constants.DELETED_USER_NAME
self.obscure_email = ''
self.profile_url = ''
elif in client_config_svc.GetServiceAccountMap():
self.display_name = client_config_svc.GetServiceAccountMap()[]
elif not self.obscure_email:
self.display_name = email
self.display_name = obscured_email
self.avail_message, self.avail_state = (
framework_helpers.GetUserAvailability(user, is_group))
self.avail_message_short = template_helpers.FitUnsafeText(
self.avail_message, 35)
def RevealEmail(self):
if not
if not in client_config_svc.GetServiceAccountMap():
self.obscure_email = False
self.display_name =
self.profile_url = '/u/%s/' %
def MakeAllUserViews(
cnxn, user_service, *list_of_user_id_lists, **kw):
"""Make a dict {user_id: user_view, ...} for all user IDs given."""
distinct_user_ids = set()
if None in distinct_user_ids:
group_ids = kw.get('group_ids', [])
user_dict = user_service.GetUsersByIDs(cnxn, distinct_user_ids)
return {user_id: UserView(user_pb, is_group=user_id in group_ids)
for user_id, user_pb in user_dict.items()}
def MakeUserView(cnxn, user_service, user_id):
"""Make a UserView for the given user ID."""
user = user_service.GetUser(cnxn, user_id)
return UserView(user)
def StuffUserView(user_id, email, obscure_email):
"""Construct a UserView with the given parameters for testing."""
user = user_pb2.MakeUser(user_id, email=email, obscure_email=obscure_email)
return UserView(user)
# TODO( Remove optional project.
def RevealAllEmailsToMembers(cnxn, services, auth, users_by_id, project=None):
# type: (MonorailConnection, Services, AuthData, Collection[user_pb2.User],
# Optional[project_pb2.Project] -> None)
"""Reveal emails based on the authenticated user.
The actual behavior can be determined by looking into
framework_bizobj.ShouldRevealEmail. Look at
for context.
This method should be deleted when endpoints and ezt pages are deprecated.
cnxn: MonorailConnection to the database.
services: Services object for connections to backend services.
auth: AuthData object that identifies the logged in user.
users_by_id: dictionary of UserView's that might be displayed.
project: Optional Project PB for the current project.
Nothing, but the UserViews in users_by_id may be modified to
publish email address.
if project:
for user_view in users_by_id.values():
if framework_bizobj.DeprecatedShouldRevealEmail(auth, project,
viewable_users = framework_bizobj.FilterViewableEmails(
cnxn, services, auth, users_by_id.values())
for user_view in viewable_users:
def RevealAllEmails(users_by_id):
"""Allow anyone to see unobscured email addresses of project members.
The modified view objects should only be used to generate views for other
project members.
users_by_id: dictionary of UserViews that will be displayed.
Nothing, but the UserViews in users_by_id may be modified to
publish email address.
for user_view in users_by_id.values():
def GetViewedUserDisplayName(mr):
"""Get display name of the viewed user given the logged-in user."""
# Do not obscure email if current user is a site admin. Do not obscure
# email if current user is viewing their own profile. For all other
# cases do whatever obscure_email setting for the user is.
viewing_self = mr.auth.user_id == mr.viewed_user_auth.user_id
email_obscured = (not(mr.auth.user_pb.is_site_admin or viewing_self)
and mr.viewed_user_auth.user_view.obscure_email)
if email_obscured:
(_username, _domain, _obscured_username,
obscured_email) = framework_bizobj.ParseAndObscureAddress(
viewed_user_display_name = obscured_email
viewed_user_display_name =
return viewed_user_display_name