Skip to content
Snippets Groups Projects

Support of special attribute updates in add_property

Open I. Nüske requested to merge f-217-set-special-property into dev
All threads resolved!
Files
4
@@ -617,6 +617,10 @@ class Entity:
If you want to add a property to an already existing entity, the
property ``id`` of that property needs to be specified before you send
the updated entity to the server.
If the specified name matches the name of a special attribute in
lowercase, such as 'name' or 'description', the attribute is set
instead. If the attribute already has a value, this fails with a
ValueError.
Parameters
----------
@@ -666,6 +670,9 @@ class Entity:
If you try to add an ``Entity`` object with File or Record role (or,
equivalently, a ``File`` or ``Record`` object) as a property, a
``ValueError`` is raised.
ValueError:
If the property to be added is a special attribute and already has
a value.
Examples
--------
@@ -744,6 +751,21 @@ class Entity:
raise UserWarning(
"This method expects you to pass at least an entity, a name or an id.")
# If the name is a special attribute, set the attribute instead
if name and name.lower() in SPECIAL_ATTRIBUTES and name.lower() in dir(self):
name = name.lower()
if getattr(self, name) is None:
setattr(self, name, value)
return self
else:
raise ValueError(f"'{name}' is a special attribute and does not "
f"support multi-property. It is already set "
f"and cannot be overwritten using this method. "
f"Please use direct assignment for setting this "
f"property after the first time.")
# ToDo: Implement the same behaviour for special attribute ids,
# https://gitlab.indiscale.com/caosdb/src/caosdb-pylib/-/issues/219
new_property = Property(name=name, id=id, description=description, datatype=datatype,
value=value, unit=unit)
Loading