From 1bc2c28e7afa4cc379091e8c3e6d0543f5e8be86 Mon Sep 17 00:00:00 2001
From: Alexander Schlemmer <alexander@mail-schlemmer.de>
Date: Fri, 18 Feb 2022 14:32:38 +0100
Subject: [PATCH] ENH: more information from Properties is added to metadata
 now

---
 src/caosdb/high_level_api.py | 42 +++++++++++++-----------------------
 1 file changed, 15 insertions(+), 27 deletions(-)

diff --git a/src/caosdb/high_level_api.py b/src/caosdb/high_level_api.py
index 5a509380..1fa458d5 100644
--- a/src/caosdb/high_level_api.py
+++ b/src/caosdb/high_level_api.py
@@ -41,7 +41,7 @@ from .apiutils import get_type_of_entity_with
 
 from typing import Any, Optional, List, Union, Dict
 
-from dataclasses import dataclass
+from dataclasses import dataclass, fields
 from datetime import datetime
 
 @dataclass
@@ -74,7 +74,6 @@ class CaosDBPythonUnresolvedReference:
     def __init__(self, id=None):
         self.id = id
 
-
 class CaosDBPythonEntity(object):
 
     _last_id = 0
@@ -129,7 +128,7 @@ class CaosDBPythonEntity(object):
 
     @name.setter
     def name(self, val: str):
-        self._name = name
+        self._name = val
 
     @property
     def description(self):
@@ -140,7 +139,7 @@ class CaosDBPythonEntity(object):
 
     @description.setter
     def description(self, val: str):
-        self._description = description
+        self._description = val
 
     @property
     def version(self):
@@ -168,11 +167,15 @@ class CaosDBPythonEntity(object):
               The entity to be set.
         """
         
-        name = ent.name
-        val = ent.value
-        pr = ent.datatype
-        val, reference = self._type_converted_value(val, pr)
-        self.set_property(name, val, reference, datatype=pr)
+        val = self._type_converted_value(ent.value, ent.datatype)
+        metadata = self.get_property_metadata(ent.name)
+        for prop_name in fields(metadata):
+            k = prop_name.name
+            metadata.__setattr__(k, ent.__getattribute__(k))
+        self.set_property(
+            ent.name,
+            val,
+            datatype=ent.datatype)
 
     def get_property_metadata(self, prop_name: str) -> CaosDBPropertyMetaData:
         """
@@ -223,21 +226,8 @@ class CaosDBPythonEntity(object):
         overwrite: bool
                    Use this if you definitely only want one property with
                    that name (set to True).
-
-        datatype: Optional[str]
         """
 
-        # Store the datatype (if given) in a hidden field
-        # The datatypes will be stored in name, value pairs.
-        # TODO: check whether handling of lists and multi properties is correct.
-        metadata = self.get_property_metadata(name)
-        metadata.datatype = datatype
-
-        if isinstance(name, db.Entity):
-            # TODO: check, whether this is reasonable?
-            name = name.name
-            raise NotImplementedError()
-
         if name in self._forbidden:
             raise RuntimeError("Entity cannot be converted to a corresponding "
                                "Python representation. Name of property " +
@@ -362,8 +352,7 @@ class CaosDBPythonEntity(object):
             return [att]
 
     def add_parent(self, parent: Union[
-            CaosDBPythonUnresolvedParent,
-            CaosDBPythonRecordType]):
+            CaosDBPythonUnresolvedParent, "CaosDBPythonRecordType"]):
         """
         Add a parent to this entity. Either using an unresolved parent or
         using a real record type.
@@ -371,11 +360,10 @@ class CaosDBPythonEntity(object):
 
         if self.has_parent(parent):
             raise RuntimeError("Duplicate parent.")
-        self._parents.add(parent)
+        self._parents.append(parent)
 
     def has_parent(self, parent: Union[
-            CaosDBPythonUnresolvedParent,
-            CaosDBPythonRecordType]):
+            CaosDBPythonUnresolvedParent, "CaosDBPythonRecordType"]):
         """
         Check whether this parent already exists for this entity.
         """
-- 
GitLab