@@ -30,6 +30,8 @@ def __init__(self, default_allow, config_models, logger):
30
30
self .qgis_server_url = os .environ .get ('QGIS_SERVER_URL' ,
31
31
'http://localhost:8001/ows/' ).rstrip ('/' ) + '/'
32
32
33
+ self .project_settings_cache = {}
34
+
33
35
def permissions (self , params , username , group , session ):
34
36
"""Query permissions for OGC service.
35
37
@@ -75,6 +77,16 @@ def permissions(self, params, username, group, session):
75
77
76
78
return permissions
77
79
80
+ def themesConfigMTime (self ):
81
+ qwc2_path = os .environ .get ('QWC2_PATH' , 'qwc2/' )
82
+ themes_config_path = os .environ .get (
83
+ 'QWC2_THEMES_CONFIG' , os .path .join (qwc2_path , 'themesConfig.json' )
84
+ )
85
+
86
+ if os .path .isfile (themes_config_path ):
87
+ return os .path .getmtime (themes_config_path )
88
+ return - 1
89
+
78
90
def parseProjectSettings (self , ows_name , ows_type ):
79
91
"""Get complete OGC service permissions from GetProjectSettings.
80
92
@@ -85,22 +97,43 @@ def parseProjectSettings(self, ows_name, ows_type):
85
97
"""
86
98
permissions = {}
87
99
88
- # get GetProjectSettings
89
- response = requests .get (
90
- urljoin (self .qgis_server_url , ows_name ),
91
- params = {
92
- 'SERVICE' : ows_type ,
93
- 'VERSION' : '1.3.0' ,
94
- 'REQUEST' : 'GetProjectSettings'
95
- },
96
- timeout = 30
97
- )
98
-
99
- if response .status_code != requests .codes .ok :
100
- self .logger .warn (
101
- "Could not get GetProjectSettings: %s" , response .content
100
+ cache = os .environ .get ("__QWC_CONFIG_SERVICE_PROJECT_SETTINGS_CACHE" , "0" ) == "1"
101
+ if cache and \
102
+ ows_type in self .project_settings_cache and \
103
+ ows_name in self .project_settings_cache [ows_type ] and \
104
+ self .project_settings_cache [ows_type ][ows_name ]["timestamp" ] != - 1 and \
105
+ self .project_settings_cache [ows_type ][ows_name ]["timestamp" ] >= self .themesConfigMTime ():
106
+ document = self .project_settings_cache [ows_type ][ows_name ]["document" ]
107
+ else :
108
+ # get GetProjectSettings
109
+ response = requests .get (
110
+ urljoin (self .qgis_server_url , ows_name ),
111
+ params = {
112
+ 'SERVICE' : ows_type ,
113
+ 'VERSION' : '1.3.0' ,
114
+ 'REQUEST' : 'GetProjectSettings'
115
+ },
116
+ timeout = 30
102
117
)
103
- return permissions
118
+
119
+ if response .status_code != requests .codes .ok :
120
+ self .logger .warn (
121
+ "Could not get GetProjectSettings: %s" , response .content
122
+ )
123
+ return permissions
124
+
125
+ document = response .content
126
+
127
+ if cache :
128
+ if not ows_type in self .project_settings_cache :
129
+ self .project_settings_cache [ows_type ] = {}
130
+ if not ows_name in self .project_settings_cache [ows_type ]:
131
+ self .project_settings_cache [ows_type ][ows_name ] = {}
132
+ self .project_settings_cache [ows_type ][ows_name ] = {
133
+ "document" : document ,
134
+ "timestamp" : self .themesConfigMTime ()
135
+ }
136
+
104
137
105
138
# parse GetProjectSettings XML
106
139
ElementTree .register_namespace ('' , 'http://www.opengis.net/wms' )
@@ -109,7 +142,7 @@ def parseProjectSettings(self, ows_name, ows_type):
109
142
ElementTree .register_namespace (
110
143
'xlink' , 'http://www.w3.org/1999/xlink'
111
144
)
112
- root = ElementTree .fromstring (response . content )
145
+ root = ElementTree .fromstring (document )
113
146
114
147
# use default namespace for XML search
115
148
# namespace dict
0 commit comments