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

ACL Filter layer widget on vector download as shp, gpkg, gpx, xls, csv etc. #796

Merged
merged 3 commits into from
Mar 29, 2024
Merged
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
49 changes: 49 additions & 0 deletions g3w-admin/qdjango/tests/test_column_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from unittest import skipIf
from .base import QdjangoTestBase

from qgis.PyQt.QtCore import QTemporaryDir
import os

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 15 additions & 6 deletions g3w-admin/qdjango/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
Loading