diff --git a/src/caosdb/common/datatype.py b/src/caosdb/common/datatype.py index eb8c1e4e0088f1924940a104ec3916b9d5d40f99..051fcad51d0bbf2ec294ac33ee26d8132a1137d5 100644 --- a/src/caosdb/common/datatype.py +++ b/src/caosdb/common/datatype.py @@ -60,10 +60,21 @@ def is_list_datatype(datatype): def is_reference(datatype): - """ returns whether the value is a reference + """Returns whether the value is a reference FILE and REFERENCE properties are examples, but also datatypes that are - RecordTypes + RecordTypes. + + Parameters + ---------- + datatype : str + The datatype to check. + + Returns + ------- + bool + True if the datatype is a not base datatype or a list of a base datatype. + Otherwise False is returned. """ if datatype in [DOUBLE, BOOLEAN, INTEGER, TEXT, DATETIME]: @@ -73,6 +84,39 @@ def is_reference(datatype): else: return True +def get_referenced_recordtype(datatype): + """Return the record type of the referenced datatype. + + Raises + ------ + ValueError + In cases where datatype is not a reference, the list does not have + a referenced record type or the datatype is a FILE. + + Parameters + ---------- + datatype : str + The datatype to check. + + Returns + ------- + str + String containing the name of the referenced datatype. + """ + + if not is_reference(datatype): + raise ValueError("datatype must be a reference") + + if is_list_datatype(datatype): + datatype = get_list_datatype(datatype) + if datatype is None: + raise ValueError("list does not have a list datatype") + + if datatype == FILE: + raise ValueError("FILE references are not considered references with a record type") + + return datatype + def get_id_of_datatype(datatype): """ returns the id of a Record Type