Skip to content

Commit

Permalink
Internal components refactor [g3w-client #574] (#787)
Browse files Browse the repository at this point in the history
* move shorten condition at top (if-else)

* ternary assignment

* alias: `field['input']` → `input`

* reduce nesting

* keep a focus on JSON response structure

* comments

* spacing

* client g3w-suite/g3w-client@5bbe9ca

* `widget_serializzer_data` → `w_data`

* client g3w-suite/g3w-client@c08802c

* fix `search_1n`

* TODO

* ✨ Client
  g3w-suite/g3w-client@c8f16e9
  g3w-suite/g3w-client-plugin-editing@fccb0e6

* g3w-suite/g3w-client@b83cdad

* g3w-suite/g3w-client@f797285

* move `layerid` into `WidgetSerializer`

* 🐛 Client
  g3w-suite/g3w-client-plugin-editing@d03671e

* g3w-suite/g3w-client@28435f2

* deprecate `G3W_CLIENT_SEARCH_ENDPOINT`

* g3w-suite/g3w-client@ea6e5ac

* attempt to fix client test

* 🐛 Client
  g3w-suite/g3w-client-plugin-editing@dc37654

* remove initial `values` in within search config

* g3w-suite/g3w-client#574

* tests

* `blanktext` → `description`

* g3w-suite/g3w-client@1f3a8d9

* 🐛 Client
 g3w-suite/g3w-client-plugin-editing@5211bee

* g3w-suite/g3w-client@e108bcd

* g3w-suite/g3w-client@9618ec2

* ✨ Client
  g3w-suite/g3w-client-plugin-editing@0375222

---------

Co-authored-by: volterra79 <[email protected]>
  • Loading branch information
Raruto and volterra79 authored Apr 22, 2024
1 parent 0e73d1d commit b6be288
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 77 deletions.
4 changes: 3 additions & 1 deletion g3w-admin/base/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@
CLIENT_DEFAULT = 'client'
CLIENT_G3WSUITE_LOGO = 'g3wsuite_logo_h40.png'
CLIENT_OWS_METHOD = 'GET'
G3W_CLIENT_SEARCH_ENDPOINT = 'ows' #or 'api' for to use api layer vector with FieldFilterBackend

# deprecated since 3.8
G3W_CLIENT_SEARCH_ENDPOINT = 'api'

# If True not show empty layer into toc and main map
G3W_CLIENT_NOT_SHOW_EMPTY_VECTORLAYER = False
Expand Down
2 changes: 1 addition & 1 deletion g3w-admin/client/static/client/js/app.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions g3w-admin/client/static/client/js/app.min.js.map

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions g3w-admin/client/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@override_settings(MEDIA_ROOT=PROJECTS_PATH)
@override_settings(DATASOURCE_PATH=DATASOURCE_PATH)
@override_settings(CLIENT_OWS_METHOD='GET')
@override_settings(G3W_CLIENT_SEARCH_ENDPOINT='ows')
@override_settings(G3W_CLIENT_SEARCH_ENDPOINT='ows') # deprecated since 3.8, until permanent removal will be forced to 'api'
class ClientApiTest(CoreTestBase):
"""Test client API"""

Expand Down Expand Up @@ -237,7 +237,7 @@ def testClientConfigApiView(self):
self.assertEqual(resp["metadata"]["keywords"], ['keyword1', 'keyword2'])
self.assertEqual(resp["thumbnail"], '/media/fake/project.png')
self.assertEqual(resp["name"], "Un progetto")
self.assertEqual(resp["search_endpoint"], 'ows')
self.assertEqual(resp["search_endpoint"], 'api')

# test for tab_toc_default
self.assertTrue(resp["toc_tab_default"], 'layers')
Expand Down Expand Up @@ -649,7 +649,8 @@ def testClientConfigApiWidget(self):
self.assertEqual(resp_serach['name'], 'Test selectbox')
self.assertEqual(resp_serach['type'], 'search')
self.assertEqual(resp_serach['options']['filter'][0]['input']['type'], 'selectfield')
self.assertEqual(resp_serach['options']['filter'][0]['input']['options']['values'], [])
# removed in v3.8
# self.assertEqual(resp_serach['options']['filter'][0]['input']['options']['values'], [])
self.assertEqual(resp_serach['options']['filter'][0]['logicop'], 'AND')

# create a search widget with autocompletebox
Expand Down Expand Up @@ -745,13 +746,14 @@ def testClientConfigApiWidget(self):
self.assertEqual(resp_serach['name'], 'Test selectbox for field with valuemap')
self.assertEqual(resp_serach['type'], 'search')
self.assertEqual(resp_serach['options']['filter'][0]['input']['type'], 'selectfield')
self.assertEqual(resp_serach['options']['filter'][0]['input']['options']['values'],
[
{"value": "1", "key": "LOW"},
{"value": "2", "key": "MEDIUM"},
{"value": "3", "key": "HIGHT"}
]
)
# removed in v3.8
# self.assertEqual(resp_serach['options']['filter'][0]['input']['options']['values'],
# [
# {"value": "1", "key": "LOW"},
# {"value": "2", "key": "MEDIUM"},
# {"value": "3", "key": "HIGHT"}
# ]
# )
self.assertEqual(resp_serach['options']['filter'][0]['logicop'], 'AND')

def testClientConfigApiViewForPrint(self):
Expand Down
2 changes: 1 addition & 1 deletion g3w-admin/editing/static/editing/js/plugin.js

Large diffs are not rendered by default.

118 changes: 54 additions & 64 deletions g3w-admin/qdjango/api/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,19 +495,14 @@ def readLeaf(layer, container):
else:
ret['layers'].append(layer_serialized_data)

# get widgects for layer
# get widgets for layer
widgets = layers[layer['id']].widget_set.all()
for widget in widgets:
widget_serializzer_data = WidgetSerializer(
widget, layer=layers[layer['id']]).data
if widget_serializzer_data['type'] in ('search', 'search_1n'):
widget_serializzer_data['options']['layerid'] = layer['id']
widget_serializzer_data['options']['querylayerid'] = layer['id']

ret['search'].append(widget_serializzer_data)
w_data = WidgetSerializer(widget, layer=layers[layer['id']], layerid=layer['id']).data
if w_data['type'] in ('search', 'search_1n'):
ret['search'].append(w_data)
else:
load_qdjango_widget_layer.send(
self, layer=layer, ret=ret, widget=widget)
load_qdjango_widget_layer.send(self, layer=layer, ret=ret, widget=widget)

for l in ret['layerstree']:
try:
Expand Down Expand Up @@ -582,7 +577,8 @@ def readLeaf(layer, container):
except:
pass

ret['search_endpoint'] = settings.G3W_CLIENT_SEARCH_ENDPOINT
# deprecated since 3.8
ret['search_endpoint'] = 'api'

# Add bookmarks:
# ---------------------------------
Expand Down Expand Up @@ -925,78 +921,72 @@ class WidgetSerializer(serializers.ModelSerializer):

def __init__(self, instance=None, data=empty, **kwargs):
self.layer = kwargs['layer']
self.layerid = kwargs['layerid']
del(kwargs['layer'])
del(kwargs['layerid'])
super(WidgetSerializer, self).__init__(instance, data, **kwargs)

def to_representation(self, instance):
ret = super(WidgetSerializer, self).to_representation(instance)
ret['type'] = instance.widget_type

body = json.loads(instance.body)

# get edittype
edittypes = eval(self.layer.edittypes)

if ret['type'] == 'search':
body = json.loads(instance.body)
has_relations = 'search' == instance.widget_type and '' != body.get('relations', '')

# rewrite type ('search' → 'search_1n' if has relations)
ret['type'] = 'search_1n' if has_relations else instance.widget_type

# check if field has a widget edit type
def etype(field, key, default=None):
return edittypes.get(field['name'], {}).get(key, default)

if ret['type'] not in ('search', 'search_1n'):
ret['body'] = body

# TODO: reduce nesting level (there are too many things called 'options')
else:
ret['options'] = {
'queryurl': None,
'title': body['title'],
'results': body['results'],
'filter': [],
'dozoomtoextent': body['dozoomtoextent'],
# 'zoom': body['zoom']
}

# other layers
if 'otherlayers' in body:
ret['options'].update({
'otherquerylayerids': body['otherlayers']
})

for field in body['fields']:

# if widgettype is selectbox, get values
if 'widgettype' in field and field['widgettype'] == 'selectbox':

field['input']['type'] = 'selectfield'
field['input']['options']['values'] = []

edittype = edittypes[field['name']]

# check if field has a widget edit type
widget_type = edittype['widgetv2type']
if field['name'] in edittypes and widget_type in ('ValueMap', 'ValueRelation'):
if widget_type == 'ValueMap':
field['input']['options']['values'] = edittype['values']
else:

# Add layer params
field['input']['options']['key'] = edittype['Value']
field['input']['options']['value'] = edittype['Key']
field['input']['options']['layer_id'] = edittype['Layer']

# For AutoccOmpleteBox input type
if 'widgettype' in field and field['widgettype'] == 'autocompletebox':
field['input']['type'] = 'autocompletefield'

input = field['input']
input['options']['blanktext'] = field['blanktext']
ret['options']['filter'].append({
'layerid': self.layerid,
'querylayerid': self.layerid,
# other layers
'otherquerylayerids': body.get('otherlayers', []),
# search inputs
'filter': [{
'op': field['filterop'],
'attribute': field['name'],
'label': field['label'],
'input': input,
'logicop': field.get('logicop', 'AND').upper()
})

# rewrite type if relations is set relations
if 'relations' in body and body['relations'] != '':
ret['type'] = 'search_1n'
ret['options']['search_1n_relationid'] = body['relations']
del(body['relations'])
'logicop': field.get('logicop', 'AND').upper(),
'input': {
**field['input'],
'widget_type': etype(field, 'widgetv2type'),
'type': ({
'autocompletebox': 'autocompletefield',
'selectbox': 'selectfield'
}).get(field.get('widgettype'), field['input'].get('type')),
'options': {
**field['input'].get('options', {}),
#'values': etype(field, 'values', []), # removed in v3.8
'description': field.get('blanktext', ''), # FIXME incorrect field name: "blanktext" ?
# ValueRelation → add layer params
**({
'key' : etype(field, 'Value'),
'value': etype(field, 'Key'),
'layer_id': etype(field, 'Layer'),
} if 'ValueRelation' == etype(field, 'widgetv2type') else {}),
},
},
} for field in body['fields']],
# relation id
**({ 'search_1n_relationid': body['relations'] } if has_relations else {})
}

else:
ret['body'] = json.loads(instance.body)
return ret

class Meta:
Expand Down

0 comments on commit b6be288

Please sign in to comment.