Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bokmål to Nynorsk macro API update [SDNTB-875] #497

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/ntb/__init__.py
Original file line number Diff line number Diff line change
@@ -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"),
)
38 changes: 34 additions & 4 deletions server/ntb/macros/nob_NO_translate_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading