Skip to content
Snippets Groups Projects

Call PartialUpdateElement but correct payload

Merged Yoann Schneider requested to merge call-update-element into master
All threads resolved!
Files
2
@@ -276,17 +276,17 @@ class ElementMixin(object):
return created_ids
def update_element(
self, element: Union[Element, CachedElement], type: str, name: str, **kwargs
def partial_update_element(
self, element: Union[Element, CachedElement], **kwargs
) -> dict:
"""
Updates an element through the API.
:param element: The element to update.
:param type: Slug type of the element.
:param name: Name of the element.
:param **kwargs:
* *type* (``str``): Optional slug type of the element.
* *name* (``str``): Optional name of the element.
* *polygon* (``list``): Optional polygon for this element
* *confidence* (``float``): Optional confidence score of this element
* *rotation_angle* (``int``): Optional rotation angle of this element
@@ -299,9 +299,14 @@ class ElementMixin(object):
assert element and isinstance(
element, (Element, CachedElement)
), "element shouldn't be null and should be an Element or CachedElement"
assert isinstance(type, str), "type should be a str"
assert isinstance(name, str), "name should be a str"
if polygon := kwargs.get("polygon"):
+1
if (element_type := kwargs.get("type")) is not None:
assert isinstance(element_type, str), "type should be a str"
if (name := kwargs.get("name")) is not None:
assert isinstance(name, str), "name should be a str"
if (polygon := kwargs.get("polygon")) is not None:
assert isinstance(polygon, list), "polygon should be a list"
assert len(polygon) >= 3, "polygon should have at least three points"
assert all(
@@ -311,20 +316,20 @@ class ElementMixin(object):
isinstance(coord, (int, float)) for point in polygon for coord in point
), "polygon points should be lists of two numbers"
if "confidence" in kwargs and (confidence := kwargs.get("confidence")):
if (confidence := kwargs.get("confidence")) is not None:
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"):
if (rotation_angle := kwargs.get("rotation_angle")) is not None:
assert (
isinstance(rotation_angle, int) and rotation_angle >= 0
), "rotation_angle should be a positive integer"
if mirrored := kwargs.get("mirrored"):
if (mirrored := kwargs.get("mirrored")) is not None:
assert isinstance(mirrored, bool), "mirrored should be a boolean"
if image := kwargs.get("image"):
if (image := kwargs.get("image")) is not None:
assert isinstance(image, UUID), "image should be a UUID"
# Cast to string
kwargs["image"] = str(kwargs["image"])
@@ -333,27 +338,25 @@ class ElementMixin(object):
logger.warning("Cannot update element as this worker is in read-only mode")
return
payload = {"type": type, "name": name, **kwargs}
updated_element = self.request(
"UpdateElement",
"PartialUpdateElement",
id=element.id,
body=payload,
body=kwargs,
)
if self.use_cache:
# Name is not present in CachedElement model
payload.pop("name")
kwargs.pop("name", None)
# Stringify polygon if present
if "polygon" in payload:
payload["polygon"] = str(payload["polygon"])
if "polygon" in kwargs:
kwargs["polygon"] = str(kwargs["polygon"])
# Retrieve the right image
if "image" in payload:
payload["image"] = CachedImage.get_by_id(payload["image"])
if "image" in kwargs:
kwargs["image"] = CachedImage.get_by_id(kwargs["image"])
CachedElement.update(**payload).where(
CachedElement.update(**kwargs).where(
CachedElement.id == element.id
).execute()
Loading