From 1eaa1e9f48bd124e8477d99aa9513ec1e1cf7e35 Mon Sep 17 00:00:00 2001 From: Andrew Champion Date: Fri, 20 Jul 2018 14:51:38 -0400 Subject: [PATCH] Volumes: create and remove volume class instances See catmaid/CATMAID#1765. --- django/applications/catmaid/control/volume.py | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/django/applications/catmaid/control/volume.py b/django/applications/catmaid/control/volume.py index 0839257c21..d09d4cf306 100644 --- a/django/applications/catmaid/control/volume.py +++ b/django/applications/catmaid/control/volume.py @@ -108,11 +108,33 @@ def save(self): raise ValueError("Can't create new volume without mesh") cursor.execute(""" - INSERT INTO catmaid_volume (user_id, project_id, editor_id, name, - comment, creation_time, edition_time, geometry) - VALUES (%(uid)s, %(pid)s, %(uid)s, %(t)s, %(c)s, now(), now(), """ + - surface + """) - RETURNING id;""", params) + WITH v AS ( + INSERT INTO catmaid_volume (user_id, project_id, editor_id, name, + comment, creation_time, edition_time, geometry) + VALUES (%(uid)s, %(pid)s, %(uid)s, %(t)s, %(c)s, now(), now(), """ + + surface + """) + RETURNING user_id, project_id, id + ), ci AS ( + INSERT INTO class_instance (user_id, project_id, name, class_id) + SELECT %(uid)s, project_id, %(t)s, id + FROM class + WHERE project_id = %(pid)s AND class_name = 'volume' + RETURNING id + ), r AS ( + SELECT id FROM relation + WHERE project_id = %(pid)s AND relation_name = 'model_of' + ) + INSERT INTO volume_class_instance + (user_id, project_id, relation_id, volume_id, class_instance_id) + SELECT + v.user_id, + v.project_id, + r.id, + v.id, + ci.id + FROM v, ci, r + RETURNING volume_id + """, params) return cursor.fetchone()[0] @@ -405,7 +427,17 @@ def remove_volume(request, project_id, volume_id): raise Exception("You don't have permissions to delete this volume") cursor.execute(""" - DELETE FROM catmaid_volume WHERE id=%s + WITH v AS ( + DELETE FROM catmaid_volume WHERE id=%s RETURNING id + ), vci AS ( + DELETE FROM volume_class_instance + USING v + WHERE volume_id = v.id + RETURNING class_instance_id + ) + DELETE FROM class_instance + USING vci + WHERE id = vci.class_instance_id; """, (volume_id,)) return Response({ @@ -586,7 +618,7 @@ def import_volumes(request, project_id): name, extension = os.path.splitext(filename) if extension.lower() == ".stl": stl_str = uploadedfile.read().decode('utf-8') - + try: vertices, triangles = _stl_ascii_to_indexed_triangles(stl_str) except InvalidSTLError as e: