From d3642559089dcda42524d4ed7eb2a397f10fcc7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Tue, 1 Aug 2023 08:57:28 +0200 Subject: [PATCH] add manage command to fix language (#118) it will remove `-CA` or `-US` from language. CPNHUB-227 --- server/cp/commands/__init__.py | 0 server/cp/commands/fix_language.py | 23 +++++++++++++++++++++++ server/manage.py | 2 ++ 3 files changed, 25 insertions(+) create mode 100644 server/cp/commands/__init__.py create mode 100644 server/cp/commands/fix_language.py diff --git a/server/cp/commands/__init__.py b/server/cp/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/cp/commands/fix_language.py b/server/cp/commands/fix_language.py new file mode 100644 index 0000000..25c01be --- /dev/null +++ b/server/cp/commands/fix_language.py @@ -0,0 +1,23 @@ + +import time + +from superdesk import get_resource_service +from newsroom.commands.manager import manager + + +@manager.command +def fix_language(resource="items", limit=50, sleep_secs=2): + service = get_resource_service(resource) + + source = {"query": {"terms": {"language": ["en-CA", "en-US", "fr-CA"]}}, "size": 100} + + for i in range(int(limit)): + items = service.search(source) + if not items.count(): + break + for item in items: + updates = {"language": item["language"].split("-")[0]} + service.system_update(item["_id"], updates, item) + print(".", end="", flush=True) + time.sleep(int(sleep_secs)) + print(".") diff --git a/server/manage.py b/server/manage.py index a6ccc73..ad34d7b 100644 --- a/server/manage.py +++ b/server/manage.py @@ -1,6 +1,8 @@ from newsroom.commands import * # noqa from newsroom.commands.manager import manager +from cp.commands.fix_language import fix_language # noqa + if __name__ == "__main__": manager.run()