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