diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ff74f61..eec4624 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,8 +7,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +[v0.4.0] - 2023-05-26 +--------------------- + +Added +~~~~~ + +* implemented `from_django_request` classmethod for creating `OGCRequest` objects from django request objects. + + [v0.3.0] - 2023-05-25 -------------------------- +--------------------- Changed ~~~~~~~ diff --git a/ows_lib/__init__.py b/ows_lib/__init__.py index 0e54ddc..5022244 100644 --- a/ows_lib/__init__.py +++ b/ows_lib/__init__.py @@ -1,2 +1,2 @@ -__version__ = "0.3.0" +__version__ = "0.4.0" VERSION = __version__ # synonym diff --git a/ows_lib/models/ogc_request.py b/ows_lib/models/ogc_request.py index 1854d49..3d7ac16 100644 --- a/ows_lib/models/ogc_request.py +++ b/ows_lib/models/ogc_request.py @@ -1,6 +1,7 @@ from typing import Dict, List from django.contrib.gis.geos import GEOSGeometry +from django.http.request import HttpRequest as DjangoRequest from eulxml.xmlmap import XmlObject, load_xmlobject_from_string from requests import Request @@ -17,8 +18,9 @@ class OGCRequest(Request): """Extended Request class which provides some analyzing functionality for ogc requests.""" - def __init__(self, *args, **kwargs) -> None: + def __init__(self, django_request: DjangoRequest = None, *args, **kwargs) -> None: super().__init__(*args, **kwargs) + self._djano_request = django_request self._ogc_query_params: Dict = {} self._bbox: GEOSGeometry = None self._requested_entities: List[str] = [] @@ -37,6 +39,19 @@ def __init__(self, *args, **kwargs) -> None: self.service_version: str = post_request.version self.service_type: str = post_request.service_type + @classmethod + def from_django_request(cls, request: DjangoRequest): + """helper function to construct an OGCRequest from a django request object.""" + ogc_request = cls( + method=request.method, + url=request.build_absolute_uri(), + params={**request.GET, **request.POST}, + data=request.body, + cookies=request.COOKIES, + django_request=request + ) + return ogc_request + @property def requested_entities(self) -> List[str]: """Returns the list of requested entities diff --git a/sonar-project.properties b/sonar-project.properties index 28203c9..aee64f2 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,7 +1,7 @@ sonar.projectName=django-ows-lib sonar.projectKey=mrmap-community_django-ows-lib sonar.organization=mrmap-community -sonar.projectVersion=v0.3.0 +sonar.projectVersion=v0.4.0 # Path to sources sonar.sources=./ows_lib diff --git a/tests/models/tests_ogc_request.py b/tests/models/tests_ogc_request.py index a843bdd..7b4bad2 100644 --- a/tests/models/tests_ogc_request.py +++ b/tests/models/tests_ogc_request.py @@ -13,6 +13,26 @@ class OGCRequestTest(SimpleTestCase): def setUp(self) -> None: self.factory = RequestFactory() + def test_ogc_request_from_django_request(self): + + django_request = self.factory.get( + "http://mrmap-proxy/wms/cd16cc1f-3abb-4625-bb96-fbe80dbe23e3/", + { + "REQUEST": ["GetMap"], + "SERVICE": "WMS", + "VERSION": "1.3.0", + "LAYERS": "somelayer,anotherlayer" + } + ) + ogc_request = OGCRequest.from_django_request(django_request) + + self.assertEqual(django_request, ogc_request._djano_request) + self.assertTrue(ogc_request.is_get) + self.assertTrue(ogc_request.is_get_map_request) + self.assertEqual(["somelayer", "anotherlayer"], + ogc_request.requested_entities) + ogc_request.prepare() + def test_ogc_request_with_get_map_request(self): """Test that OGCRequest helper class works correctly for a given GetMap get request"""