Skip to content

Commit

Permalink
Add 'filters' property to 'layers' of /api/config REST API.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlorenzetti committed Oct 19, 2023
1 parent edda598 commit b199557
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
22 changes: 21 additions & 1 deletion g3w-admin/qdjango/api/layers/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
__date__ = '2020-05-06'
__copyright__ = 'Copyright 2015 - 2020, Gis3w'

from qdjango.models import Layer
from qdjango.models import Layer, FilterLayerSaved
from rest_framework import serializers
from usersmanage.models import User, Group as AuthGroup

Expand All @@ -36,3 +36,23 @@ class Meta:
fields = ['pk', 'name']


class FilterLayerSavedSerializer(serializers.ModelSerializer):

class Meta:
model = FilterLayerSaved
fields = (
'id',
'name',
)

def to_representation(self, instance):

ret = super().to_representation(instance)

# Change pk(id) with fid
del(ret['id'])
ret['fid'] = instance.pk

return ret


39 changes: 37 additions & 2 deletions g3w-admin/qdjango/api/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
from rest_framework import serializers
from rest_framework.fields import empty
from owslib.wms import WebMapService
from qdjango.models import Project, Layer, Widget, SessionTokenFilter, GeoConstraintRule, MSG_LEVELS
from qdjango.models import (
Project,
Layer,
Widget,
SessionTokenFilter,
GeoConstraintRule,
MSG_LEVELS,
FilterLayerSaved
)
from qdjango.utils.data import QGIS_LAYER_TYPE_NO_GEOM
from qdjango.utils.models import get_capabilities4layer, get_view_layer_ids
from qdjango.signals import load_qdjango_widget_layer
from qdjango.apps import get_qgs_project
from qdjango.utils.structure import QdjangoMetaLayer, datasourcearcgis2dict
from qdjango.api.layers.serializers import FilterLayerSavedSerializer
from core.utils.structure import mapLayerAttributes
from core.configs import *
from core.signals import after_serialized_project_layer
Expand Down Expand Up @@ -412,6 +421,16 @@ def to_representation(self, instance):
meta_layer = QdjangoMetaLayer()
to_remove_from_layerstree = []

# Get FilterToken layer filters saved:
# Build a layer_filters dict to pass FilterLayerSaved instance to LayerSerializer
layer_filters = {}
filters = FilterLayerSaved.objects.filter(user=self.request.user, layer__project=instance)
for f in filters:
if f.layer.qgs_layer_id not in layer_filters:
layer_filters[f.layer.qgs_layer_id] = []
layer_filters[f.layer.qgs_layer_id].append(f)


def readLeaf(layer, container):

if 'nodes' in layer:
Expand All @@ -431,7 +450,12 @@ def readLeaf(layer, container):

try:
layer_serialized = LayerSerializer(
layers[layer['id']], qgs_project=qgs_project, request=self.request, layertreenode=layer)
layers[layer['id']],
qgs_project=qgs_project,
request=self.request,
layertreenode=layer,
filters=layer_filters[layer['id']]
)
except KeyError:
logger.error(
'Layer %s is missing from QGIS project!' % layer['id'])
Expand Down Expand Up @@ -600,6 +624,10 @@ def __init__(self, instance=None, data=empty, **kwargs):
self.layertreenode = kwargs['layertreenode']
del (kwargs['layertreenode'])

# FilterLayerSaved
self.filters = kwargs['filters']
del (kwargs['filters'])

super(LayerSerializer, self).__init__(instance, data, **kwargs)

class Meta:
Expand Down Expand Up @@ -849,6 +877,13 @@ def to_representation(self, instance):
if 'showfeaturecount' in self.layertreenode and self.layertreenode['showfeaturecount']:
ret['featurecount'] = get_qgis_featurecount(qgs_maplayer)


# Set FilterLayerSaved instances
if len(self.filters) > 0:
ret['filters'] = []
for f in self.filters:
ret['filters'].append(FilterLayerSavedSerializer(f).data)

return ret


Expand Down
Binary file modified g3w-admin/qdjango/tests/data/geodata/qgis_widget_test_data.gpkg
Binary file not shown.

0 comments on commit b199557

Please sign in to comment.