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 | """View objects to help display projects in EZT.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | import time |
| 13 | |
| 14 | import ezt |
| 15 | |
| 16 | from framework import framework_constants |
| 17 | from framework import framework_helpers |
| 18 | from framework import permissions |
| 19 | from framework import template_helpers |
| 20 | from framework import timestr |
| 21 | from framework import urls |
| 22 | from proto import project_pb2 |
| 23 | |
| 24 | |
| 25 | class ProjectAccessView(object): |
| 26 | """Object for project access information that can be easily used in EZT.""" |
| 27 | |
| 28 | ACCESS_NAMES = { |
| 29 | project_pb2.ProjectAccess.ANYONE: 'Anyone on the Internet', |
| 30 | project_pb2.ProjectAccess.MEMBERS_ONLY: 'Project Members', |
| 31 | } |
| 32 | |
| 33 | def __init__(self, project_access_enum): |
| 34 | self.key = int(project_access_enum) |
| 35 | self.name = self.ACCESS_NAMES[project_access_enum] |
| 36 | |
| 37 | |
| 38 | class ProjectView(template_helpers.PBProxy): |
| 39 | """View object to make it easy to display a search result in EZT.""" |
| 40 | |
| 41 | _MAX_SUMMARY_CHARS = 70 |
| 42 | _LIMITED_DESCRIPTION_CHARS = 500 |
| 43 | |
| 44 | def __init__(self, pb, starred=False, now=None, num_stars=None, |
| 45 | membership_desc=None): |
| 46 | super(ProjectView, self).__init__(pb) |
| 47 | |
| 48 | self.limited_summary = template_helpers.FitUnsafeText( |
| 49 | pb.summary, self._MAX_SUMMARY_CHARS) |
| 50 | |
| 51 | self.limited_description = template_helpers.FitUnsafeText( |
| 52 | pb.description, self._LIMITED_DESCRIPTION_CHARS) |
| 53 | |
| 54 | self.state_name = str(pb.state) # Gives the enum name |
| 55 | self.relative_home_url = '/p/%s' % pb.project_name |
| 56 | |
| 57 | if now is None: |
| 58 | now = time.time() |
| 59 | |
| 60 | last_full_hour = now - (now % framework_constants.SECS_PER_HOUR) |
| 61 | self.cached_content_timestamp = max( |
| 62 | pb.cached_content_timestamp, last_full_hour) |
| 63 | self.last_updated_exists = ezt.boolean(pb.recent_activity) |
| 64 | course_grain, fine_grain = timestr.GetHumanScaleDate(pb.recent_activity) |
| 65 | if course_grain == 'Older': |
| 66 | self.recent_activity = fine_grain |
| 67 | else: |
| 68 | self.recent_activity = course_grain |
| 69 | |
| 70 | self.starred = ezt.boolean(starred) |
| 71 | |
| 72 | self.num_stars = num_stars |
| 73 | self.plural = '' if num_stars == 1 else 's' |
| 74 | self.membership_desc = membership_desc |
| 75 | |
| 76 | |
| 77 | class MemberView(object): |
| 78 | """EZT-view of details of how a person is participating in a project.""" |
| 79 | |
| 80 | def __init__( |
| 81 | self, logged_in_user_id, member_id, user_view, project, |
| 82 | project_commitments, effective_ids=None, ac_exclusion=False, |
| 83 | no_expand=False, is_group=False): |
| 84 | """Initialize a MemberView with the given information. |
| 85 | |
| 86 | Args: |
| 87 | logged_in_user_id: int user ID of the viewing user, or 0 for anon. |
| 88 | member_id: int user ID of the project member being viewed. |
| 89 | user_view: UserView object for this member. |
| 90 | project: Project PB for the currently viewed project. |
| 91 | project_commitments: ProjectCommitments PB for the currently viewed |
| 92 | project, or None if commitments are not to be displayed. |
| 93 | effective_ids: optional set of user IDs for this user, if supplied |
| 94 | we show the highest role that they have via any group membership. |
| 95 | ac_exclusion: True when this member should not be in autocomplete. |
| 96 | no_expand: True for user groups that should not expand when generating |
| 97 | autocomplete options. |
| 98 | is_group: True if this user is actually a user group. |
| 99 | """ |
| 100 | self.viewing_self = ezt.boolean(logged_in_user_id == member_id) |
| 101 | |
| 102 | self.user = user_view |
| 103 | member_qs_param = user_view.user_id |
| 104 | self.detail_url = '/p/%s%s?u=%s' % ( |
| 105 | project.project_name, urls.PEOPLE_DETAIL, member_qs_param) |
| 106 | self.role = framework_helpers.GetRoleName( |
| 107 | effective_ids or {member_id}, project) |
| 108 | self.extra_perms = permissions.GetExtraPerms(project, member_id) |
| 109 | self.notes = None |
| 110 | if project_commitments is not None: |
| 111 | for commitment in project_commitments.commitments: |
| 112 | if commitment.member_id == member_id: |
| 113 | self.notes = commitment.notes |
| 114 | break |
| 115 | |
| 116 | # Attributes needed by table_view_helpers.py |
| 117 | self.labels = [] |
| 118 | self.derived_labels = [] |
| 119 | |
| 120 | self.ac_include = ezt.boolean(not ac_exclusion) |
| 121 | self.ac_expand = ezt.boolean(not no_expand) |
| 122 | |
| 123 | self.is_group = ezt.boolean(is_group) |
| 124 | self.is_service_account = ezt.boolean(framework_helpers.IsServiceAccount( |
| 125 | self.user.email)) |