"""
nb-NO to nn-NO Metadata Macro will perform the following changes to current content item:
- change the language to nn-NO
"""

import requests
from requests.auth import HTTPBasicAuth
from superdesk.editor_utils import generate_fields


def nob_NO_translate_macro(item, **kwargs):
    creds = HTTPBasicAuth("superdesk", "babel")
    payload = {
        k: item.get(k)
        for k in item
        if k in ("guid", "headline", "body_html", "body_text", "abstract", "description_html", "description_text")
    }
    r = requests.post("http://api.smalldata.no:8080/translate", data=payload, timeout=(10, 30), auth=creds)
    if r.status_code == 200:
        item.update(r.json())
        generate_fields(item, ["headline", "abstract", "body_html"], force=True)
    return item


name = "Bokmal to Nynorsk Translate Macro"
label = "Omsett NB til NN"
callback = nob_NO_translate_macro
access_type = "frontend"
action_type = "direct"
from_languages = ["nb-NO"]
to_languages = ["nn-NO"]
replace_type = "editor_state"

@@ -183,59 +183,8 @@
 uth%0A
-from superdesk.editor_utils import generate_fields%0A
 %0A%0Ade
@@ -626,89 +626,8 @@
 ())%0A
-        generate_fields(item, %5B%22headline%22, %22abstract%22, %22body_html%22%5D, force=True)%0A
     
@@ -848,34 +848,4 @@
 O%22%5D%0A
-replace_type = %22editor_state%22%0A

revert translate macro changes for editor3