From d33b26d39b8616950964f5ccef270609275c74cd Mon Sep 17 00:00:00 2001
From: Jan Zerdik <zerdik@cesnet.cz>
Date: Mon, 23 Sep 2019 10:45:32 +0200
Subject: [PATCH] Changes in config files.

Changes in config files regarding event classes in special directory. (Redmine issue: #5751)
---
 conf/core/reporting.json.conf |  4 +++-
 conf/mentat-hawat.py.conf     |  1 -
 conf/requirements-latest.pip  |  1 +
 conf/requirements.pip         |  1 +
 lib/hawat/app.py              | 21 +++++++++++++++++----
 lib/mentat/const.py           |  2 ++
 lib/mentat/module/reporter.py |  2 ++
 7 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/conf/core/reporting.json.conf b/conf/core/reporting.json.conf
index cd00688d4..e11829b62 100644
--- a/conf/core/reporting.json.conf
+++ b/conf/core/reporting.json.conf
@@ -5,6 +5,7 @@
     "__core__reporter": {
         "reports_dir": "/var/mentat/reports/reporter",
         "templates_dir": "/etc/mentat/templates/reporter",
+        "event_classes_dir": "/etc/mentat/event_classes",
         "mail_admin": "root",
 
         # Additional variables that can be used inside the report templates.
@@ -14,7 +15,8 @@
         "template_vars": {
             "report_access_url": "https://mentat.domain.org/mentat/reports/",
             "contact_email": "abuse@domain.org",
-            "admin_email": "mentat-admin@domain.org"
+            "admin_email": "mentat-admin@domain.org",
+            "deault_event_class": "default"
         }
     },
     #
diff --git a/conf/mentat-hawat.py.conf b/conf/mentat-hawat.py.conf
index 1e2ef417e..b24cec918 100644
--- a/conf/mentat-hawat.py.conf
+++ b/conf/mentat-hawat.py.conf
@@ -14,7 +14,6 @@ SECRET_KEY = '!!!-CHANGE-ME-ASAP-!!!-local-secret-key'
 #HAWAT_REPORT_FEEDBACK_MAILS = ['admin@domain.org']
 
 # Directories with translations used in Hawat.
-BABEL_TRANSLATION_DIRECTORIES = 'translations;/etc/mentat/templates/reporter/translations'
 BABEL_DEFAULT_LOCALE          = 'en'
 BABEL_DEFAULT_TIMEZONE        = 'UTC'
 #BABEL_DEFAULT_LOCALE         = 'cs'
diff --git a/conf/requirements-latest.pip b/conf/requirements-latest.pip
index 507a48ec7..b3ab9b3f9 100644
--- a/conf/requirements-latest.pip
+++ b/conf/requirements-latest.pip
@@ -32,3 +32,4 @@ ipranges
 typedcols
 idea-format
 python-dateutil
+PyBabel-json-md
diff --git a/conf/requirements.pip b/conf/requirements.pip
index e8f264995..4658ab088 100644
--- a/conf/requirements.pip
+++ b/conf/requirements.pip
@@ -32,3 +32,4 @@ ipranges==0.1.10
 typedcols==0.1.13
 idea-format==0.1.11
 python-dateutil==2.8.0
+PyBabel-json-md==0.1.0
diff --git a/lib/hawat/app.py b/lib/hawat/app.py
index f9e8bc8a6..b92b34b1b 100644
--- a/lib/hawat/app.py
+++ b/lib/hawat/app.py
@@ -30,6 +30,7 @@ import copy
 import datetime
 import jinja2
 import yaml
+import json
 
 #
 # Flask related modules.
@@ -463,6 +464,11 @@ def _setup_app_core(app):
                 limit = flask.current_app.config['HAWAT_LIMIT_AODS']
             return hawat.utils.LimitCounter(limit)
 
+        def load_json_from_file(filename):
+            with open(filename) as fp:
+                res = json.load(fp)
+            return res
+
         return dict(
             get_endpoints_dict    = get_endpoints_dict,
             get_endpoint_class    = get_endpoint_class,
@@ -489,10 +495,11 @@ def _setup_app_core(app):
 
             current_datetime_utc = datetime.datetime.utcnow(),
 
-            include_raw       = include_raw,
-            json_to_yaml      = json_to_yaml,
-            get_uuid4         = get_uuid4,
-            get_limit_counter = get_limit_counter
+            include_raw         = include_raw,
+            json_to_yaml        = json_to_yaml,
+            get_uuid4           = get_uuid4,
+            get_limit_counter   = get_limit_counter,
+            load_json_from_file = load_json_from_file
         )
 
     class HawatJSONEncoder(flask.json.JSONEncoder):
@@ -773,6 +780,12 @@ def _setup_app_intl(app):
     :return: Modified Hawat application
     :rtype: hawat.base.HawatApp
     """
+    app.config["BABEL_TRANSLATION_DIRECTORIES"] = "translations;"
+    app.config["BABEL_TRANSLATION_DIRECTORIES"] += os.path.join(app.config["MENTAT_CORE"]["__core__reporter"]["templates_dir"], "translations;")
+    for i in os.listdir(app.config["MENTAT_CORE"]["__core__reporter"]["event_classes_dir"]):
+        if os.path.isdir(os.path.join(app.config["MENTAT_CORE"]["__core__reporter"]["event_classes_dir"], i)):
+            app.config["BABEL_TRANSLATION_DIRECTORIES"] += os.path.join(app.config["MENTAT_CORE"]["__core__reporter"]["event_classes_dir"], i, "translations;")
+
     hawat.intl.BABEL.init_app(app)
     app.set_resource(hawat.const.RESOURCE_BABEL, hawat.intl.BABEL)
     app.cli.add_command(hawat.intl.WINTL_CLI)
diff --git a/lib/mentat/const.py b/lib/mentat/const.py
index 574c0d3e2..1df9f76bf 100644
--- a/lib/mentat/const.py
+++ b/lib/mentat/const.py
@@ -108,6 +108,8 @@ CKEY_CORE_REPORTER_TEMPLATESDIR = 'templates_dir'
 """Name of the configuration subkey key for ``templates dir`` configuration in ``core reporter`` configurations."""
 CKEY_CORE_REPORTER_TEMPLATEVARS = 'template_vars'
 """Name of the configuration subkey key for ``template vars`` configuration in ``core reporter`` configurations."""
+CKEY_CORE_REPORTER_EVENTCLASSESDIR = 'event_classes_dir'
+"""Name of the configuration subkey key for ``event classes dir`` configuration in ``core reporter`` configurations."""
 
 CKEY_CORE_INFORMANT = '__core__informant'
 """Name of the configuration key for ``core informant`` configurations."""
diff --git a/lib/mentat/module/reporter.py b/lib/mentat/module/reporter.py
index f696319e5..6dbf7ba31 100644
--- a/lib/mentat/module/reporter.py
+++ b/lib/mentat/module/reporter.py
@@ -158,6 +158,7 @@ class MentatReporterScript(mentat.script.fetcher.FetcherScript):
     CORECFG_REPORTER           = '__core__reporter'
     CONFIG_REPORTS_DIR         = 'reports_dir'
     CONFIG_TEMPLATES_DIR       = 'templates_dir'
+    CONFIG_EVENT_CLASSES_DIR   = 'event_classes_dir'
     CONFIG_TEMPLATE_VARS       = 'template_vars'
     CONFIG_FORCE_MODE          = 'force_mode'
     CONFIG_FORCE_ATTACHMENTS   = 'force_attachments'
@@ -366,6 +367,7 @@ class MentatReporterScript(mentat.script.fetcher.FetcherScript):
                 self.eventservice,
                 self.sqlservice,
                 self.mailerservice,
+                self.config[self.CORECFG_REPORTER][self.CONFIG_EVENT_CLASSES_DIR],
                 self.c(self.CONFIG_REGULAR)
             )
 
-- 
GitLab