generated from superdesk/newsroom-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to set any code based on a query CPCN-803
- Loading branch information
Showing
4 changed files
with
44 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from . import fix_language # noqa | ||
from . import fix_mediaformat # noqa | ||
from . import fix_mediaformat # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,53 @@ | ||
import time | ||
|
||
from typing import get_args | ||
from superdesk import get_resource_service | ||
from cp.signals import get_media_type_name, get_media_type_scheme | ||
from cp.signals import get_media_type_name, get_media_type_scheme, MediaType | ||
from newsroom.commands.manager import manager | ||
|
||
|
||
@manager.command | ||
def fix_mediaformat(resource="items", limit=500, sleep_secs=2): | ||
def fix_mediaformat( | ||
resource="items", query="", code="wireaudio", limit=500, sleep_secs=2, dry_run=False | ||
): | ||
if not query: | ||
print("Please provide a query to filter the items.") | ||
return | ||
if code not in get_args(MediaType): | ||
print("Invalid media type code.") | ||
return | ||
service = get_resource_service(resource) | ||
media_type_scheme = get_media_type_scheme() | ||
source = { | ||
"query": { | ||
"bool": {"must_not": {"term": {"subject.scheme": media_type_scheme}}} | ||
"bool": {"must": {"query_string": {"query": query}}}, | ||
}, | ||
"size": 100, | ||
"from": 0, | ||
} | ||
for i in range(int(limit)): | ||
items = service.search(source) | ||
if not items.count(): | ||
for i in range(0, int(limit), source["size"]): | ||
source["from"] = i | ||
items = list(service.search(source)) | ||
if not len(items): | ||
break | ||
for item in items: | ||
updates = {"subject": item["subject"].copy() if item.get("subject") else []} | ||
updates["subject"] = [ | ||
s for s in updates["subject"] if s.get("scheme") != media_type_scheme | ||
] | ||
updates["subject"].append( | ||
dict( | ||
code="wiretext", | ||
name=get_media_type_name("wiretext", item.get("language")), | ||
code=code, | ||
name=get_media_type_name(code, item.get("language")), | ||
scheme=media_type_scheme, | ||
) | ||
) | ||
|
||
service.system_update(item["_id"], updates, item) | ||
if dry_run: | ||
print("Would update", item["_id"], "with", updates) | ||
else: | ||
print("Updating", item["_id"]) | ||
service.system_update(item["_id"], updates, item) | ||
print(".", end="", flush=True) | ||
time.sleep(int(sleep_secs)) | ||
print("done.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters