From 22d871ef2c905ad38d67c828e565101aed86b845 Mon Sep 17 00:00:00 2001 From: Eva Bardou <ebardou@teklia.com> Date: Thu, 23 Jul 2020 13:33:31 +0000 Subject: [PATCH] Add generic function create_sub_element() --- arkindex_worker/worker.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/arkindex_worker/worker.py b/arkindex_worker/worker.py index a0750937..99971d3b 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, + }, + ) -- GitLab