From ee41d72c9dc4dc5bc3a1679b3698d618f3ea550e Mon Sep 17 00:00:00 2001 From: devketanpro Date: Thu, 17 Oct 2024 15:27:05 +0530 Subject: [PATCH 1/2] =?UTF-8?q?Bokm=C3=A5l=20to=20Nynorsk=20macro=20API=20?= =?UTF-8?q?update=20[SDNTB-875]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/ntb/__init__.py | 9 +++++ server/ntb/macros/nob_NO_translate_macro.py | 38 ++++++++++++++++++--- server/settings.py | 2 ++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/server/ntb/__init__.py b/server/ntb/__init__.py index 9efb89291..a1ca4591c 100644 --- a/server/ntb/__init__.py +++ b/server/ntb/__init__.py @@ -1,7 +1,16 @@ import logging +import superdesk +from flask_babel import lazy_gettext logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) MEDIATOPICS_CV = "topics" SUBJECTCODES_CV = "subject_custom" + +superdesk.register_default_user_preference( + "macro_config", + {"fields": {"Formval nynorskrobot": ""}}, + label=lazy_gettext("Macro config"), + category=lazy_gettext("Macro"), +) diff --git a/server/ntb/macros/nob_NO_translate_macro.py b/server/ntb/macros/nob_NO_translate_macro.py index db40f223e..794fe76c9 100644 --- a/server/ntb/macros/nob_NO_translate_macro.py +++ b/server/ntb/macros/nob_NO_translate_macro.py @@ -4,22 +4,52 @@ """ import requests -from requests.auth import HTTPBasicAuth +from apps.auth import get_user +from flask import current_app as app def nob_NO_translate_macro(item, **kwargs): - creds = HTTPBasicAuth("superdesk", "babel") + preference_params = {k: True for k in get_user_preference_params()} + + token = app.config.get("OMSETT_API_TOKEN", "") + payload = { k: item.get(k) for k in item - if k in ("guid", "headline", "body_html", "body_text", "abstract", "description_html", "description_text") + if k + in ( + "guid", + "headline", + "body_html", + "body_text", + "abstract", + "description_html", + "description_text", + "evolvedfrom", + ) + } + + data = { + "token": token, + "document": payload, + "prefs": preference_params, + "fileType": "html", } - r = requests.post("http://api.smalldata.no:8080/translate", data=payload, timeout=(10, 30), auth=creds) + + r = requests.post("https://nynorsk.cloud/translate", data=data, timeout=(10, 30)) + if r.status_code == 200: item.update(r.json()) return item +def get_user_preference_params(): + user = get_user() + user_macro_preferences = user.get("user_preferences", {}).get("macro_config", {}) + field_param = user_macro_preferences.get("fields").get("Formval nynorskrobot", "") + return field_param.split(",") + + name = "Bokmal to Nynorsk Translate Macro" label = "Omsett NB til NN" callback = nob_NO_translate_macro diff --git a/server/settings.py b/server/settings.py index 81b5a624b..494743122 100644 --- a/server/settings.py +++ b/server/settings.py @@ -308,3 +308,5 @@ INGEST_DEFAULT_BODY_FOOTER = "(©NTB)" NTB_IPTC_SEQUENCE = strtobool(env("NTB_IPTC_SEQUENCE", "off")) + +OMSETT_API_TOKEN = env('OMSETT_API_TOKEN', "") From 5b3da41db7f3451497f6e21d97e96935e39f7263 Mon Sep 17 00:00:00 2001 From: devketanpro Date: Thu, 17 Oct 2024 15:35:15 +0530 Subject: [PATCH 2/2] refromat code via black --- server/settings.py | 277 ++++++++++++++++++++++----------------------- 1 file changed, 136 insertions(+), 141 deletions(-) diff --git a/server/settings.py b/server/settings.py index 494743122..ebb1f7380 100644 --- a/server/settings.py +++ b/server/settings.py @@ -17,147 +17,147 @@ from ntb.io.feed_parsers import stt_newsml # NOQA from content_api.app.settings import CONTENTAPI_INSTALLED_APPS from superdesk.default_settings import ( - HTML_TAGS_WHITELIST as _HTML_TAGS_WHITELIST, strtobool, CORE_APPS as _CORE_APPS, env + HTML_TAGS_WHITELIST as _HTML_TAGS_WHITELIST, + strtobool, + CORE_APPS as _CORE_APPS, + env, ) ABS_PATH = os.path.abspath(os.path.dirname(__file__)) -INIT_DATA_PATH = os.path.join(ABS_PATH, 'data') -LOCATORS_DATA_FILE = os.path.join(ABS_PATH, 'data', 'locators.json') +INIT_DATA_PATH = os.path.join(ABS_PATH, "data") +LOCATORS_DATA_FILE = os.path.join(ABS_PATH, "data", "locators.json") # make sure db auth is not included -CORE_APPS = [app for app in _CORE_APPS if app != 'apps.auth.db'] +CORE_APPS = [app for app in _CORE_APPS if app != "apps.auth.db"] INSTALLED_APPS = [ - 'apps.auth', - 'apps.auth.db.commands', - 'superdesk.auth.oauth', - 'superdesk.roles', - 'ntb.scanpix', - 'planning', - 'ntb.io', - 'ntb.io.feeding_services.newsworthy', - 'ntb.publish', - 'ntb.ping_scanpix', - 'ntb.mediatopics_to_subject_mapping', - - 'superdesk.users', - 'superdesk.upload', - 'superdesk.sequences', - 'superdesk.notification', - 'superdesk.activity', - 'superdesk.vocabularies', - 'superdesk.profiling', - 'superdesk.backend_meta', - 'apps.comments', - - 'superdesk.io', - 'superdesk.io.feeding_services', - 'superdesk.io.feed_parsers', - 'superdesk.io.subjectcodes', - 'superdesk.io.iptc', - 'apps.io', - 'apps.io.feeding_services', - 'superdesk.publish', - 'superdesk.commands', - 'superdesk.locators', - - 'apps.auth', - 'apps.archive', - 'apps.stages', - 'apps.desks', - 'apps.tasks', - 'apps.preferences', - 'apps.spikes', - 'apps.prepopulate', - 'apps.legal_archive', - 'apps.search', - 'apps.saved_searches', - 'apps.privilege', - 'apps.rules', - 'apps.highlights', - 'apps.products', - 'apps.publish', - 'apps.publish.enqueue', - 'apps.publish.formatters', - 'apps.content_filters', - 'apps.content_types', - 'apps.dictionaries', - 'apps.duplication', - 'apps.spellcheck', - 'apps.templates', - 'apps.archived', - 'apps.validators', - 'apps.validate', - 'apps.workspace', - 'apps.macros', - 'apps.archive_broadcast', - 'apps.search_providers', - 'apps.feature_preview', - 'apps.workqueue', - 'apps.picture_crop', - 'apps.languages', - - 'ntb.macros', - 'ntb.push_content_notification', - - 'superdesk.text_checkers.ai.imatrics', + "apps.auth", + "apps.auth.db.commands", + "superdesk.auth.oauth", + "superdesk.roles", + "ntb.scanpix", + "planning", + "ntb.io", + "ntb.io.feeding_services.newsworthy", + "ntb.publish", + "ntb.ping_scanpix", + "ntb.mediatopics_to_subject_mapping", + "superdesk.users", + "superdesk.upload", + "superdesk.sequences", + "superdesk.notification", + "superdesk.activity", + "superdesk.vocabularies", + "superdesk.profiling", + "superdesk.backend_meta", + "apps.comments", + "superdesk.io", + "superdesk.io.feeding_services", + "superdesk.io.feed_parsers", + "superdesk.io.subjectcodes", + "superdesk.io.iptc", + "apps.io", + "apps.io.feeding_services", + "superdesk.publish", + "superdesk.commands", + "superdesk.locators", + "apps.auth", + "apps.archive", + "apps.stages", + "apps.desks", + "apps.tasks", + "apps.preferences", + "apps.spikes", + "apps.prepopulate", + "apps.legal_archive", + "apps.search", + "apps.saved_searches", + "apps.privilege", + "apps.rules", + "apps.highlights", + "apps.products", + "apps.publish", + "apps.publish.enqueue", + "apps.publish.formatters", + "apps.content_filters", + "apps.content_types", + "apps.dictionaries", + "apps.duplication", + "apps.spellcheck", + "apps.templates", + "apps.archived", + "apps.validators", + "apps.validate", + "apps.workspace", + "apps.macros", + "apps.archive_broadcast", + "apps.search_providers", + "apps.feature_preview", + "apps.workqueue", + "apps.picture_crop", + "apps.languages", + "ntb.macros", + "ntb.push_content_notification", + "superdesk.text_checkers.ai.imatrics", ] RENDITIONS = { - 'picture': { - 'thumbnail': {'width': 220, 'height': 120}, - 'viewImage': {'width': 640, 'height': 640}, - 'baseImage': {'width': 1400, 'height': 1400}, + "picture": { + "thumbnail": {"width": 220, "height": 120}, + "viewImage": {"width": 640, "height": 640}, + "baseImage": {"width": 1400, "height": 1400}, + }, + "avatar": { + "thumbnail": {"width": 60, "height": 60}, + "viewImage": {"width": 200, "height": 200}, }, - 'avatar': { - 'thumbnail': {'width': 60, 'height': 60}, - 'viewImage': {'width': 200, 'height': 200}, - } } -MACROS_MODULE = env('MACROS_MODULE', 'ntb.macros') +MACROS_MODULE = env("MACROS_MODULE", "ntb.macros") -WS_HOST = env('WSHOST', '0.0.0.0') -WS_PORT = env('WSPORT', '5100') +WS_HOST = env("WSHOST", "0.0.0.0") +WS_PORT = env("WSPORT", "5100") # Determines if the ODBC publishing mechanism will be used, If enabled then pyodbc must be installed along with it's # dependencies -ODBC_PUBLISH = env('ODBC_PUBLISH', None) +ODBC_PUBLISH = env("ODBC_PUBLISH", None) # ODBC test server connection string -ODBC_TEST_CONNECTION_STRING = env('ODBC_TEST_CONNECTION_STRING', - 'DRIVER=FreeTDS;DSN=NEWSDB;UID=???;PWD=???;DATABASE=News') +ODBC_TEST_CONNECTION_STRING = env( + "ODBC_TEST_CONNECTION_STRING", + "DRIVER=FreeTDS;DSN=NEWSDB;UID=???;PWD=???;DATABASE=News", +) -DEFAULT_SOURCE_VALUE_FOR_MANUAL_ARTICLES = 'NTB' +DEFAULT_SOURCE_VALUE_FOR_MANUAL_ARTICLES = "NTB" DEFAULT_URGENCY_VALUE_FOR_MANUAL_ARTICLES = 3 DEFAULT_GENRE_VALUE_FOR_MANUAL_ARTICLES = [] # This value gets injected into NewsML 1.2 and G2 output documents. -NEWSML_PROVIDER_ID = 'ntb.no' -ORGANIZATION_NAME = env('ORGANIZATION_NAME', 'NTB') -ORGANIZATION_NAME_ABBREVIATION = env('ORGANIZATION_NAME_ABBREVIATION', 'NTB') - -AMAZON_CONTAINER_NAME = env('AMAZON_CONTAINER_NAME', '') -AMAZON_ACCESS_KEY_ID = env('AMAZON_ACCESS_KEY_ID', '') -AMAZON_SECRET_ACCESS_KEY = env('AMAZON_SECRET_ACCESS_KEY', '') -AMAZON_REGION = env('AMAZON_REGION', 'us-east-1') -AMAZON_SERVE_DIRECT_LINKS = env('AMAZON_SERVE_DIRECT_LINKS', False) -AMAZON_S3_USE_HTTPS = env('AMAZON_S3_USE_HTTPS', False) - -is_testing = os.environ.get('SUPERDESK_TESTING', '').lower() == 'true' +NEWSML_PROVIDER_ID = "ntb.no" +ORGANIZATION_NAME = env("ORGANIZATION_NAME", "NTB") +ORGANIZATION_NAME_ABBREVIATION = env("ORGANIZATION_NAME_ABBREVIATION", "NTB") + +AMAZON_CONTAINER_NAME = env("AMAZON_CONTAINER_NAME", "") +AMAZON_ACCESS_KEY_ID = env("AMAZON_ACCESS_KEY_ID", "") +AMAZON_SECRET_ACCESS_KEY = env("AMAZON_SECRET_ACCESS_KEY", "") +AMAZON_REGION = env("AMAZON_REGION", "us-east-1") +AMAZON_SERVE_DIRECT_LINKS = env("AMAZON_SERVE_DIRECT_LINKS", False) +AMAZON_S3_USE_HTTPS = env("AMAZON_S3_USE_HTTPS", False) + +is_testing = os.environ.get("SUPERDESK_TESTING", "").lower() == "true" ELASTICSEARCH_FORCE_REFRESH = is_testing ELASTICSEARCH_AUTO_AGGREGATIONS = False -SIGN_OFF_MAPPING = 'email' -DEFAULT_CONTENT_TYPE = 'Standard' -DEFAULT_LANGUAGE = 'nb-NO' +SIGN_OFF_MAPPING = "email" +DEFAULT_CONTENT_TYPE = "Standard" +DEFAULT_LANGUAGE = "nb-NO" GENERATE_SHORT_GUID = True LANGUAGES = [ - {'language': 'nb-NO', 'label': 'Bokmål', 'source': True, 'destination': False}, - {'language': 'nn-NO', 'label': 'Nynorsk', 'source': False, 'destination': True}, - {'language': 'en', 'label': 'English', 'source': False, 'destination': False}, - {'language': 'de', 'label': 'German', 'source': False, 'destination': False} + {"language": "nb-NO", "label": "Bokmål", "source": True, "destination": False}, + {"language": "nn-NO", "label": "Nynorsk", "source": False, "destination": True}, + {"language": "en", "label": "English", "source": False, "destination": False}, + {"language": "de", "label": "German", "source": False, "destination": False}, ] # NTB NITF specific behaviour @@ -167,7 +167,7 @@ NO_TAKES = True -FTP_TIMEOUT = int(env('SUPERDESK_FTP_TIMEOUT', 10)) +FTP_TIMEOUT = int(env("SUPERDESK_FTP_TIMEOUT", 10)) #: after how many minutes consider content to be too old for ingestion INGEST_OLD_CONTENT_MINUTES = 1 @@ -180,27 +180,22 @@ SESSION_EXPIRY_MINUTES = 12 * 60 NIFS_STAGE_MAP = { - 6: 'Eliteserien menn', - 682936: 'Eliteserien menn', - 679874: 'Eliteserien menn', - 679873: '1. divisjon menn', - 682948: '1. divisjon menn' + 6: "Eliteserien menn", + 682936: "Eliteserien menn", + 679874: "Eliteserien menn", + 679873: "1. divisjon menn", + 682948: "1. divisjon menn", } NIFS_QCODE_MAP = { - 1: '15054000', # Fotball - 2: '15031000', # Ishockey - 3: '15029000', # Håndball - 4: '15076000' # Bandy + 1: "15054000", # Fotball + 2: "15031000", # Ishockey + 3: "15029000", # Håndball + 4: "15076000", # Bandy } -NIFS_SPORT_MAP = { - 1: 'Fotball', - 2: 'Ishockey', - 3: 'Håndball', - 4: 'Bandy' -} +NIFS_SPORT_MAP = {1: "Fotball", 2: "Ishockey", 3: "Håndball", 4: "Bandy"} -PLANNING_EXPORT_BODY_TEMPLATE = ''' +PLANNING_EXPORT_BODY_TEMPLATE = """ {% for item in items %}

{{ item.name or item.headline or item.slugline }}

{{ item.description_text or '' }} @@ -220,7 +215,7 @@ {% endif %}

{% endfor %} -''' +""" PUBLISH_QUEUE_EXPIRY_MINUTES = 60 * 24 * 7 # 7d @@ -252,28 +247,28 @@ }, } -SCANPIX_PING_OWNER = env('SCANPIX_PING_OWNER') -SCANPIX_PING_USERNAME = env('SCANPIX_PING_USERNAME') -SCANPIX_PING_PASSWORD = env('SCANPIX_PING_PASSWORD') +SCANPIX_PING_OWNER = env("SCANPIX_PING_OWNER") +SCANPIX_PING_USERNAME = env("SCANPIX_PING_USERNAME") +SCANPIX_PING_PASSWORD = env("SCANPIX_PING_PASSWORD") -CONTENTAPI_INSTALLED_APPS += ( - 'ntb.content_api_rss', -) +CONTENTAPI_INSTALLED_APPS += ("ntb.content_api_rss",) HIGH_PRIORITY_QUEUE_ENABLED = True PLANNING_EVENT_TEMPLATES_ENABLED = True -HTML_TAGS_WHITELIST = _HTML_TAGS_WHITELIST + ('a', 'table', 'tbody', 'tr', 'td', 'th') +HTML_TAGS_WHITELIST = _HTML_TAGS_WHITELIST + ("a", "table", "tbody", "tr", "td", "th") # if google auth is not configured enable password auth -google_confs = [os.environ.get(conf) for conf in ('GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET')] -if not all(google_confs) or os.environ.get('SUPERDESK_AUTH'): - INSTALLED_APPS.append('apps.auth.db') +google_confs = [ + os.environ.get(conf) for conf in ("GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET") +] +if not all(google_confs) or os.environ.get("SUPERDESK_AUTH"): + INSTALLED_APPS.append("apps.auth.db") SCHEMA_VERSION = 1 -IMATRICS_SUBJECT_SCHEME = 'topics' +IMATRICS_SUBJECT_SCHEME = "topics" AMAZON_SQS_REGION = env("AMAZON_SQS_REGION") AMAZON_SQS_QUEUE_NAME = env("AMAZON_SQS_QUEUE_NAME") @@ -309,4 +304,4 @@ NTB_IPTC_SEQUENCE = strtobool(env("NTB_IPTC_SEQUENCE", "off")) -OMSETT_API_TOKEN = env('OMSETT_API_TOKEN', "") +OMSETT_API_TOKEN = env("OMSETT_API_TOKEN", "")