Skip to content

Commit

Permalink
Refactoring and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlorenzetti committed Oct 19, 2023
1 parent 3951559 commit edda598
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions g3w-admin/qdjango/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ def response_filtertoken_mode(self, request):
request_data = request.query_params

def _get_sessiontokenfilter():
"""
Get or create an instance of SessionTokeFilter model
:return: A tuple with the instance of SessioneTokeFilter model and a boolean state for created or retreive
:rtype: tuple
"""
s, created = SessionTokenFilter.objects.get_or_create(
sessionid=sessionid,
defaults={'user': request.user if request.user.pk else None}
Expand All @@ -410,11 +416,41 @@ def _get_sessiontokenfilter():


def _create_qgs_expr(s, fidsin=None, fidsout=None):
""" Create qgs expression to save in db """
"""
Create qgs expression to save in db
:param s: Instance of SessionTokenFilter model.
:param fidsin: String of features id to contain.
:param fidsout: String of features id to exlude.
:return: QGIS expression string
:rtype: str
"""

expr = f'$id IN ({fidsin})' if fidsin else f'$id NOT IN ({fidsout})'
return f'{s.qgs_expr} AND {expr}' if s else expr

def _check_url_params_name_fid(mode):
"""
Check for 'name' and 'fid' url parameters
:param mode: Value of 'mode' URL parameter
:return: Dict for model to save with fid or name key
:rtype: dict
"""
name = request_data.get('name')
fid = request_data.get('fid')
if not name and not fid:
raise APIException(
f"'name' or 'fid' parameter is required for mode='{mode}'.")
if fid and name:
raise APIException(
f"'fid' only or 'name' only parameter is required for mode='{mode}'.")

# Return dict for model
return {'pk': fid} if fid else {'name': name}




if mode == 'create_update':

Expand Down Expand Up @@ -469,16 +505,7 @@ def _create_qgs_expr(s, fidsin=None, fidsout=None):

elif mode == 'apply':

name = request_data.get('name')
fid = request_data.get('fid')
if not name and not fid:
raise APIException(
"'name' or 'fid' parameter is required for mode='apply'.")

if fid:
kwargs = {'pk': fid}
else:
kwargs = {'name': name}
kwargs = _check_url_params_name_fid('apply')

fls = FilterLayerSaved.objects.get(
layer = self.layer,
Expand All @@ -496,22 +523,15 @@ def _create_qgs_expr(s, fidsin=None, fidsout=None):
}
)

# Return newone of current filter token
token_data.update({
'filtertoken': s.token
})


elif mode == 'delete_saved':

name = request_data.get('name')
fid = request_data.get('fid')
if not name and not fid:
raise APIException(
"'name' or 'fid' parameter is required for mode='delete_saved'.")
if fid:
kwargs = {'pk': fid}
else:
kwargs = {'name': name}
kwargs = _check_url_params_name_fid('delete_saved')

FilterLayerSaved.objects.get(
layer = self.layer,
Expand Down

0 comments on commit edda598

Please sign in to comment.