From 7b96ac46386d0e9ca857eb9c5b41a71ad867ec5b Mon Sep 17 00:00:00 2001 From: pycook Date: Wed, 23 Oct 2019 15:05:33 +0800 Subject: [PATCH] attribute alias must be unique --- api/lib/cmdb/attribute.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/api/lib/cmdb/attribute.py b/api/lib/cmdb/attribute.py index 1d2fe3f..efe235c 100644 --- a/api/lib/cmdb/attribute.py +++ b/api/lib/cmdb/attribute.py @@ -96,7 +96,8 @@ class AttributeManager(object): name = kwargs.pop("name") alias = kwargs.pop("alias", "") alias = name if not alias else alias - Attribute.get_by(name=name, first=True) and abort(400, "attribute {0} is already existed".format(name)) + Attribute.get_by(name=name, first=True) and abort(400, "attribute name <{0}> is already existed".format(name)) + Attribute.get_by(alias=alias, first=True) and abort(400, "attribute alias <{0}> is already existed".format(name)) attr = Attribute.create(flush=True, name=name, @@ -121,6 +122,15 @@ class AttributeManager(object): def update(self, _id, **kwargs): attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id)) + if kwargs.get("name"): + other = Attribute.get_by(name=kwargs['name'], first=True, to_dict=False) + if other and other.id != attr.id: + return abort(400, "Attribute name <{0}> cannot be duplicate!".format(kwargs['name'])) + if kwargs.get("alias"): + other = Attribute.get_by(alias=kwargs['alias'], first=True, to_dict=False) + if other and other.id != attr.id: + return abort(400, "Attribute alias <{0}> cannot be duplicate!".format(kwargs['alias'])) + choice_value = kwargs.pop("choice_value", False) is_choice = True if choice_value else False