diff --git a/g3w-admin/qdjango/tests/test_column_acl.py b/g3w-admin/qdjango/tests/test_column_acl.py index d159a7732..315269ee3 100644 --- a/g3w-admin/qdjango/tests/test_column_acl.py +++ b/g3w-admin/qdjango/tests/test_column_acl.py @@ -43,6 +43,7 @@ from unittest import skipIf from .base import QdjangoTestBase +from qgis.PyQt.QtCore import QTemporaryDir import os logger = logging.getLogger(__name__) @@ -305,6 +306,54 @@ def test_vector_api(self): self.assertIsNone(record['AREA']) self.assertIsNone(record['SOURCETHM']) + # Test for download API + # ------------------------------------------------- + + self.world.download_csv = True + self.world.download = True + self.world.save() + + response = self._testApiCallAdmin01( + 'core-vector-api', [ + 'csv', + 'qdjango', + self.world.project.pk, + self.world.qgis_layer.id()]) + + self.assertEqual(response.status_code, 200) + + temp = QTemporaryDir() + fname = temp.path() + '/temp.csv' + with open(fname, 'wb+') as f: + f.write(response.content) + + vl = QgsVectorLayer(fname) + self.assertTrue(vl.isValid()) + + self.assertEqual(['APPROX', 'CAPITAL', 'NAME'].sort(), + [vl.attributeDisplayName(idx) for idx in vl.attributeList()].sort()) + + # SHP + response = self._testApiCallAdmin01( + 'core-vector-api', [ + 'shp', + 'qdjango', + self.world.project.pk, + self.world.qgis_layer.id()]) + + self.assertEqual(response.status_code, 200) + + self.assertEqual(response.status_code, 200) + z = zipfile.ZipFile(BytesIO(response.content)) + temp = QTemporaryDir() + z.extractall(temp.path()) + vl = QgsVectorLayer(temp.path()) + self.assertTrue(vl.isValid()) + + self.assertEqual(['APPROX', 'CAPITAL', 'NAME'].sort(), + [vl.attributeDisplayName(idx) for idx in vl.attributeList()].sort()) + + def test_init_config(self): response = self._testApiCallAdmin01( diff --git a/g3w-admin/qdjango/vector.py b/g3w-admin/qdjango/vector.py index b3704a193..a3363a304 100644 --- a/g3w-admin/qdjango/vector.py +++ b/g3w-admin/qdjango/vector.py @@ -604,6 +604,7 @@ def _selection_responde_download_mode(self, qgs_request, save_options): ) save_options.onlySelectedFeatures = True + def _build_download_filename(self, request): """Build file name on filter context""" @@ -702,7 +703,7 @@ def _add_extrafields(self, request, filepath, filename): except Exception as e: logger.error(e) - def _set_download_attributes(self, save_options): + def _set_download_attributes(self, qgs_request, save_options): """ Set attributes for QgsVectorFileWriter.SaveVectorOptions instance. Check for fields excluded for WMS service into QGIS project. @@ -716,6 +717,14 @@ 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)) + # Integrate attributes removed by filters by intersection + if qgs_request.subsetOfAttributes(): + if len(save_options.attributes) > 0: + save_options.attributes = list( + set(qgs_request.subsetOfAttributes()).intersection(set(save_options.attributes))) + else: + save_options.attributes = qgs_request.subsetOfAttributes() + def response_shp_mode(self, request): """ Download Shapefile of data @@ -743,7 +752,7 @@ def response_shp_mode(self, request): save_options.fileEncoding = 'utf-8' # Set attributes - self._set_download_attributes(save_options) + self._set_download_attributes(qgs_request, save_options) # Make a selection based on the request self._selection_responde_download_mode(qgs_request, save_options) @@ -829,7 +838,7 @@ def response_gpx_mode(self, request): ] # Set attributes - self._set_download_attributes(save_options) + self._set_download_attributes(qgs_request, save_options) filename = self._build_download_filename(request) + '.gpx' @@ -881,7 +890,7 @@ def response_xls_mode(self, request): save_options.fileEncoding = 'utf-8' # Set attributes - self._set_download_attributes(save_options) + self._set_download_attributes(qgs_request, save_options) tmp_dir = tempfile.TemporaryDirectory() @@ -935,7 +944,7 @@ def response_gpkg_mode(self, request): save_options.fileEncoding = 'utf-8' # Set attributes - self._set_download_attributes(save_options) + self._set_download_attributes(qgs_request, save_options) tmp_dir = tempfile.TemporaryDirectory() @@ -989,7 +998,7 @@ def response_csv_mode(self, request): save_options.fileEncoding = 'utf-8' # Set attributes - self._set_download_attributes(save_options) + self._set_download_attributes(qgs_request, save_options) tmp_dir = tempfile.TemporaryDirectory()