2019-10-25 12:36:58 +00:00
|
|
|
from typing import Union, Sequence, Tuple
|
2019-09-24 18:34:35 +00:00
|
|
|
|
|
|
|
from database.utils import partition_tags
|
|
|
|
from service_repo import APICall
|
|
|
|
from service_repo.base import PartialVersion
|
|
|
|
|
|
|
|
|
|
|
|
def conform_output_tags(call: APICall, documents: Union[dict, Sequence[dict]]):
|
2019-10-25 12:36:58 +00:00
|
|
|
"""
|
|
|
|
For old clients both tags and system tags are returned in 'tags' field
|
|
|
|
"""
|
2019-09-24 18:34:35 +00:00
|
|
|
if call.requested_endpoint_version >= PartialVersion("2.3"):
|
|
|
|
return
|
|
|
|
if isinstance(documents, dict):
|
|
|
|
documents = [documents]
|
|
|
|
for doc in documents:
|
|
|
|
system_tags = doc.get("system_tags")
|
|
|
|
if system_tags:
|
|
|
|
doc["tags"] = list(set(doc.get("tags", [])) | set(system_tags))
|
|
|
|
|
|
|
|
|
|
|
|
def conform_tag_fields(call: APICall, document: dict):
|
2019-10-25 12:36:58 +00:00
|
|
|
"""
|
|
|
|
Upgrade old client tags in place
|
|
|
|
"""
|
|
|
|
if "tags" in document:
|
|
|
|
tags, system_tags = conform_tags(
|
|
|
|
call, document["tags"], document.get("system_tags")
|
|
|
|
)
|
|
|
|
if tags != document.get("tags"):
|
|
|
|
document["tags"] = tags
|
|
|
|
if system_tags != document.get("system_tags"):
|
|
|
|
document["system_tags"] = system_tags
|
|
|
|
|
|
|
|
|
|
|
|
def conform_tags(
|
|
|
|
call: APICall, tags: Sequence, system_tags: Sequence
|
|
|
|
) -> Tuple[Sequence, Sequence]:
|
2019-09-24 18:34:35 +00:00
|
|
|
"""
|
|
|
|
Make sure that 'tags' from the old SDK clients
|
|
|
|
are correctly split into 'tags' and 'system_tags'
|
|
|
|
Make sure that there are no duplicate tags
|
|
|
|
"""
|
|
|
|
if call.requested_endpoint_version < PartialVersion("2.3"):
|
2019-10-25 12:36:58 +00:00
|
|
|
tags, system_tags = _upgrade_tags(call, tags, system_tags)
|
|
|
|
return _get_unique_values(tags), _get_unique_values(system_tags)
|
|
|
|
|
|
|
|
|
|
|
|
def _upgrade_tags(call: APICall, tags: Sequence, system_tags: Sequence):
|
|
|
|
if tags is not None and not system_tags:
|
2019-09-24 18:34:35 +00:00
|
|
|
service_name = call.endpoint_name.partition(".")[0]
|
2019-10-25 12:36:58 +00:00
|
|
|
entity = service_name[:-1] if service_name.endswith("s") else service_name
|
|
|
|
return partition_tags(entity, tags)
|
2019-09-24 18:34:35 +00:00
|
|
|
|
2019-10-25 12:36:58 +00:00
|
|
|
return tags, system_tags
|
2019-09-24 18:34:35 +00:00
|
|
|
|
|
|
|
|
2019-10-25 12:36:58 +00:00
|
|
|
def _get_unique_values(values: Sequence) -> Sequence:
|
|
|
|
"""Get unique values from the given sequence"""
|
|
|
|
if not values:
|
|
|
|
return values
|
2019-09-24 18:34:35 +00:00
|
|
|
|
2019-10-25 12:36:58 +00:00
|
|
|
return list(set(values))
|