Skip to content

Commit 387db3e

Browse files
committed
[Fixes #2233 #2234 #2235] Implementation of dynamic request
1 parent ff825cb commit 387db3e

File tree

5 files changed

+435
-0
lines changed

5 files changed

+435
-0
lines changed

geonode_mapstore_client/apps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def run_setup_hooks(*args, **kwargs):
9898
re_path(r"^maps$", TemplateView.as_view(template_name="geonode-mapstore-client/pages/maps.html")),
9999
re_path(r"^documents$", TemplateView.as_view(template_name="geonode-mapstore-client/pages/documents.html")),
100100
re_path(r"^geostories$", TemplateView.as_view(template_name="geonode-mapstore-client/pages/geostories.html")),
101+
re_path("reqparams/", views.RequestConfigurationView.as_view(), name="request-params"),
101102
]
102103

103104
# adding default format for metadata schema validation
@@ -304,6 +305,9 @@ def run_setup_hooks(*args, **kwargs):
304305

305306
setattr(settings, "MAPSTORE_DASHBOARD_CATALOGUE_SELECTED_SERVICE", MAPSTORE_DASHBOARD_CATALOGUE_SELECTED_SERVICE)
306307
setattr(settings, "MAPSTORE_DASHBOARD_CATALOGUE_SERVICES", MAPSTORE_DASHBOARD_CATALOGUE_SERVICES)
308+
setattr(settings, "REQUEST_CONFIGURATION_RULES_HANDLERS", [
309+
"geonode_mapstore_client.handlers.BaseConfigurationRuleHandler",
310+
])
307311

308312

309313
def connect_geoserver_style_visual_mode_signal():
@@ -324,4 +328,8 @@ def ready(self):
324328
if not apps.ready:
325329
run_setup_hooks()
326330
connect_geoserver_style_visual_mode_signal()
331+
332+
from geonode_mapstore_client.registry import request_configuration_rules_registry
333+
request_configuration_rules_registry.init_registry()
334+
327335
super(AppConfig, self).ready()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from django.conf import settings
2+
from geonode.base.auth import get_or_create_token
3+
from .registry import BaseRequestConfigurationRuleHandler
4+
5+
6+
7+
class BaseConfigurationRuleHandler(BaseRequestConfigurationRuleHandler):
8+
"""
9+
Base handler for configuration rules.
10+
"""
11+
12+
def get_rules(self, request):
13+
user = request.user
14+
if user.is_anonymous:
15+
return []
16+
rules = []
17+
token_obj = get_or_create_token(user)
18+
access_token = token_obj.token
19+
20+
rules.extend(
21+
[
22+
{
23+
"urlPattern": f"{settings.GEOSERVER_WEB_UI_LOCATION.rstrip('/')}/.*",
24+
"params": {"access_token": access_token},
25+
},
26+
{"urlPattern": f"{settings.SITEURL.rstrip('/')}/gs.*", "params": {"access_token": access_token}},
27+
{
28+
"urlPattern": f"{settings.SITEURL.rstrip('/')}/api/v2.*",
29+
"headers": {"Authorization": f"Bearer {access_token}"},
30+
},
31+
]
32+
)
33+
return rules
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from django.conf import settings
2+
from django.utils.module_loading import import_string
3+
4+
5+
class BaseRequestConfigurationRuleHandler:
6+
"""
7+
Base class for request configuration rule handlers.
8+
"""
9+
10+
def get_rules(self, request):
11+
return []
12+
13+
14+
class RequestConfigurationRulesRegistry:
15+
"""
16+
A registry for request configuration rule handlers.
17+
"""
18+
19+
REGISTRY = []
20+
21+
def init_registry(self):
22+
self._register()
23+
self.sanity_checks()
24+
25+
def add(self, module_path):
26+
item = import_string(module_path)
27+
self.__check_item(item)
28+
if item not in self.REGISTRY:
29+
self.REGISTRY.append(item)
30+
31+
def remove(self, module_path):
32+
item = import_string(module_path)
33+
self.__check_item(item)
34+
if item in self.REGISTRY:
35+
self.REGISTRY.remove(item)
36+
37+
def reset(self):
38+
self.REGISTRY = []
39+
40+
@classmethod
41+
def get_registry(cls):
42+
return cls.REGISTRY
43+
44+
def sanity_checks(self):
45+
for item in self.REGISTRY:
46+
self.__check_item(item)
47+
48+
def get_rules(self, request):
49+
rules = []
50+
for HandlerClass in self.REGISTRY:
51+
handler = HandlerClass()
52+
rules.extend(handler.get_rules(request))
53+
return {"rules": rules}
54+
55+
def __check_item(self, item):
56+
"""
57+
Ensure that the handler is a subclass of BaseRequestConfigurationRuleHandler
58+
"""
59+
if not (isinstance(item, type) and issubclass(item, BaseRequestConfigurationRuleHandler)):
60+
raise TypeError(f"Item must be a subclass of BaseRequestConfigurationRuleHandler, " f"got {item}")
61+
62+
def _register(self):
63+
for module_path in getattr(settings, "REQUEST_CONFIGURATION_RULES_HANDLERS", []):
64+
self.add(module_path)
65+
66+
67+
request_configuration_rules_registry = RequestConfigurationRulesRegistry()

0 commit comments

Comments
 (0)