From 49c14a67b67a7688a551ef9427195c0c76278f07 Mon Sep 17 00:00:00 2001
From: mpeltriaux <michel.peltriaux@sgdnord.rlp.de>
Date: Wed, 20 Apr 2022 09:23:24 +0200
Subject: [PATCH] #138 WIP NETGIS Map client

* adds functionality for address search widget
    * drops default proxy.php (replaced by own python call)
* reduces maxZoom in config.json
---
 konova/urls.py                   |  3 ++-
 konova/views.py                  | 28 +++++++++++++++++++++++++++-
 templates/map/client/config.json |  4 ++--
 templates/map/client/proxy.php   | 22 ----------------------
 4 files changed, 31 insertions(+), 26 deletions(-)
 delete mode 100644 templates/map/client/proxy.php

diff --git a/konova/urls.py b/konova/urls.py
index d2458f50..5a3c9456 100644
--- a/konova/urls.py
+++ b/konova/urls.py
@@ -24,7 +24,7 @@ from konova.autocompletes import EcoAccountAutocomplete, \
     ShareTeamAutocomplete, HandlerCodeAutocomplete
 from konova.settings import SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY, DEBUG
 from konova.sso.sso import KonovaSSOClient
-from konova.views import logout_view, home_view, get_geom_parcels
+from konova.views import logout_view, home_view, get_geom_parcels, map_client_proxy_view
 
 sso_client = KonovaSSOClient(SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY)
 urlpatterns = [
@@ -41,6 +41,7 @@ urlpatterns = [
     path('analysis/', include("analysis.urls")),
     path('api/', include("api.urls")),
     path('geom/<id>/parcels', get_geom_parcels, name="geometry-parcels"),
+    path('client/proxy', map_client_proxy_view, name="map-client-proxy"),
 
     # Autocomplete paths for all apps
     path("atcmplt/eco-accounts", EcoAccountAutocomplete.as_view(), name="accounts-autocomplete"),
diff --git a/konova/views.py b/konova/views.py
index 2fd5ff91..66d03141 100644
--- a/konova/views.py
+++ b/konova/views.py
@@ -5,9 +5,12 @@ Contact: michel.peltriaux@sgdnord.rlp.de
 Created on: 16.11.20
 
 """
+import json
+
+import requests
 from django.contrib.auth import logout
 from django.contrib.auth.decorators import login_required
-from django.http import HttpRequest, HttpResponse
+from django.http import HttpRequest, HttpResponse, JsonResponse
 from django.shortcuts import redirect, render, get_object_or_404
 from django.template.loader import render_to_string
 from django.utils import timezone
@@ -168,3 +171,26 @@ def get_500_view(request: HttpRequest):
     """
     context = BaseContext.context
     return render(request, "500.html", context, status=500)
+
+
+@login_required
+def map_client_proxy_view(request: HttpRequest):
+    """ Provides proxy functionality for NETGIS map client.
+
+    Used for fetching content of a provided url
+
+    Args:
+        request (HttpRequest): The incoming request
+
+    Returns:
+
+    """
+    url = request.META.get("QUERY_STRING")
+    response = requests.get(url)
+    body = json.loads(response.content)
+    if response.status_code != 200:
+        return JsonResponse({
+            "status_code": response.status_code,
+            "content": body,
+        })
+    return JsonResponse(body)
diff --git a/templates/map/client/config.json b/templates/map/client/config.json
index ca1c31f8..cbf486f2 100644
--- a/templates/map/client/config.json
+++ b/templates/map/client/config.json
@@ -34,7 +34,7 @@
 		"projection": "EPSG:25832",
 		"center": [ 385000, 5543000 ],
 		"minZoom": 5,
-		"maxZoom": 25,
+		"maxZoom": 21,
 		"zoom": 8,
 		"attribution": "LANIS RLP"
 	},
@@ -46,6 +46,6 @@
 	
 	"search":
 	{
-		"url": "./proxy.php?https://www.geoportal.rlp.de/mapbender/geoportal/gaz_geom_mobile.php?outputFormat=json&resultTarget=web&searchEPSG={epsg}&maxResults=5&maxRows=5&featureClass=P&style=full&searchText={q}&name_startsWith={q}"
+		"url": "/client/proxy?https://www.geoportal.rlp.de/mapbender/geoportal/gaz_geom_mobile.php?outputFormat=json&resultTarget=web&searchEPSG={epsg}&maxResults=5&maxRows=5&featureClass=P&style=full&searchText={q}&name_startsWith={q}"
 	}
 }
\ No newline at end of file
diff --git a/templates/map/client/proxy.php b/templates/map/client/proxy.php
deleted file mode 100644
index f011e2ee..00000000
--- a/templates/map/client/proxy.php
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-//$query = urldecode( $_GET[ "q" ] );
-$query = urldecode( $_SERVER[ "QUERY_STRING" ] );
-
-// Open the Curl session
-$session = curl_init( $query );
-
-// Don't return HTTP headers. Do return the contents of the call
-curl_setopt( $session, CURLOPT_HEADER, false );
-curl_setopt( $session, CURLOPT_RETURNTRANSFER, true );
-
-// Make the call
-$response = curl_exec( $session );
-	
-// Output
-header( "Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept" );
-header( "Access-Control-Allow-Origin: *" );
-header( "Content-Type: application/json" );
-echo $response;
-
-curl_close( $session );