From 0f839164b62c6108b7e784688d7de3d0c2544457 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Fri, 21 Jun 2024 16:54:20 +0200 Subject: [PATCH 1/2] adding new section "user_tracking" to settings with one variable JS_CODE --- qiita_core/configuration_manager.py | 14 ++++++++ qiita_core/support_files/config_test.cfg | 7 ++++ .../tests/test_configuration_manager.py | 36 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/qiita_core/configuration_manager.py b/qiita_core/configuration_manager.py index 02fb555c4..bf435034f 100644 --- a/qiita_core/configuration_manager.py +++ b/qiita_core/configuration_manager.py @@ -127,6 +127,11 @@ class ConfigurationManager(object): The email address a user should write to when asking for help sysadmin_email : str The email address, Qiita sends internal notifications to a sys admin + tracking_js_code : str + You might want to track user on your Qiita instance. The content of the + JS_CODE variable will inject this JavaScript code to the sitebase.html + template, which means it will be added to basically every page of + Qiita. Raises ------ @@ -162,6 +167,7 @@ def __init__(self): self._get_vamps(config) self._get_portal(config) self._iframe(config) + self._get_tracking(config) def _get_main(self, config): """Get the configuration of the main section""" @@ -390,3 +396,11 @@ def _get_portal(self, config): def _iframe(self, config): self.iframe_qiimp = config.get('iframe', 'QIIMP', fallback=None) + + def _get_tracking(self, config): + """Get the configuration of the 'user_tracking' section""" + + self.tracking_js_code = config.get( + 'user_tracking', 'JS_CODE', fallback=None) + if not self.tracking_js_code: + self.tracking_js_code = None diff --git a/qiita_core/support_files/config_test.cfg b/qiita_core/support_files/config_test.cfg index 917d49adf..46215a37a 100644 --- a/qiita_core/support_files/config_test.cfg +++ b/qiita_core/support_files/config_test.cfg @@ -196,3 +196,10 @@ STATS_MAP_CENTER_LONGITUDE = # On May 2024, we removed QIIMP from the code base but we will leave this # section in case we need to add access to another iframe in the future; note # that the qiita-terms are also accessed via iframe but this is internal + +# ----------------------------- User tracking Settings --------------------- +[user_tracking] +# You might want to track user on your Qiita instance. You can here inject +# JavaScript code to the sitebase.html template, which means it will be added +# to basically every page of Qiita. +JS_CODE = diff --git a/qiita_core/tests/test_configuration_manager.py b/qiita_core/tests/test_configuration_manager.py index 92b27d969..7d15cff3f 100644 --- a/qiita_core/tests/test_configuration_manager.py +++ b/qiita_core/tests/test_configuration_manager.py @@ -289,6 +289,23 @@ def test_get_portal_latlong(self): obs._get_portal(self.conf) self.assertEqual(obs.stats_map_center_longitude, -105.24827) + def test_get_tracking(self): + obs = ConfigurationManager() + + # test for multi line content + self.assertTrue(len(obs.tracking_js_code) > 520) + self.assertIn("['setTrackerUrl'", obs.tracking_js_code) + + # test that None is returned, if JS_CODE in config file, but not set + self.conf.set('user_tracking', 'JS_CODE', "") + obs._get_tracking(self.conf) + self.assertEqual(obs.tracking_js_code, None) + + # test that if JS_CODE is not in config file, result in None + self.conf.remove_option('user_tracking', 'JS_CODE') + obs._get_tracking(self.conf) + self.assertEqual(obs.tracking_js_code, None) + CONF = """ # ------------------------------ Main settings -------------------------------- @@ -471,6 +488,25 @@ def test_get_portal_latlong(self): # ----------------------------- iframes settings --------------------------- [iframe] +# ----------------------------- User tracking Settings --------------------- +[user_tracking] +# You might want to track user on your Qiita instance. You can here inject +# JavaScript code to the sitebase.html template, which means it will be added +# to basically every page of Qiita. +JS_CODE = + """ if __name__ == '__main__': From 89d34b666cfac419def702168a6795ced7e051b1 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Fri, 21 Jun 2024 16:55:00 +0200 Subject: [PATCH 2/2] inject tracking code if JS_CODE in user_tracking section of settings is not None --- qiita_pet/templates/sitebase.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/qiita_pet/templates/sitebase.html b/qiita_pet/templates/sitebase.html index 7165f33a2..a2fe4620e 100644 --- a/qiita_pet/templates/sitebase.html +++ b/qiita_pet/templates/sitebase.html @@ -334,6 +334,9 @@ + {% if qiita_config.tracking_js_code is not None %} + {% raw qiita_config.tracking_js_code %} + {% end %}