Skip to content

Commit

Permalink
Add 'autofilter' parameter management for /vector/api/data (#961)
Browse files Browse the repository at this point in the history
* Add 'autofilter' parameter management for /vector/api/data

* Add logging error.

* ⬆️ Client
  g3w-suite/g3w-client#676

* Parameter comparison using str

* Add tests for autofilter

* ⬆️ Client
  g3w-suite/g3w-client#676

* ⬆️ Client
  g3w-suite/g3w-client@3b27599

* ⬆️ Client
  g3w-suite/g3w-client@8ce29b6

---------

Co-authored-by: wlorenzetti <[email protected]>
Co-authored-by: volterra79 <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent b395998 commit 11f2dff
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 8 deletions.
2 changes: 1 addition & 1 deletion g3w-admin/client/static/client/css/app.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion g3w-admin/client/static/client/js/vendor.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion g3w-admin/client/templates/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<link rel="stylesheet" type="text/css" href="{{ css }}" />
{% endfor %}{% endif %}
</head>
<body class="{% if sidebar_collapse %} sidebar-collapse {% endif %}">
<body class="skin-{{ skin_class|default:'blue' }} {% if sidebar_collapse %} sidebar-collapse {% endif %}">
{% if SETTINGS.SENTRY_JS %}
{{ SETTINGS.SENTRY_JS | safe }}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion g3w-admin/core/api/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def get_response(self, request, mode_call=None, project_type=None, layer_id=None
# before to send response
extra_data = before_return_vector_data_layer.send(self)
for ed in extra_data:
if ed[1] and ed[0].__name__ in ('add_constraints', 'add_atomic_capabilities'):
if ed[1] and ed[0].__name__ in ('add_constraints', 'add_atomic_capabilities', 'add_filter_token'):
self.results.results.update(ed[1])

# response a APIVectorLayer
Expand Down
71 changes: 67 additions & 4 deletions g3w-admin/qdjango/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@
load_layer_actions,
pre_delete_project,
pre_update_project,
before_return_vector_data_layer
)
from django.conf import settings
from django.urls import (
reverse, resolve
)
from django.http import HttpRequest
from django.contrib.auth.signals import user_logged_out
from django.db.models.signals import post_delete, post_save, pre_delete, pre_save
from django.db.models.signals import (
post_delete,
post_save,
pre_delete,
pre_save
)
from django.dispatch import receiver
from django.template import loader
from qgis.core import QgsProject
Expand All @@ -27,8 +37,15 @@
Message
)
from .searches import ProjectSearch
from .signals import post_save_qdjango_project_file
from .views import QdjangoProjectListView, QdjangoProjectUpdateView
from .views import (
QdjangoProjectListView,
QdjangoProjectUpdateView
)
from .vector import (
LayerVectorView,
MODE_DATA
)
import json

logger = logging.getLogger("django.request")

Expand Down Expand Up @@ -318,4 +335,50 @@ def invalid_prj_cache_by_message(**kwargs):
logging.getLogger("g3wadmin.debug").debug(
f"Parent qdjango project /api/config invalidate on create/update/delete of a project messages: "
f"{kwargs['instance'].project}"
)
)


@receiver(before_return_vector_data_layer)
def add_filter_token(**kwargs):
"""
Add a 'filtertoken' to response if 'autofilter' is set to 1
:return: A dict with autofilter token
:rtype: dict, None
"""
# check if is instance of layerVectorView
if (
isinstance(kwargs["sender"], LayerVectorView)
and kwargs["sender"].mode_call == MODE_DATA
and 'autofilter' in kwargs["sender"].request_data
and str(kwargs["sender"].request_data['autofilter']) == '1'
):
layer = kwargs["sender"].layer

try:
# Get fids
results = kwargs["sender"].results.results
if results['result']:
fids = [f["id"] for f in results['vector']['data']['features']]

if fids:
rkwargs = {'project_type': 'qdjango',
'project_id': layer.project.pk,
'layer_name': layer.qgs_layer_id,
'mode_call': 'filtertoken'
}

url = reverse('core-vector-api', kwargs=rkwargs)
req = HttpRequest()
req.method = 'GET'
req.COOKIES = kwargs["sender"].request.COOKIES
req.user = kwargs["sender"].request.user
req.resolver_match = resolve(url)
req.GET['fidsin'] = ",".join(fids)

view = LayerVectorView.as_view()
res = view(req, *[], **rkwargs).render()

return json.loads(res.content)['data']
except Exception as e:
logger.error(f'[ERROR]: Error on getting FILTERTOKEN: {e}')
return None
36 changes: 36 additions & 0 deletions g3w-admin/qdjango/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1961,7 +1961,43 @@ def test_config_with_multiline_conf(self):
self.assertEqual(jres['vector']['fields'][1]['name'], 'CAPITAL')
self.assertEqual(jres['vector']['fields'][1]['input']['type'], 'texthtml')

def test_autofilter_parameter(self):
"""
Test the 'autofilter' parameter for /vector/api/data REST API
"""

response = self._testApiCall(
'core-vector-api', [
'data',
'qdjango',
self.project_widget310.instance.pk,
'main_layer_e867d371_3388_4e2d_a214_95adbb56165c'])
resp = json.loads(response.content)
self.assertFalse('filtertoken' in resp)

response = self._testApiCall(
'core-vector-api', [
'data',
'qdjango',
self.project_widget310.instance.pk,
'main_layer_e867d371_3388_4e2d_a214_95adbb56165c'],
kwargs={
'autofilter': 'true'
})
resp = json.loads(response.content)
self.assertFalse('filtertoken' in resp)

response = self._testApiCall(
'core-vector-api', [
'data',
'qdjango',
self.project_widget310.instance.pk,
'main_layer_e867d371_3388_4e2d_a214_95adbb56165c'],
kwargs={
'autofilter': '1'
})
resp = json.loads(response.content)
self.assertTrue('filtertoken' in resp)


class TestGeoConstraintVectorAPIFilter(QdjangoTestBase):
Expand Down

0 comments on commit 11f2dff

Please sign in to comment.