Skip to content
Snippets Groups Projects

Call PartialUpdateElement but correct payload

Merged Yoann Schneider requested to merge call-update-element into master
Files
3
@@ -3,6 +3,7 @@
ElementsWorker methods for elements and element types.
"""
from typing import Dict, Iterable, List, NamedTuple, Optional, Union
from uuid import UUID
from peewee import IntegrityError
@@ -276,32 +277,25 @@ class ElementMixin(object):
return created_ids
def update_element(
self,
element: Union[Element, CachedElement],
type: Optional[str] = None,
name: Optional[str] = None,
polygon: Optional[List[List[Union[int, float]]]] = None,
confidence: Optional[float] = None,
self, element: Union[Element, CachedElement], type: str, name: str, **kwargs
) -> dict:
"""
Partially update an element through the API.
Updates an element through the API.
:param element: The element to update.
:param type: Optional new slug type of the element.
:param name: Optional new name of the element.
:param polygon: Optional new polygon of the element.
:param confidence: Optional new confidence score, between 0.0 and 1.0.
:returns: A dict from the ``PartialUpdateElement`` API endpoint,
:returns: A dict from the ``UpdateElement`` API endpoint,
"""
assert element and isinstance(
element, (Element, CachedElement)
), "element shouldn't be null and should be an Element or CachedElement"
assert type is None or isinstance(type, str), "type should be None or a str"
assert name is None or isinstance(name, str), "name should be None or a str"
assert polygon is None or isinstance(
polygon, list
), "polygon should be None or a list"
if polygon:
assert isinstance(type, str), "type should be a str"
assert isinstance(name, str), "name should be a str"
if polygon := kwargs.get("polygon"):
assert isinstance(polygon, list), "polygon should be a list"
assert len(polygon) >= 3, "polygon should have at least three points"
assert all(
isinstance(point, list) and len(point) == 2 for point in polygon
@@ -309,33 +303,51 @@ class ElementMixin(object):
assert all(
isinstance(coord, (int, float)) for point in polygon for coord in point
), "polygon points should be lists of two numbers"
assert confidence is None or (
isinstance(confidence, float) and 0 <= confidence <= 1
), "confidence should be None or a float in [0..1] range"
if "confidence" in kwargs and (confidence := kwargs.get("confidence")):
assert (
isinstance(confidence, float) and 0 <= confidence <= 1
), "confidence should be None or a float in [0..1] range"
if rotation_angle := kwargs.get("rotation_angle"):
assert (
isinstance(rotation_angle, int) and rotation_angle >= 0
), "rotation_angle should be a positive integer"
if mirrored := kwargs.get("mirrored"):
assert isinstance(mirrored, bool), "mirrored should be a boolean"
if image := kwargs.get("image"):
assert isinstance(image, UUID), "image should be a UUID"
# Cast to string
kwargs["image"] = str(kwargs["image"])
if self.is_read_only:
logger.warning("Cannot update element as this worker is in read-only mode")
return
payload = {"type": type, "name": name, **kwargs}
updated_element = self.request(
"PartialUpdateElement",
"UpdateElement",
id=element.id,
body={
"type": type,
"name": name,
"polygon": polygon,
"confidence": confidence,
},
body=payload,
)
if self.use_cache:
CachedElement.update(
{
CachedElement.type: type,
CachedElement.polygon: str(polygon),
CachedElement.confidence: confidence,
}
).where(CachedElement.id == element.id).execute()
# Name is not present in CachedElement model
payload.pop("name")
# Stringify polygon if present
if "polygon" in payload:
payload["polygon"] = str(payload["polygon"])
# Retrieve the right image
payload["image"] = CachedImage.get_by_id(payload["image"])
CachedElement.update(**payload).where(
CachedElement.id == element.id
).execute()
return updated_element
Loading