diff --git a/arkindex_worker/worker.py b/arkindex_worker/worker.py index a0750937e7ffd087262ad012a35313c50b10d927..99971d3b464885f738f2d7a2c4a8f80ab3651391 100644 --- a/arkindex_worker/worker.py +++ b/arkindex_worker/worker.py @@ -148,3 +148,39 @@ class ElementsWorker(BaseWorker): def process_element(self, element): """Override this method to analyze an Arkindex element from the provided list""" + + def create_sub_element(self, element, type, name, polygon): + """ + Create a child element on the given element through API + """ + assert element and isinstance( + element, Element + ), "element shouldn't be null and should be of type Element" + assert type and isinstance( + type, str + ), "type shouldn't be null and should be of type str" + assert name and isinstance( + name, str + ), "name shouldn't be null and should be of type str" + assert polygon and isinstance( + polygon, list + ), "polygon shouldn't be null and should be of type 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 + ), "polygon points should be lists of two items" + assert all( + isinstance(coord, (int, float)) for point in polygon for coord in point + ), "polygon points should be lists of two numbers" + + self.api_client.request( + "CreateElement", + body={ + "type": type, + "name": name, + "image": element.zone.image.id, + "corpus": element.corpus.id, + "polygon": polygon, + "parent": element.id, + }, + )