Skip to content

Commit

Permalink
Bokmål to Nynorsk macro API update [SDNTB-875] (#497)
Browse files Browse the repository at this point in the history
* Bokmål to Nynorsk macro API update [SDNTB-875]

* refromat code via black
  • Loading branch information
devketanpro committed Oct 22, 2024
1 parent 5e05c77 commit c26bfb9
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 144 deletions.
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

0 comments on commit c26bfb9

Please sign in to comment.