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