From 83476d5c3167c0309d94e983c18a27a436d9c4fa Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Thu, 4 Apr 2024 14:34:12 +0200 Subject: [PATCH] Config statsmap lat long (#3388) * Update CHANGELOG.md * let user configure the center point for the stats world map --------- Co-authored-by: Antonio Gonzalez --- qiita_core/configuration_manager.py | 41 +++++++++++++++ qiita_core/support_files/config_test.cfg | 7 +++ .../tests/test_configuration_manager.py | 52 +++++++++++++++++++ qiita_pet/templates/stats.html | 2 +- 4 files changed, 101 insertions(+), 1 deletion(-) diff --git a/qiita_core/configuration_manager.py b/qiita_core/configuration_manager.py index 44db36400..656ccc521 100644 --- a/qiita_core/configuration_manager.py +++ b/qiita_core/configuration_manager.py @@ -109,6 +109,12 @@ class ConfigurationManager(object): The portal subdirectory used in the URL portal_fp : str The filepath to the portal styling config file + stats_map_center_latitude : float + The center latitude of the world map, shown on the Stats map. + Defaults to 40.01027 (Boulder, CO, USA) + stats_map_center_longitude : float + The center longitude of the world map, shown on the Stats map. + Defaults to -105.24827 (Boulder, CO, USA) qiita_env : str The script used to start the qiita environment private_launcher : str @@ -347,5 +353,40 @@ def _get_portal(self, config): else: self.portal_dir = "" + msg = ("The value %s for %s you set in Qiita's configuration file " + "(section 'portal') for the Stats world map cannot be " + "intepreted as a float! %s") + lat_default = 40.01027 # Boulder CO, USA + try: + self.stats_map_center_latitude = config.get( + 'portal', 'STATS_MAP_CENTER_LATITUDE', fallback=lat_default) + if self.stats_map_center_latitude == '': + self.stats_map_center_latitude = lat_default + self.stats_map_center_latitude = float( + self.stats_map_center_latitude) + except ValueError as e: + raise ValueError(msg % (self.stats_map_center_latitude, + 'STATS_MAP_CENTER_LATITUDE', e)) + + lon_default = -105.24827 # Boulder CO, USA + try: + self.stats_map_center_longitude = config.get( + 'portal', 'STATS_MAP_CENTER_LONGITUDE', fallback=lon_default) + if self.stats_map_center_longitude == '': + self.stats_map_center_longitude = lon_default + self.stats_map_center_longitude = float( + self.stats_map_center_longitude) + except ValueError as e: + raise ValueError(msg % (self.stats_map_center_longitude, + 'STATS_MAP_CENTER_LONGITUDE', e)) + for (name, val) in [('latitude', self.stats_map_center_latitude), + ('longitude', self.stats_map_center_longitude)]: + msg = ("The %s of %s you set in Qiita's configuration file " + "(section 'portal') for the Stats world map cannot be %s!") + if val < -180: + raise ValueError(msg % (name, val, 'smaller than -180°')) + if val > 180: + raise ValueError(msg % (name, val, 'larger than 180°')) + def _iframe(self, config): self.iframe_qiimp = config.get('iframe', 'QIIMP') diff --git a/qiita_core/support_files/config_test.cfg b/qiita_core/support_files/config_test.cfg index 8f922e34e..2bcb5bec4 100644 --- a/qiita_core/support_files/config_test.cfg +++ b/qiita_core/support_files/config_test.cfg @@ -183,6 +183,13 @@ PORTAL_DIR = # Full path to portal styling config file PORTAL_FP = +# The center latitude of the world map, shown on the Stats map. +# Defaults to 40.01027 (Boulder, CO, USA) +STATS_MAP_CENTER_LATITUDE = + +# The center longitude of the world map, shown on the Stats map. +# Defaults to -105.24827 (Boulder, CO, USA) +STATS_MAP_CENTER_LONGITUDE = # ----------------------------- iframes settings --------------------------- [iframe] diff --git a/qiita_core/tests/test_configuration_manager.py b/qiita_core/tests/test_configuration_manager.py index ebc7f67f1..f9c47ee0b 100644 --- a/qiita_core/tests/test_configuration_manager.py +++ b/qiita_core/tests/test_configuration_manager.py @@ -245,6 +245,50 @@ def test_get_portal(self): obs._get_portal(self.conf) self.assertEqual(obs.portal_dir, "/gold_portal") + def test_get_portal_latlong(self): + obs = ConfigurationManager() + + # if parameters are given, but not set, they should default to Boulder + self.assertEqual(obs.stats_map_center_latitude, 40.01027) + self.assertEqual(obs.stats_map_center_longitude, -105.24827) + + # a string cannot be parsed as a float + self.conf.set('portal', 'STATS_MAP_CENTER_LATITUDE', 'kurt') + with self.assertRaises(ValueError): + obs._get_portal(self.conf) + + # check for illegal float values + self.conf.set('portal', 'STATS_MAP_CENTER_LATITUDE', "-200") + with self.assertRaises(ValueError): + obs._get_portal(self.conf) + self.conf.set('portal', 'STATS_MAP_CENTER_LATITUDE', "200") + with self.assertRaises(ValueError): + obs._get_portal(self.conf) + + # check if value defaults if option is missing altogether + self.conf.remove_option('portal', 'STATS_MAP_CENTER_LATITUDE') + obs._get_portal(self.conf) + self.assertEqual(obs.stats_map_center_latitude, 40.01027) + + # same as above, but for longitude + # a string cannot be parsed as a float + self.conf.set('portal', 'STATS_MAP_CENTER_LONGITUDE', 'kurt') + with self.assertRaises(ValueError): + obs._get_portal(self.conf) + + # check for illegal float values + self.conf.set('portal', 'STATS_MAP_CENTER_LONGITUDE', "-200") + with self.assertRaises(ValueError): + obs._get_portal(self.conf) + self.conf.set('portal', 'STATS_MAP_CENTER_LONGITUDE', "200") + with self.assertRaises(ValueError): + obs._get_portal(self.conf) + + # check if value defaults if option is missing altogether + self.conf.remove_option('portal', 'STATS_MAP_CENTER_LONGITUDE') + obs._get_portal(self.conf) + self.assertEqual(obs.stats_map_center_longitude, -105.24827) + CONF = """ # ------------------------------ Main settings -------------------------------- @@ -417,6 +461,14 @@ def test_get_portal(self): # Full path to portal styling config file PORTAL_FP = /tmp/portal.cfg +# The center latitude of the world map, shown on the Stats map. +# Defaults to 40.01027 (Boulder, CO, USA) +STATS_MAP_CENTER_LATITUDE = + +# The center longitude of the world map, shown on the Stats map. +# Defaults to -105.24827 (Boulder, CO, USA) +STATS_MAP_CENTER_LONGITUDE = + # ----------------------------- iframes settings --------------------------- [iframe] QIIMP = https://localhost:8898/ diff --git a/qiita_pet/templates/stats.html b/qiita_pet/templates/stats.html index 37ea9d406..deb4b2aa2 100644 --- a/qiita_pet/templates/stats.html +++ b/qiita_pet/templates/stats.html @@ -81,7 +81,7 @@ vectorLayer ], view: new ol.View({ - center: ol.proj.fromLonLat([-105.24827, 40.01027]), + center: ol.proj.fromLonLat([{% raw qiita_config.stats_map_center_longitude %}, {% raw qiita_config.stats_map_center_latitude %}]), zoom: 4 }) });