From a7815ac9cff539ca156c9e85823c6d955fc7477c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <henrik@trineo.org>
Date: Wed, 6 May 2020 14:47:51 +0200
Subject: [PATCH] FIX: passing an Entity with a name may fail

---
 src/caosdb/common/models.py | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py
index ca13c23e..37f62210 100644
--- a/src/caosdb/common/models.py
+++ b/src/caosdb/common/models.py
@@ -552,24 +552,33 @@ class Entity(object):
             The first Property of this Entity with a matching name or id.
 
         """
-        result = None
-        if hasattr(pattern, "name"):
-            result = self.get_property(pattern.name)
-        if result is None and hasattr(pattern, "id"):
-            result = self.get_property(pattern.id)
-        if result is not None:
-            return result
-
-        if isinstance(pattern, int):
+        # entity given
+
+        if (hasattr(pattern, "name") or hasattr(pattern, "id")):
+            # only return if a result was found, otherwise use id
+
+            if (hasattr(pattern, "name") and pattern.name is not None
+                    and self.get_property(pattern.name) is not None):
+
+                return self.get_property(pattern.name)
+
+            if hasattr(pattern, "id")and pattern.id is not None:
+                return self.get_property(pattern.id)
+
+        # int given
+        elif isinstance(pattern, int):
             for p in self.properties:
                 if p.id is not None and int(p.id) == int(pattern):
                     return p
-        else:
+        # str given
+        elif isinstance(pattern, str):
             for p in self.properties:
                 if (p.name is not None
                         and str(p.name).lower() == str(pattern).lower()):
 
                     return p
+        else:
+            raise ValueError("argument should be entity, int , string")
 
         return None
 
-- 
GitLab