blob: 8b17bbbb740610d544b751f8f3a102d3a24d3fb6 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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"""Classes to display hotlists in templates."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import ezt
12
13import logging
14
15from framework import framework_helpers
16from framework import permissions
17from framework import template_helpers
18
19
20class MemberView(object):
21 """EZT-view of details of how a person is participating in a project."""
22
23 def __init__(self, logged_in_user_id, member_id, user_view, hotlist,
24 effective_ids=None):
25 """Initialize a MemberView with the given information.
26
27 Args:
28 logged_in_user_id: int user ID of the viewing user, or 0 for anon.
29 member_id: int user ID of the hotlist member being viewed.
30 user_ivew: UserView object for this member
31 hotlist: Hotlist PB for the currently viewed hotlist
32 effective_ids: optional set of user IDs for this user, if supplied
33 we show the highest role that they have via any group membership.
34 """
35
36 self.viewing_self = ezt.boolean(logged_in_user_id == member_id)
37
38 self.user = user_view
39 member_qs_param = user_view.user_id
40 self.detail_url = '/u/%s/' % member_qs_param
41 self.role = framework_helpers.GetHotlistRoleName(
42 effective_ids or {member_id}, hotlist)
43
44
45class HotlistView(template_helpers.PBProxy):
46 """Wrapper class that makes it easier to display a hotlist via EZT."""
47
48 def __init__(
49 self, hotlist_pb, perms, user_auth=None,
50 viewed_user_id=None, users_by_id=None, is_starred=False):
51 super(HotlistView, self).__init__(hotlist_pb)
52
53 self.visible = permissions.CanViewHotlist(
54 user_auth.effective_ids, perms, hotlist_pb)
55
56 self.access_is_private = ezt.boolean(hotlist_pb.is_private)
57 if not hotlist_pb.owner_ids: # Should never happen.
58 logging.error('Unowned Hotlist: id:%r, name:%r',
59 hotlist_pb.hotlist_id,
60 hotlist_pb.name)
61 self.url = ''
62 return
63 owner_id = hotlist_pb.owner_ids[0] # only one owner allowed
64 owner = users_by_id[owner_id]
65 if owner.user.banned:
66 self.visible = False
67 if owner.obscure_email or not self.visible:
68 self.url = (
69 '/u/%d/hotlists/%s' % (owner_id, hotlist_pb.name))
70 else:
71 self.url = (
72 '/u/%s/hotlists/%s' % (
73 owner.email, hotlist_pb.name))
74
75 self.role_name = ''
76 if viewed_user_id in hotlist_pb.owner_ids:
77 self.role_name = 'owner'
78 elif any(effective_id in hotlist_pb.editor_ids for
79 effective_id in user_auth.effective_ids):
80 self.role_name = 'editor'
81
82 if users_by_id:
83 self.owners = [users_by_id[owner_id] for
84 owner_id in hotlist_pb.owner_ids]
85 self.editors = [users_by_id[editor_id] for
86 editor_id in hotlist_pb.editor_ids]
87 self.num_issues = len(hotlist_pb.items)
88 self.is_followed = ezt.boolean(user_auth.user_id in hotlist_pb.follower_ids)
89 # TODO(jojwang): if hotlist follower's will not be used, perhaps change
90 # from is_followed to is_member or just use is_starred
91 self.num_followers = len(hotlist_pb.follower_ids)
92 self.is_starred = ezt.boolean(is_starred)