def add_key(key, key_type, value_type=None, value_regexp=None, session=None):
"""
Adds a new allowed key.
:param key: the name for the new key.
:param key_type: the type of the key: all(container, dataset, file), collection(dataset or container), file, derived(compute from file for collection).
:param value_type: the type of the value, if defined.
:param value_regexp: the regular expression that values should match, if defined.
:param session: The database session in use.
"""
# Check if value_type is supported
if value_type and value_type not in [str(t) for t in AUTHORIZED_VALUE_TYPES]:
raise UnsupportedValueType('The type \'%(value_type)s\' is not supported for values!' % locals())
new_key = models.DIDKey(key=key, value_type=value_type and str(value_type), value_regexp=value_regexp, key_type=key_type)
try:
new_key.save(session=session)
except IntegrityError as error:
if error.args[0] == "(IntegrityError) column key is not unique":
raise Duplicate('key \'%(key)s\' already exists!' % locals())
raise
评论列表
文章目录