studio: show only recent changes of current user (#62953)

This commit is contained in:
Lauréline Guérin 2022-06-28 16:35:24 +02:00
parent 598ce40e3d
commit e56c295643
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
5 changed files with 26 additions and 7 deletions

View File

@ -127,6 +127,9 @@ def test_studio_home_recent_errors(pub):
def test_studio_home_recent_changes(pub):
create_superuser(pub)
user = create_superuser(pub)
other_user = pub.user_class(name='other')
other_user.store()
BlockDef.wipe()
CardDef.wipe()
@ -152,6 +155,20 @@ def test_studio_home_recent_changes(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

View File

@ -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

View File

@ -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)

View File

@ -3793,15 +3793,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()

View File

@ -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>