Exemplo n.º 1
0
  def testGetStarredProjects_MembersOnly(self):
    # Both users were able to star the project in the past.  The stars do not
    # go away even if access to the project changes.
    self.services.project_star.SetStar(
        self.cnxn, self.members_only_project.project_id, REGULAR_USER_ID, True)
    self.services.project_star.SetStar(
        self.cnxn, self.members_only_project.project_id, OTHER_USER_ID, True)

    # But now, only one of them is currently a member, so only regular_user
    # can see the starred project in the lists.
    self.assertProjectsAnyOrder(
        sitewide_helpers.GetViewableStarredProjects(
            self.cnxn, self.services, REGULAR_USER_ID, {REGULAR_USER_ID},
            self.regular_user),
        self.members_only_project)
    self.assertProjectsAnyOrder(
        sitewide_helpers.GetViewableStarredProjects(
            self.cnxn, self.services, OTHER_USER_ID, {REGULAR_USER_ID},
            self.regular_user),
        self.members_only_project)

    # The other user cannot see the project, so he does not see it in either
    # list of starred projects.
    self.assertProjectsAnyOrder(
        sitewide_helpers.GetViewableStarredProjects(
            self.cnxn, self.services, REGULAR_USER_ID, {OTHER_USER_ID},
            self.other_user))  # No expected projects listed.
    self.assertProjectsAnyOrder(
        sitewide_helpers.GetViewableStarredProjects(
            self.cnxn, self.services, OTHER_USER_ID, {OTHER_USER_ID},
            self.other_user))  # No expected projects listed.
Exemplo n.º 2
0
  def testUserUpdatesProjects(self):
    uup = userupdates.UserUpdatesProjects(None, None, self.services)

    self.mox.StubOutWithMock(sitewide_helpers, 'GetViewableStarredProjects')
    sitewide_helpers.GetViewableStarredProjects(
        self.mr.cnxn, self.services, self.mr.viewed_user_auth.user_id,
        self.mr.auth.effective_ids, self.mr.auth.user_pb).AndReturn(
            [self.project])

    self.mox.StubOutWithMock(activities, 'GatherUpdatesData')
    activities.GatherUpdatesData(
        self.services, self.mr, mox.IgnoreArg(),
        user_ids=None,
        project_ids=[self.project_id],
        ending=uup._ENDING,
        updates_page_url=uup._UPDATES_PAGE_URL,
        highlight=uup._HIGHLIGHT).AndReturn({})

    self.mox.ReplayAll()

    page_data = uup.GatherPageData(self.mr)
    self.mox.VerifyAll()
    self.assertEqual(3, len(page_data))
    self.assertEqual('st5', page_data['user_tab_mode'])
    self.assertEqual('yes', page_data['viewing_user_page'])
    self.assertEqual(uup._TAB_MODE, page_data['user_updates_tab_mode'])
    def testGetStarredProjects_OnlyViewableLiveStarred(self):
        viewed_user_id = 123
        for p in self.services.project.test_projects.values():
            self.services.project_star.SetStar(self.cnxn, p.project_id,
                                               viewed_user_id, True)

        self.assertProjectsAnyOrder(
            sitewide_helpers.GetViewableStarredProjects(
                self.cnxn, self.services, viewed_user_id, {REGULAR_USER_ID},
                self.regular_user), self.regular_owner_live,
            self.regular_committer_live, self.other_owner_live,
            self.other_committer_live, self.members_only_project)
Exemplo n.º 4
0
  def testGetStarredProjects_OnlyViewableLiveStarred(self):
    viewed_user_id = 123
    for p in self.services.project.test_projects.values():
      # We go straight to the services layer because this is a test set up
      # rather than an actual user request.
      self.services.project_star.SetStar(
          self.cnxn, p.project_id, viewed_user_id, True)

    self.assertProjectsAnyOrder(
        sitewide_helpers.GetViewableStarredProjects(
            self.cnxn, self.services, viewed_user_id,
            {REGULAR_USER_ID}, self.regular_user),
        self.regular_owner_live, self.regular_committer_live,
        self.other_owner_live, self.other_committer_live,
        self.members_only_project)
Exemplo n.º 5
0
    def _GatherProjects(self, mr):
        """Return a dict of project names the current user is involved in."""
        with self.profiler.Phase('GetUserProjects'):
            project_lists = sitewide_helpers.GetUserProjects(
                mr.cnxn, self.services, mr.auth.user_pb, mr.auth.effective_ids,
                mr.auth.effective_ids)
            (visible_ownership, _visible_deleted, visible_membership,
             visible_contrib) = project_lists

        with self.profiler.Phase('GetStarredProjects'):
            starred_projects = sitewide_helpers.GetViewableStarredProjects(
                mr.cnxn, self.services, mr.auth.user_id, mr.auth.effective_ids,
                mr.auth.user_pb)

        projects_dict = {
            'memberof': [p.project_name for p in visible_membership],
            'ownerof': [p.project_name for p in visible_ownership],
            'contributorto': [p.project_name for p in visible_contrib],
            'starred_projects': [p.project_name for p in starred_projects],
        }

        return projects_dict
Exemplo n.º 6
0
 def _GetProjectIDsForUpdates(self, mr):
     """Returns a list of project IDs whom to retrieve activities from."""
     starred_projects = sitewide_helpers.GetViewableStarredProjects(
         mr.cnxn, self.services, mr.viewed_user_auth.user_id,
         mr.auth.effective_ids, mr.auth.user_pb)
     return [project.project_id for project in starred_projects]
Exemplo n.º 7
0
    def GatherPageData(self, mr):
        """Build up a dictionary of data values to use when rendering the page.

    Args:
      mr: commonly used info parsed from the request.

    Returns:
      Dict of values used by EZT for rendering the page.
    """
        can_create_project = permissions.CanCreateProject(mr.perms)

        # Kick off the search pipeline, it has its own promises for parallelism.
        pipeline = projectsearch.ProjectSearchPipeline(mr, self.services,
                                                       self.profiler)

        # Meanwhile, determine which projects the signed-in user has starred.
        starred_project_ids = set()
        # A dict of project id to the user's membership status.
        project_memberships = {}
        if mr.auth.user_id:
            starred_projects = sitewide_helpers.GetViewableStarredProjects(
                mr.cnxn, self.services, mr.auth.user_id, mr.auth.effective_ids,
                mr.auth.user_pb)
            starred_project_ids = {p.project_id for p in starred_projects}

            owned, _archive_owned, member_of, contrib_of = (
                sitewide_helpers.GetUserProjects(mr.cnxn, self.services,
                                                 mr.auth.user_pb,
                                                 mr.auth.effective_ids,
                                                 mr.auth.effective_ids))
            project_memberships.update(
                {proj.project_id: 'Owner'
                 for proj in owned})
            project_memberships.update(
                {proj.project_id: 'Member'
                 for proj in member_of})
            project_memberships.update(
                {proj.project_id: 'Contributor'
                 for proj in contrib_of})

        # Finish the project search pipeline.
        pipeline.SearchForIDs()
        pipeline.GetProjectsAndPaginate(mr.cnxn, urls.HOSTING_HOME)
        project_ids = [p.project_id for p in pipeline.visible_results]
        star_count_dict = self.services.project_star.CountItemsStars(
            mr.cnxn, project_ids)

        # Make ProjectView objects
        project_view_list = [
            project_views.ProjectView(
                p,
                starred=p.project_id in starred_project_ids,
                num_stars=star_count_dict.get(p.project_id),
                membership_desc=project_memberships.get(p.project_id))
            for p in pipeline.visible_results
        ]
        return {
            'can_create_project': ezt.boolean(can_create_project),
            'learn_more_link': settings.learn_more_link,
            'projects': project_view_list,
            'pagination': pipeline.pagination,
        }
Exemplo n.º 8
0
    def GatherPageData(self, mr):
        """Build up a dictionary of data values to use when rendering the page."""
        viewed_user = mr.viewed_user_auth.user_pb
        if self.services.usergroup.GetGroupSettings(
                mr.cnxn, mr.viewed_user_auth.user_id):
            url = framework_helpers.FormatAbsoluteURL(mr,
                                                      '/g/%s/' %
                                                      viewed_user.email,
                                                      include_project=False)
            self.redirect(url, abort=True)  # Show group page instead.

        with self.profiler.Phase('GetUserProjects'):
            project_lists = sitewide_helpers.GetUserProjects(
                mr.cnxn, self.services, mr.auth.user_pb, mr.auth.effective_ids,
                mr.viewed_user_auth.effective_ids)

            (visible_ownership, visible_archived, visible_membership,
             visible_contrib) = project_lists

        viewed_user_display_name = framework_views.GetViewedUserDisplayName(mr)

        with self.profiler.Phase('GetStarredProjects'):
            starred_projects = sitewide_helpers.GetViewableStarredProjects(
                mr.cnxn, self.services, mr.viewed_user_auth.user_id,
                mr.auth.effective_ids, mr.auth.user_pb)

        logged_in_starred_pids = []
        if mr.auth.user_id:
            logged_in_starred_pids = self.services.project_star.LookupStarredItemIDs(
                mr.cnxn, mr.auth.user_id)

        starred_user_ids = self.services.user_star.LookupStarredItemIDs(
            mr.cnxn, mr.viewed_user_auth.user_id)
        starred_user_dict = framework_views.MakeAllUserViews(
            mr.cnxn, self.services.user, starred_user_ids)
        starred_users = starred_user_dict.values()

        is_user_starred = self._IsUserStarred(mr.cnxn, mr.auth.user_id,
                                              mr.viewed_user_auth.user_id)

        if viewed_user.last_visit_timestamp:
            last_visit_str = timestr.FormatRelativeDate(
                viewed_user.last_visit_timestamp, days_only=True)
            last_visit_str = last_visit_str or 'Less than 2 days ago'
        else:
            last_visit_str = 'Never'

        if viewed_user.email_bounce_timestamp:
            last_bounce_str = timestr.FormatRelativeDate(
                viewed_user.email_bounce_timestamp, days_only=True)
            last_bounce_str = last_bounce_str or 'Less than 2 days ago'
        else:
            last_bounce_str = None

        can_ban = permissions.CanBan(mr, self.services)
        viewed_user_is_spammer = viewed_user.banned.lower() == 'spam'
        viewed_user_may_be_spammer = not viewed_user_is_spammer
        all_projects = self.services.project.GetAllProjects(mr.cnxn)
        for project_id in all_projects:
            project = all_projects[project_id]
            viewed_user_perms = permissions.GetPermissions(
                viewed_user, mr.viewed_user_auth.effective_ids, project)
            if (viewed_user_perms != permissions.EMPTY_PERMISSIONSET
                    and viewed_user_perms != permissions.USER_PERMISSIONSET):
                viewed_user_may_be_spammer = False

        ban_token = None
        ban_spammer_token = None
        if mr.auth.user_id and can_ban:
            form_token_path = mr.request.path + 'ban.do'
            ban_token = xsrf.GenerateToken(mr.auth.user_id, form_token_path)
            form_token_path = mr.request.path + 'banSpammer.do'
            ban_spammer_token = xsrf.GenerateToken(mr.auth.user_id,
                                                   form_token_path)

        page_data = {
            'user_tab_mode':
            'st2',
            'viewed_user_display_name':
            viewed_user_display_name,
            'viewed_user_may_be_spammer':
            ezt.boolean(viewed_user_may_be_spammer),
            'viewed_user_is_spammer':
            ezt.boolean(viewed_user_is_spammer),
            'viewed_user_is_banned':
            ezt.boolean(viewed_user.banned),
            'viewed_user_ignore_action_limits':
            (ezt.boolean(viewed_user.ignore_action_limits)),
            'owner_of_projects': [
                project_views.ProjectView(p,
                                          starred=p.project_id
                                          in logged_in_starred_pids)
                for p in visible_ownership
            ],
            'committer_of_projects': [
                project_views.ProjectView(p,
                                          starred=p.project_id
                                          in logged_in_starred_pids)
                for p in visible_membership
            ],
            'contributor_to_projects': [
                project_views.ProjectView(p,
                                          starred=p.project_id
                                          in logged_in_starred_pids)
                for p in visible_contrib
            ],
            'owner_of_archived_projects':
            [project_views.ProjectView(p) for p in visible_archived],
            'starred_projects': [
                project_views.ProjectView(p,
                                          starred=p.project_id
                                          in logged_in_starred_pids)
                for p in starred_projects
            ],
            'starred_users':
            starred_users,
            'is_user_starred':
            ezt.boolean(is_user_starred),
            'viewing_user_page':
            ezt.boolean(True),
            'last_visit_str':
            last_visit_str,
            'last_bounce_str':
            last_bounce_str,
            'vacation_message':
            viewed_user.vacation_message,
            'can_ban':
            ezt.boolean(can_ban),
            'ban_token':
            ban_token,
            'ban_spammer_token':
            ban_spammer_token
        }

        settings = framework_helpers.UserSettings.GatherUnifiedSettingsPageData(
            mr.auth.user_id, mr.viewed_user_auth.user_view, viewed_user)
        page_data.update(settings)

        return page_data