From 320a466989707ba64c3bd9dfd21b9dcb401aa547 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 28 Jun 2022 16:35:24 +0200
Subject: [PATCH 1/2] studio: show only recent changes of current user (#62953)

---
 tests/admin_pages/test_studio.py         | 18 +++++++++++++++++-
 wcs/backoffice/studio.py                 |  5 +++--
 wcs/snapshots.py                         |  4 ++--
 wcs/sql.py                               |  5 +++--
 wcs/templates/wcs/backoffice/studio.html |  2 +-
 5 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/tests/admin_pages/test_studio.py b/tests/admin_pages/test_studio.py
index 14c2e8b74..f6db5de75 100644
--- a/tests/admin_pages/test_studio.py
+++ b/tests/admin_pages/test_studio.py
@@ -137,7 +137,9 @@ def test_studio_home_recent_errors(sql_pub):
 
 def test_studio_home_recent_changes(sql_pub):
     pub = sql_pub
-    create_superuser(pub)
+    user = create_superuser(pub)
+    other_user = pub.user_class(name='other')
+    other_user.store()
 
     BlockDef.wipe()
     CardDef.wipe()
@@ -163,6 +165,20 @@ def test_studio_home_recent_changes(sql_pub):
 
     app = login(get_app(pub))
     resp = app.get('/backoffice/studio/')
+    assert len(resp.pyquery.find('ul.recent-changes li')) == 0
+
+    for snapshot in pub.snapshot_class.select():
+        snapshot.user_id = other_user.id
+        snapshot.store()
+    resp = app.get('/backoffice/studio/')
+    assert len(resp.pyquery.find('ul.recent-changes li')) == 0
+
+    for snapshot in pub.snapshot_class.select():
+        snapshot.user_id = user.id
+        snapshot.store()
+    resp = app.get('/backoffice/studio/')
+    assert len(resp.pyquery.find('ul.recent-changes li')) == 5
+
     # too old
     for i in range(5):
         assert 'backoffice/forms/blocks/%s/' % objects[BlockDef.xml_root_node][i].id not in resp
diff --git a/wcs/backoffice/studio.py b/wcs/backoffice/studio.py
index 2b144a0ce..913b04f3c 100644
--- a/wcs/backoffice/studio.py
+++ b/wcs/backoffice/studio.py
@@ -14,7 +14,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, see <http://www.gnu.org/licenses/>.
 
-from quixote import get_publisher
+from quixote import get_publisher, get_request
 from quixote.directory import Directory
 
 from wcs.admin.logged_errors import LoggedErrorsDirectory
@@ -78,7 +78,8 @@ class StudioDirectory(Directory):
         }
         if get_publisher().snapshot_class:
             context['recent_objects'] = get_publisher().snapshot_class.get_recent_changes(
-                object_types=[ot.xml_root_node for ot in object_types]
+                object_types=[ot.xml_root_node for ot in object_types],
+                user=get_request().user,
             )
         return template.QommonTemplateResponse(
             templates=['wcs/backoffice/studio.html'], context=context, is_django_native=True
diff --git a/wcs/snapshots.py b/wcs/snapshots.py
index f4a9e3081..0f1bd599b 100644
--- a/wcs/snapshots.py
+++ b/wcs/snapshots.py
@@ -184,8 +184,8 @@ class Snapshot:
             obj.store()
 
     @classmethod
-    def get_recent_changes(cls, object_types):
-        elements = cls._get_recent_changes(object_types)
+    def get_recent_changes(cls, object_types, user):
+        elements = cls._get_recent_changes(object_types, user)
         instances = []
         for object_type, object_id, snapshot_timestamp in elements:
             klass = cls.get_class(object_type)
diff --git a/wcs/sql.py b/wcs/sql.py
index 1510f728e..4e036331e 100644
--- a/wcs/sql.py
+++ b/wcs/sql.py
@@ -3788,15 +3788,16 @@ class Snapshot(SqlMixin, wcs.snapshots.Snapshot):
         return cls.get(row[0])
 
     @classmethod
-    def _get_recent_changes(cls, object_types):
+    def _get_recent_changes(cls, object_types, user):
         conn, cur = get_connection_and_cursor()
         sql_statement = '''SELECT object_type, object_id, MAX(timestamp) AS m
                            FROM snapshots
                            WHERE object_type IN %(object_types)s
+                           AND user_id = %(user_id)s
                            GROUP BY object_type, object_id
                            ORDER BY m DESC
                            LIMIT 5'''
-        parameters = {'object_types': tuple(object_types)}
+        parameters = {'object_types': tuple(object_types), 'user_id': str(user.id)}
         cur.execute(sql_statement, parameters)
         result = cur.fetchall()
         conn.commit()
diff --git a/wcs/templates/wcs/backoffice/studio.html b/wcs/templates/wcs/backoffice/studio.html
index f0d117a48..ce54e2e4b 100644
--- a/wcs/templates/wcs/backoffice/studio.html
+++ b/wcs/templates/wcs/backoffice/studio.html
@@ -34,7 +34,7 @@
 
     <div class="paragraph">
       <h3>{% trans "Recent changes" context "studio" %}</h3>
-      <ul>
+      <ul class="recent-changes">
         {% for obj in recent_objects %}
         <li><a href="{{ obj.get_admin_url }}">{{ obj.name }} ({{ obj.verbose_name }})</a>
                 <span class="timestamp">{{ obj.snapshot_timestamp }}</span></li>
-- 
2.35.1

