Skip to content

Commit 83476d5

Browse files
sjanssen2antgonza
andauthored
Config statsmap lat long (#3388)
* Update CHANGELOG.md * let user configure the center point for the stats world map --------- Co-authored-by: Antonio Gonzalez <[email protected]>
1 parent 667a22a commit 83476d5

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

qiita_core/configuration_manager.py

+41
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ class ConfigurationManager(object):
109109
The portal subdirectory used in the URL
110110
portal_fp : str
111111
The filepath to the portal styling config file
112+
stats_map_center_latitude : float
113+
The center latitude of the world map, shown on the Stats map.
114+
Defaults to 40.01027 (Boulder, CO, USA)
115+
stats_map_center_longitude : float
116+
The center longitude of the world map, shown on the Stats map.
117+
Defaults to -105.24827 (Boulder, CO, USA)
112118
qiita_env : str
113119
The script used to start the qiita environment
114120
private_launcher : str
@@ -347,5 +353,40 @@ def _get_portal(self, config):
347353
else:
348354
self.portal_dir = ""
349355

356+
msg = ("The value %s for %s you set in Qiita's configuration file "
357+
"(section 'portal') for the Stats world map cannot be "
358+
"intepreted as a float! %s")
359+
lat_default = 40.01027 # Boulder CO, USA
360+
try:
361+
self.stats_map_center_latitude = config.get(
362+
'portal', 'STATS_MAP_CENTER_LATITUDE', fallback=lat_default)
363+
if self.stats_map_center_latitude == '':
364+
self.stats_map_center_latitude = lat_default
365+
self.stats_map_center_latitude = float(
366+
self.stats_map_center_latitude)
367+
except ValueError as e:
368+
raise ValueError(msg % (self.stats_map_center_latitude,
369+
'STATS_MAP_CENTER_LATITUDE', e))
370+
371+
lon_default = -105.24827 # Boulder CO, USA
372+
try:
373+
self.stats_map_center_longitude = config.get(
374+
'portal', 'STATS_MAP_CENTER_LONGITUDE', fallback=lon_default)
375+
if self.stats_map_center_longitude == '':
376+
self.stats_map_center_longitude = lon_default
377+
self.stats_map_center_longitude = float(
378+
self.stats_map_center_longitude)
379+
except ValueError as e:
380+
raise ValueError(msg % (self.stats_map_center_longitude,
381+
'STATS_MAP_CENTER_LONGITUDE', e))
382+
for (name, val) in [('latitude', self.stats_map_center_latitude),
383+
('longitude', self.stats_map_center_longitude)]:
384+
msg = ("The %s of %s you set in Qiita's configuration file "
385+
"(section 'portal') for the Stats world map cannot be %s!")
386+
if val < -180:
387+
raise ValueError(msg % (name, val, 'smaller than -180°'))
388+
if val > 180:
389+
raise ValueError(msg % (name, val, 'larger than 180°'))
390+
350391
def _iframe(self, config):
351392
self.iframe_qiimp = config.get('iframe', 'QIIMP')

qiita_core/support_files/config_test.cfg

+7
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ PORTAL_DIR =
183183
# Full path to portal styling config file
184184
PORTAL_FP =
185185

186+
# The center latitude of the world map, shown on the Stats map.
187+
# Defaults to 40.01027 (Boulder, CO, USA)
188+
STATS_MAP_CENTER_LATITUDE =
189+
190+
# The center longitude of the world map, shown on the Stats map.
191+
# Defaults to -105.24827 (Boulder, CO, USA)
192+
STATS_MAP_CENTER_LONGITUDE =
186193

187194
# ----------------------------- iframes settings ---------------------------
188195
[iframe]

qiita_core/tests/test_configuration_manager.py

+52
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,50 @@ def test_get_portal(self):
245245
obs._get_portal(self.conf)
246246
self.assertEqual(obs.portal_dir, "/gold_portal")
247247

248+
def test_get_portal_latlong(self):
249+
obs = ConfigurationManager()
250+
251+
# if parameters are given, but not set, they should default to Boulder
252+
self.assertEqual(obs.stats_map_center_latitude, 40.01027)
253+
self.assertEqual(obs.stats_map_center_longitude, -105.24827)
254+
255+
# a string cannot be parsed as a float
256+
self.conf.set('portal', 'STATS_MAP_CENTER_LATITUDE', 'kurt')
257+
with self.assertRaises(ValueError):
258+
obs._get_portal(self.conf)
259+
260+
# check for illegal float values
261+
self.conf.set('portal', 'STATS_MAP_CENTER_LATITUDE', "-200")
262+
with self.assertRaises(ValueError):
263+
obs._get_portal(self.conf)
264+
self.conf.set('portal', 'STATS_MAP_CENTER_LATITUDE', "200")
265+
with self.assertRaises(ValueError):
266+
obs._get_portal(self.conf)
267+
268+
# check if value defaults if option is missing altogether
269+
self.conf.remove_option('portal', 'STATS_MAP_CENTER_LATITUDE')
270+
obs._get_portal(self.conf)
271+
self.assertEqual(obs.stats_map_center_latitude, 40.01027)
272+
273+
# same as above, but for longitude
274+
# a string cannot be parsed as a float
275+
self.conf.set('portal', 'STATS_MAP_CENTER_LONGITUDE', 'kurt')
276+
with self.assertRaises(ValueError):
277+
obs._get_portal(self.conf)
278+
279+
# check for illegal float values
280+
self.conf.set('portal', 'STATS_MAP_CENTER_LONGITUDE', "-200")
281+
with self.assertRaises(ValueError):
282+
obs._get_portal(self.conf)
283+
self.conf.set('portal', 'STATS_MAP_CENTER_LONGITUDE', "200")
284+
with self.assertRaises(ValueError):
285+
obs._get_portal(self.conf)
286+
287+
# check if value defaults if option is missing altogether
288+
self.conf.remove_option('portal', 'STATS_MAP_CENTER_LONGITUDE')
289+
obs._get_portal(self.conf)
290+
self.assertEqual(obs.stats_map_center_longitude, -105.24827)
291+
248292

249293
CONF = """
250294
# ------------------------------ Main settings --------------------------------
@@ -417,6 +461,14 @@ def test_get_portal(self):
417461
# Full path to portal styling config file
418462
PORTAL_FP = /tmp/portal.cfg
419463
464+
# The center latitude of the world map, shown on the Stats map.
465+
# Defaults to 40.01027 (Boulder, CO, USA)
466+
STATS_MAP_CENTER_LATITUDE =
467+
468+
# The center longitude of the world map, shown on the Stats map.
469+
# Defaults to -105.24827 (Boulder, CO, USA)
470+
STATS_MAP_CENTER_LONGITUDE =
471+
420472
# ----------------------------- iframes settings ---------------------------
421473
[iframe]
422474
QIIMP = https://localhost:8898/

qiita_pet/templates/stats.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
vectorLayer
8282
],
8383
view: new ol.View({
84-
center: ol.proj.fromLonLat([-105.24827, 40.01027]),
84+
center: ol.proj.fromLonLat([{% raw qiita_config.stats_map_center_longitude %}, {% raw qiita_config.stats_map_center_latitude %}]),
8585
zoom: 4
8686
})
8787
});

0 commit comments

Comments
 (0)