From cbbc378e4a0d92b700b8c444cf061f7a31945738 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 3 Oct 2019 16:09:42 +0200
Subject: [PATCH 2/2] cvsdatasource: data is now a new-generation endpoint
 (#32121)

---
 passerelle/apps/csvdatasource/models.py            | 14 ++++++++++++++
 .../csvdatasource/csvdatasource_detail.html        |  7 ++++---
 passerelle/apps/csvdatasource/urls.py              |  4 ----
 tests/test_csv_datasource.py                       |  6 +++++-
 tests/test_import_export.py                        |  6 +++++-
 5 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/passerelle/apps/csvdatasource/models.py b/passerelle/apps/csvdatasource/models.py
index 397d40c8..d0f1454c 100644
--- a/passerelle/apps/csvdatasource/models.py
+++ b/passerelle/apps/csvdatasource/models.py
@@ -269,6 +269,20 @@ class CsvDataSource(BaseResource):
     def titles(self):
         return [smart_text(t.strip()) for t in self.columns_keynames.split(',')]
 
+    @endpoint(perm='can_access', methods=['get'], name='data')
+    def data(self, request, **kwargs):
+        params = request.GET
+        filters = []
+        for column_title in [t.strip() for t in self.columns_keynames.split(',') if t]:
+            if column_title in params.keys():
+                query_value = request.GET.get(column_title, '')
+                if 'case-insensitive' in params:
+                    filters.append("%s.lower() == %r" % (column_title, query_value.lower()))
+                else:
+                    filters.append("%s == %r" % (column_title, query_value))
+        query = Query(filters='\n'.join(filters))
+        return self.execute_query(request, query, **params.dict())
+
     @endpoint(perm='can_access', methods=['get'],
               name='query', pattern=r'^(?P<query_name>[\w-]+)/$')
     def select(self, request, query_name, **kwargs):
diff --git a/passerelle/apps/csvdatasource/templates/csvdatasource/csvdatasource_detail.html b/passerelle/apps/csvdatasource/templates/csvdatasource/csvdatasource_detail.html
index 620db8fd..9fdbfea9 100644
--- a/passerelle/apps/csvdatasource/templates/csvdatasource/csvdatasource_detail.html
+++ b/passerelle/apps/csvdatasource/templates/csvdatasource/csvdatasource_detail.html
@@ -13,16 +13,17 @@
 {% endblock %}
 
 {% block endpoints %}
+{% url 'generic-endpoint' connector='csvdatasource' slug=object.slug endpoint='data' as csvdatasource_data_url %}
 <ul>
   <li>{% trans "Returning all file lines: "%}
-    <a href="{% url "csvdatasource-data" slug=object.slug %}">{%  url "csvdatasource-data" slug=object.slug %}</a>
+    <a href="{{ csvdatasource_data_url }}">{{ csvdatasource_data_url }}</a>
   </li>
   <ul>
   <li>{% trans "Returning lines containing 'abc' in 'text' column: "%}
-    <a href="{% url "csvdatasource-data" slug=object.slug %}?q=abc">{%  url "csvdatasource-data" slug=object.slug %}?q=abc</a>
+    <a href="{{ csvdatasource_data_url }}?q=abc">{{ csvdatasource_data_url }}?q=abc</a>
   </li>
   <li>{% trans "Same filter, but case insensitive:" %}
-    <a href="{% url "csvdatasource-data" slug=object.slug %}?case-insensitive&q=abc">{%  url "csvdatasource-data" slug=object.slug %}?case-insensitive&q=abc</a>
+    <a href="{{ csvdatasource_data_url }}?case-insensitive&q=abc">{{ csvdatasource_data_url }}?case-insensitive&q=abc</a>
   </li>
   </ul>
   {% for query in object.query_set.all %}
diff --git a/passerelle/apps/csvdatasource/urls.py b/passerelle/apps/csvdatasource/urls.py
index 62cb6311..da90131a 100644
--- a/passerelle/apps/csvdatasource/urls.py
+++ b/passerelle/apps/csvdatasource/urls.py
@@ -18,10 +18,6 @@ from django.conf.urls import include, url
 
 from .views import *
 
-urlpatterns = [
-    url(r'^(?P<slug>[\w,-]+)/data$', CsvDataView.as_view(), name='csvdatasource-data'),
-]
-
 management_urlpatterns = [
     url(r'^(?P<connector_slug>[\w,-]+)/download/$',
         CsvDownload.as_view(), name='csv-download'),
diff --git a/tests/test_csv_datasource.py b/tests/test_csv_datasource.py
index 78911758..a68ebe03 100644
--- a/tests/test_csv_datasource.py
+++ b/tests/test_csv_datasource.py
@@ -92,7 +92,11 @@ def setup():
             apiuser=api,
             resource_type=obj_type,
             resource_pk=csv.pk)
-        url = reverse('csvdatasource-data', kwargs={'slug': csv.slug})
+        url = reverse('generic-endpoint', kwargs={
+            'connector': 'csvdatasource',
+            'slug': csv.slug,
+            'endpoint': 'data',
+        })
         return csv, url
 
     return maker
diff --git a/tests/test_import_export.py b/tests/test_import_export.py
index 217cd930..69cf00d3 100644
--- a/tests/test_import_export.py
+++ b/tests/test_import_export.py
@@ -64,7 +64,11 @@ def setup():
         obj_type = ContentType.objects.get_for_model(csv)
         AccessRight.objects.create(codename='can_access', apiuser=api, resource_type=obj_type,
                                    resource_pk=csv.pk)
-        url = reverse('csvdatasource-data', kwargs={'slug': csv.slug})
+        url = reverse('generic-endpoint', kwargs={
+            'connector': 'csvdatasource',
+            'slug': csv.slug,
+            'endpoint': 'data',
+        })
         return csv, url
 
     return maker
-- 
2.20.1

