You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2026/09/02 16:05:17 UTC

(allura) branch master updated: optimize _get_site_notification in case of users with many projects

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git


The following commit(s) were added to refs/heads/master by this push:
     new e0f558625 optimize _get_site_notification in case of users with many projects
e0f558625 is described below

commit e0f558625020463a9ad2af7744bafdae9ee321ec
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Wed Sep 2 12:05:07 2026 -0400

    optimize _get_site_notification in case of users with many projects
---
 Allura/allura/lib/plugin.py        | 17 ++++++++++-------
 Allura/allura/tests/test_plugin.py | 39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 7 deletions(-)

diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 4c8e470ea..4c7cc389c 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -1673,14 +1673,8 @@ def _get_site_notification(self, url='', user=None, tool_name='', site_notificat
 
         active_notes = SiteNotification.actives()
         note_to_show = None
+        projects_by_role = {}  # my_projects_by_role_name is expensive, so keep a cache if we do use it
         for note in active_notes:
-            if note.user_role and (not user or user.is_anonymous()):
-                continue
-            if note.user_role:
-                projects = user.my_projects_by_role_name(note.user_role)
-                if len(projects) == 0 or len(projects) == 1 and projects[0].is_user_project:
-                    continue
-
             if note.page_regex and re.search(note.page_regex, url) is None:
                 continue
             if note.page_tool_type and tool_name.lower() != note.page_tool_type.lower():
@@ -1696,6 +1690,15 @@ def _get_site_notification(self, url='', user=None, tool_name='', site_notificat
             if closed or note.impressions > 0 and views > note.impressions:
                 continue
 
+            if note.user_role:
+                if not user or user.is_anonymous():
+                    continue
+                if note.user_role not in projects_by_role:
+                    projects_by_role[note.user_role] = user.my_projects_by_role_name(note.user_role)
+                projects = projects_by_role[note.user_role]
+                if len(projects) == 0 or len(projects) == 1 and projects[0].is_user_project:
+                    continue
+
             # this notification is ok to show, so this is the one.
             views += 1
             note_to_show = note
diff --git a/Allura/allura/tests/test_plugin.py b/Allura/allura/tests/test_plugin.py
index c12fc6806..cde5ba0ec 100644
--- a/Allura/allura/tests/test_plugin.py
+++ b/Allura/allura/tests/test_plugin.py
@@ -627,6 +627,45 @@ def test_get_site_notifications_with_api_cookie(self, SiteNotification):
         assert get_note[0] is note
         assert get_note[1] == 'testid-2-False'
 
+    @patch('allura.lib.plugin.c')
+    @patch('allura.model.notification.SiteNotification')
+    @patch('tg.response')
+    @patch('tg.request')
+    def test_get_site_notification_role_lookup_skipped_when_closed(self, request, response, SiteNotification, c):
+        note = MagicMock()
+        note._id = 'deadbeef'
+        note.user_role = 'Test'
+        note.page_regex = None
+        note.page_tool_type = None
+        note.impressions = 10
+        SiteNotification.actives.return_value = [note]
+        request.cookies = {'site-notification': 'deadbeef-1-true'}
+        c.user.is_anonymous.return_value = False
+
+        assert self.Provider().get_site_notification() is None
+        assert not c.user.my_projects_by_role_name.called
+
+    @patch('allura.lib.plugin.c')
+    @patch('allura.model.notification.SiteNotification')
+    @patch('tg.response', MagicMock())
+    @patch('tg.request', MagicMock())
+    def test_get_site_notification_role_lookup_done_once(self, SiteNotification, c):
+        notes = []
+        for i in range(2):
+            note = MagicMock()
+            note._id = f'deadbeef{i}'
+            note.user_role = 'Test'
+            note.page_regex = None
+            note.page_tool_type = None
+            note.impressions = 10
+            notes.append(note)
+        SiteNotification.actives.return_value = notes
+        c.user.is_anonymous.return_value = False
+        c.user.my_projects_by_role_name.return_value = []
+
+        assert self.Provider().get_site_notification() is None
+        assert c.user.my_projects_by_role_name.call_count == 1
+
 
 class TestLocalAuthenticationProvider: