Skip to content

Commit

Permalink
Merge pull request #11 from mrmap-community/feature/from-django-request
Browse files Browse the repository at this point in the history
adds classmethod to create from django request objects
  • Loading branch information
jokiefer authored May 26, 2023
2 parents ff81391 + b433437 commit 353171d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion ows_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.3.0"
__version__ = "0.4.0"
VERSION = __version__ # synonym
17 changes: 16 additions & 1 deletion ows_lib/models/ogc_request.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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] = []
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/models/tests_ogc_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down

0 comments on commit 353171d

Please sign in to comment.