Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """A class to display the hosting home page.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import logging |
| 11 | import ezt |
| 12 | |
| 13 | import settings |
| 14 | from businesslogic import work_env |
| 15 | from framework import exceptions |
| 16 | from framework import permissions |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 17 | from framework import servlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 18 | from framework import urls |
| 19 | from project import project_views |
| 20 | from sitewide import projectsearch |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 21 | |
| 22 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 23 | class HostingHome(servlet.Servlet): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 24 | """HostingHome shows the project list and link to create a project.""" |
| 25 | |
| 26 | _PAGE_TEMPLATE = 'sitewide/hosting-home-page.ezt' |
| 27 | |
| 28 | def GatherPageData(self, mr): |
| 29 | """Build up a dictionary of data values to use when rendering the page. |
| 30 | |
| 31 | Args: |
| 32 | mr: commonly used info parsed from the request. |
| 33 | |
| 34 | Returns: |
| 35 | Dict of values used by EZT for rendering the page. |
| 36 | """ |
| 37 | redirect_msg = self._MaybeRedirectToDomainDefaultProject(mr) |
| 38 | logging.info(redirect_msg) |
| 39 | |
| 40 | can_create_project = permissions.CanCreateProject(mr.perms) |
| 41 | |
| 42 | # Kick off the search pipeline, it has its own promises for parallelism. |
| 43 | pipeline = projectsearch.ProjectSearchPipeline(mr, self.services) |
| 44 | |
| 45 | # Meanwhile, determine which projects the signed-in user has starred. |
| 46 | with work_env.WorkEnv(mr, self.services) as we: |
| 47 | starred_projects = we.ListStarredProjects() |
| 48 | starred_project_ids = {p.project_id for p in starred_projects} |
| 49 | |
| 50 | # A dict of project id to the user's membership status. |
| 51 | project_memberships = {} |
| 52 | if mr.auth.user_id: |
| 53 | with work_env.WorkEnv(mr, self.services) as we: |
| 54 | owned, _archive_owned, member_of, contrib_of = ( |
| 55 | we.GetUserProjects(mr.auth.effective_ids)) |
| 56 | project_memberships.update({proj.project_id: 'Owner' for proj in owned}) |
| 57 | project_memberships.update( |
| 58 | {proj.project_id: 'Member' for proj in member_of}) |
| 59 | project_memberships.update( |
| 60 | {proj.project_id: 'Contributor' for proj in contrib_of}) |
| 61 | |
| 62 | # Finish the project search pipeline. |
| 63 | pipeline.SearchForIDs(domain=mr.request.host) |
| 64 | pipeline.GetProjectsAndPaginate(mr.cnxn, urls.HOSTING_HOME) |
| 65 | project_ids = [p.project_id for p in pipeline.visible_results] |
| 66 | star_count_dict = self.services.project_star.CountItemsStars( |
| 67 | mr.cnxn, project_ids) |
| 68 | |
| 69 | # Make ProjectView objects |
| 70 | project_view_list = [ |
| 71 | project_views.ProjectView( |
| 72 | p, starred=p.project_id in starred_project_ids, |
| 73 | num_stars=star_count_dict.get(p.project_id), |
| 74 | membership_desc=project_memberships.get(p.project_id)) |
| 75 | for p in pipeline.visible_results] |
| 76 | return { |
| 77 | 'can_create_project': ezt.boolean(can_create_project), |
| 78 | 'learn_more_link': settings.learn_more_link, |
| 79 | 'projects': project_view_list, |
| 80 | 'pagination': pipeline.pagination, |
| 81 | } |
| 82 | |
| 83 | def _MaybeRedirectToDomainDefaultProject(self, mr): |
| 84 | """If there is a relevant default project, redirect to it.""" |
| 85 | project_name = settings.domain_to_default_project.get(mr.request.host) |
| 86 | if not project_name: |
| 87 | return 'No configured default project redirect for this domain.' |
| 88 | |
| 89 | project = None |
| 90 | try: |
| 91 | project = self.services.project.GetProjectByName(mr.cnxn, project_name) |
| 92 | except exceptions.NoSuchProjectException: |
| 93 | pass |
| 94 | |
| 95 | if not project: |
| 96 | return 'Domain default project %s not found' % project_name |
| 97 | |
| 98 | if not permissions.UserCanViewProject( |
| 99 | mr.auth.user_pb, mr.auth.effective_ids, project): |
| 100 | return 'User cannot view default project: %r' % project |
| 101 | |
| 102 | project_url = '/p/%s' % project_name |
| 103 | self.redirect(project_url, abort=True) |
| 104 | return 'Redirected to %r' % project_url |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 105 | |
| 106 | def GetOldHostingHome(self, **kwargs): |
| 107 | return self.handler(**kwargs) |