Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add new REST API parameter: ftod (fields to download) #619

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/app.min.js

Large diffs are not rendered by default.

Binary file modified g3w-admin/qdjango/tests/data/geodata/qgis_widget_test_data.gpkg
Binary file not shown.
139 changes: 137 additions & 2 deletions g3w-admin/qdjango/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def test_server_filters_value_relation_api(self):
qgs_request = QgsFeatureRequest()
qgs_request.setFilterExpression("\"type\" IN ('B','B1')")
total_count = len([f for f in qgis_layer.getFeatures(qgs_request)])

resp = json.loads(
self._testApiCall(
"core-vector-api",
Expand All @@ -674,9 +674,144 @@ def test_server_filters_value_relation_api(self):
{"field": "type|ilike|B", "formatter": "1"},
).content
)

self.assertEqual(resp["vector"]["count"], total_count)

def test_vector_api_url_param_ftod(self):
"""Test for URL Parameter for vecto api REST ftod (fields to download)"""

# Set download propteries
pois = Layer.objects.get(
project_id=self.project328_value_relation.instance.pk,
qgs_layer_id='poi_2c470d17_a234_464c_83f8_416bcdedda17')

pois.download = True
pois.download_csv = True
pois.download_xls = True
pois.download_gpx = True
pois.download_gpkg = True

pois.save()

response = self._testApiCall('core-vector-api',
[
'csv',
'qdjango',
self.project328_value_relation.instance.pk,
'poi_2c470d17_a234_464c_83f8_416bcdedda17'
],
{
'ftod': ''
})
self.assertEqual(response.status_code, 200)
temp = QTemporaryDir()
fname = temp.path() + '/temp_ftod.csv'
with open(fname, 'wb') as f:
f.write(response.content)

vl = QgsVectorLayer(fname)
self.assertTrue(vl.isValid())

fields = [f for f in vl.fields()]

self.assertEqual(len(fields), 4)
self.assertEqual(fields[0].name(), 'pkuid')

response = self._testApiCall('core-vector-api',
[
'csv',
'qdjango',
self.project328_value_relation.instance.pk,
'poi_2c470d17_a234_464c_83f8_416bcdedda17'
],
{
'ftod': 'rif,type'
})
self.assertEqual(response.status_code, 200)
temp = QTemporaryDir()
fname = temp.path() + '/temp_ftod.csv'
with open(fname, 'wb') as f:
f.write(response.content)

vl = QgsVectorLayer(fname)
self.assertTrue(vl.isValid())

fields = [f for f in vl.fields()]

self.assertEqual(len(fields), 2)
self.assertEqual(fields[0].name(), 'rif')
self.assertEqual(fields[1].name(), 'type')

# Check backspaces
response = self._testApiCall('core-vector-api',
[
'csv',
'qdjango',
self.project328_value_relation.instance.pk,
'poi_2c470d17_a234_464c_83f8_416bcdedda17'
],
{
'ftod': 'rif,type '
})
self.assertEqual(response.status_code, 200)
temp = QTemporaryDir()
fname = temp.path() + '/temp_ftod.csv'
with open(fname, 'wb') as f:
f.write(response.content)

vl = QgsVectorLayer(fname)
self.assertTrue(vl.isValid())

fields = [f for f in vl.fields()]

self.assertEqual(len(fields), 2)
self.assertEqual(fields[0].name(), 'rif')
self.assertEqual(fields[1].name(), 'type')

response = self._testApiCall('core-vector-api',
[
'csv',
'qdjango',
self.project328_value_relation.instance.pk,
'poi_2c470d17_a234_464c_83f8_416bcdedda17'
],
{
'ftod': 'goofy'
})
self.assertEqual(response.status_code, 200)
temp = QTemporaryDir()
fname = temp.path() + '/temp_ftod.csv'
with open(fname, 'wb') as f:
f.write(response.content)

vl = QgsVectorLayer(fname)
self.assertFalse(vl.isValid())

response = self._testApiCall('core-vector-api',
[
'csv',
'qdjango',
self.project328_value_relation.instance.pk,
'poi_2c470d17_a234_464c_83f8_416bcdedda17'
],
{
'ftod': 'rif,goofy'
})
self.assertEqual(response.status_code, 200)
temp = QTemporaryDir()
fname = temp.path() + '/temp_ftod.csv'
with open(fname, 'wb') as f:
f.write(response.content)

vl = QgsVectorLayer(fname)
self.assertTrue(vl.isValid())

fields = [f for f in vl.fields()]

self.assertEqual(len(fields), 1)
self.assertEqual(fields[0].name(), 'rif')


class TestQdjangoLayersAPI(QdjangoTestBase):
""" Test qdjango layer API """

Expand Down
29 changes: 29 additions & 0 deletions g3w-admin/qdjango/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ def initial(self, request, *args, **kwargs):
if 'widget_type' in kwargs:
self.widget_type = kwargs['widget_type']

# Get requests_params
if request.method == 'POST':
request_data = request.data
else:
request_data = request.query_params
if "ftod" in request_data:
self.fields_to_download = request_data.get("ftod")


super(LayerVectorView, self).initial(request, *args, **kwargs)

def get_forms(self):
Expand Down Expand Up @@ -589,6 +598,26 @@ def _set_download_attributes(self, save_options):
column_to_exclude = [self.metadata_layer.qgis_layer.fields().indexFromName(f) for f in column_to_exclude]
save_options.attributes = list(set(self.metadata_layer.qgis_layer.attributeList()) - set(column_to_exclude))

# Filter by user selection: request parameter `ftod`(fields to download)
if hasattr(self, 'fields_to_download') and self.fields_to_download != '':
try:

# Parse
fnames = self.fields_to_download.strip().split(',')

# Get fields index
findexes = [self.metadata_layer.qgis_layer.fields().indexFromName(f) for f in fnames]

if save_options.attributes and len(save_options.attributes) > 0:
save_options.attributes = list(set(save_options.attributes).intersection(set(findexes)))
else:
save_options.attributes = findexes
except:
pass

if len(save_options.attributes) == 0:
save_options.attributes = [-1]

def response_shp_mode(self, request):
"""
Download Shapefile of data
Expand Down
Loading