From 17f8b3ffbcb9609ce68b4842ba06f6adb635c59d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sat, 7 May 2022 15:38:03 +0200
Subject: [PATCH] misc: keep logs tab open when using pagination (#64834)

---
 .../includes/resource-logs-table.html         |  4 +-
 .../passerelle/manage/service_view.html       |  8 ++--
 passerelle/views.py                           |  3 ++
 tests/test_manager.py                         | 45 +++++++++++++++++++
 4 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/passerelle/templates/passerelle/includes/resource-logs-table.html b/passerelle/templates/passerelle/includes/resource-logs-table.html
index e149dc87..b6cae60c 100644
--- a/passerelle/templates/passerelle/includes/resource-logs-table.html
+++ b/passerelle/templates/passerelle/includes/resource-logs-table.html
@@ -1,7 +1,7 @@
 {% load i18n passerelle tz gadjo %}
 
 {% if logrecords %}
-<table class="main" data-log-base-url="{% url 'view-logs-connector' connector=object.get_connector_slug slug=object.slug %}" >
+<table class="main" id="table-logs" data-log-base-url="{% url 'view-logs-connector' connector=object.get_connector_slug slug=object.slug %}" >
     <thead>
         <th>{% trans 'Timestamp' %}</th>
         <th>{% trans 'Source IP' %}</th>
@@ -21,7 +21,7 @@
     </tbody>
 </table>
 
-{% with page_obj=logrecords anchor="#logs" without_key="log_id" page_key=page_key|default:"page" %}
+{% with page_obj=logrecords anchor="#table-logs" without_key="log_id" page_key=page_key|default:"page" %}
   {% if page_obj.has_next or page_obj.has_previous %}
   {% spaceless %}
   <p class="paginator">
diff --git a/passerelle/templates/passerelle/manage/service_view.html b/passerelle/templates/passerelle/manage/service_view.html
index b3965f2d..fc10f522 100644
--- a/passerelle/templates/passerelle/manage/service_view.html
+++ b/passerelle/templates/passerelle/manage/service_view.html
@@ -61,7 +61,7 @@
 
 <div class="pk-tabs connector-tabs">
  <div class="pk-tabs--tab-list" role="tablist">
-  <button role="tab" aria-selected="true" aria-controls="panel-endpoints" id="tab-endpoints" tabindex="0">{% trans "Endpoints" %}</button>
+  <button role="tab" {% if default_tab_page == 'endpoints' %}aria-selected="true" tabindex="0"{% else %}aria-selected="false" tabindex="-1"{% endif %}aria-controls="panel-endpoints" id="tab-endpoints">{% trans "Endpoints" %}</button>
   {% if object|can_edit:request.user and object.queries is not None %}
   <button role="tab" aria-selected="false" aria-controls="panel-queries" id="tab-queries" tabindex="-1">{% trans "Queries" %}</button>
   {% endif %}
@@ -70,7 +70,7 @@
   <button role="tab" aria-selected="false" aria-controls="panel-security" id="tab-security" tabindex="-1">{% trans "Security" %}</button>
   {% endif %}
   {% if perms.base.see_resourcelog %}
-  <button role="tab" aria-selected="false" aria-controls="panel-logs" id="tab-logs" tabindex="-1">{% trans "Logs" %}</button>
+  <button role="tab" {% if default_tab_page == 'logs' %}aria-selected="true" tabindex="0"{% else %}aria-selected="false" tabindex="-1"{% endif %} aria-controls="panel-logs" id="tab-logs">{% trans "Logs" %}</button>
   {% endif %}
   {% if perms.base.view_job and object.jobs_set.all %}
   <button role="tab" aria-selected="false" aria-controls="panel-jobs" id="tab-jobs" tabindex="-1">{% trans "Jobs" %}</button>
@@ -78,7 +78,7 @@
  </div>
  <div class="pk-tabs--container">
 
-<div id="panel-endpoints" role="tabpanel" tabindex="0" aria-labelledby="tab-endpoints">
+<div id="panel-endpoints" role="tabpanel" tabindex="0" aria-labelledby="tab-endpoints" {% if default_tab_page != 'endpoints' %}hidden{% endif %}>
   {% if object.documentation_url %}
   <div class="pk-information documentation"><a href="{{ object.documentation_url }}">{% trans "Documentation" %}</a></div>
   {% endif %}
@@ -145,7 +145,7 @@
 {% endif %}
 
 {% if perms.base.see_resourcelog %}
-<div id="panel-logs" role="tabpanel" tabindex="-1" aria-labelledby="tab-logs" hidden>
+<div id="panel-logs" role="tabpanel" tabindex="-1" aria-labelledby="tab-logs" {% if not default_tab_page == 'logs' %}hidden{% endif %}>
   <div>
   {% block logs %}
     {% resource_logs_table resource=object %}
diff --git a/passerelle/views.py b/passerelle/views.py
index e781d729..4d4fd3f1 100644
--- a/passerelle/views.py
+++ b/passerelle/views.py
@@ -178,6 +178,9 @@ class GenericConnectorView(GenericConnectorMixin, DetailView):
     def get_context_data(self, slug=None, **kwargs):
         context = super().get_context_data(**kwargs)
         context['has_check_status'] = not hasattr(context['object'].check_status, 'not_implemented')
+        context['default_tab_page'] = 'endpoints'
+        if 'page' in self.request.GET:
+            context['default_tab_page'] = 'logs'
         return context
 
     def get_template_names(self):
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 27af26da..7020c8b9 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -253,6 +253,51 @@ def test_logs(app, admin_user):
     assert 'title="Level 42 - Oct. 6, 2020 14:08:12"' in resp.text
 
 
+def test_logs_tab_pagination(app, admin_user):
+    data = StringIO('1;Foo\n2;Bar\n3;Baz')
+    csv = CsvDataSource.objects.create(
+        csv_file=File(data, 't.csv'),
+        columns_keynames='id, text',
+        slug='test',
+        title='a title',
+        description='a description',
+    )
+
+    query = Query(slug='fooba', resource=csv, structure='array')
+    query.projections = '\n'.join(['id:int(id)', 'text:text'])
+    query.save()
+
+    api = ApiUser.objects.create(
+        username='public', fullname='public', description='access for all', keytype='', key=''
+    )
+    obj_type = ContentType.objects.get_for_model(csv)
+    AccessRight.objects.create(
+        codename='can_access',
+        apiuser=api,
+        resource_type=obj_type,
+        resource_pk=csv.pk,
+    )
+
+    app = login(app)
+    resp = app.get(csv.get_absolute_url())
+    assert '<p>No records found</p>' in resp.text
+
+    for i in range(20):
+        app.get('/csvdatasource/test/query/foobar/?q=toto%s' % i)
+
+    resp = app.get(csv.get_absolute_url())
+    assert resp.pyquery('#panel-logs[hidden]')
+    assert resp.pyquery('#panel-endpoints:not([hidden])')
+    assert resp.pyquery('#table-logs tr').length == 10
+    assert resp.pyquery('#panel-logs .paginator a:first').text() == '2'
+    resp = resp.click('2')
+    assert resp.pyquery('#panel-logs:not([hidden])')
+    assert resp.pyquery('#panel-endpoints[hidden]')
+    assert resp.pyquery('#table-logs tr').length == 10
+    resp.pyquery('#panel-logs .paginator a:first').text()
+    assert resp.pyquery('#panel-logs .paginator a:first').text() == '1'
+
+
 def test_logs_search(app, admin_user):
     csv = CsvDataSource.objects.create(csv_file=File(StringIO('1;t\n'), 't.csv'), slug='t', title='t')
     app = login(app)
-- 
2.36.0

