mirror of https://github.com/veops/cmdb.git
pref(api): error tips for out of range value (#453)
This commit is contained in:
parent
702d8d65f0
commit
6cda354c21
|
@ -331,14 +331,14 @@ class AutoDiscoveryCICRUD(DBMixin):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_attributes_by_type_id(type_id):
|
def get_attributes_by_type_id(type_id):
|
||||||
from api.lib.cmdb.ci_type import CITypeAttributeManager
|
from api.lib.cmdb.ci_type import CITypeAttributeManager
|
||||||
attributes = [i[1] for i in CITypeAttributeManager.get_all_attributes(type_id) or []]
|
attributes = [i for i in CITypeAttributeManager.get_attributes_by_type_id(type_id) or []]
|
||||||
|
|
||||||
attr_names = set()
|
attr_names = set()
|
||||||
adts = AutoDiscoveryCITypeCRUD.get_by_type_id(type_id)
|
adts = AutoDiscoveryCITypeCRUD.get_by_type_id(type_id)
|
||||||
for adt in adts:
|
for adt in adts:
|
||||||
attr_names |= set((adt.attributes or {}).values())
|
attr_names |= set((adt.attributes or {}).values())
|
||||||
|
|
||||||
return [attr.to_dict() for attr in attributes if attr.name in attr_names]
|
return [attr for attr in attributes if attr['name'] in attr_names]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def search(cls, page, page_size, fl=None, **kwargs):
|
def search(cls, page, page_size, fl=None, **kwargs):
|
||||||
|
|
|
@ -96,7 +96,7 @@ class ErrFormat(CommonErrFormat):
|
||||||
# 属性 {} 的值必须是唯一的, 当前值 {} 已存在
|
# 属性 {} 的值必须是唯一的, 当前值 {} 已存在
|
||||||
attribute_value_unique_required = _l("The value of attribute {} must be unique, {} already exists")
|
attribute_value_unique_required = _l("The value of attribute {} must be unique, {} already exists")
|
||||||
attribute_value_required = _l("Attribute {} value must exist") # 属性 {} 值必须存在
|
attribute_value_required = _l("Attribute {} value must exist") # 属性 {} 值必须存在
|
||||||
|
attribute_value_out_of_range = _l("Out of range value, the maximum value is 2147483647")
|
||||||
# 新增或者修改属性值未知错误: {}
|
# 新增或者修改属性值未知错误: {}
|
||||||
attribute_value_unknown_error = _l("Unknown error when adding or modifying attribute value: {}")
|
attribute_value_unknown_error = _l("Unknown error when adding or modifying attribute value: {}")
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,21 @@ import six
|
||||||
import api.models.cmdb as model
|
import api.models.cmdb as model
|
||||||
from api.lib.cmdb.cache import AttributeCache
|
from api.lib.cmdb.cache import AttributeCache
|
||||||
from api.lib.cmdb.const import ValueTypeEnum
|
from api.lib.cmdb.const import ValueTypeEnum
|
||||||
|
from api.lib.cmdb.resp_format import ErrFormat
|
||||||
|
|
||||||
TIME_RE = re.compile(r'(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d')
|
TIME_RE = re.compile(r'(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d')
|
||||||
|
|
||||||
|
|
||||||
|
class ValueDeserializeError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def string2int(x):
|
def string2int(x):
|
||||||
return int(float(x))
|
v = int(float(x))
|
||||||
|
if v > 2147483647:
|
||||||
|
raise ValueDeserializeError(ErrFormat.attribute_value_out_of_range)
|
||||||
|
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
def str2datetime(x):
|
def str2datetime(x):
|
||||||
|
|
|
@ -5,14 +5,16 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
import imp
|
import imp
|
||||||
import jinja2
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
import jinja2
|
||||||
from flask import abort
|
from flask import abort
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from jinja2schema import infer
|
from jinja2schema import infer
|
||||||
from jinja2schema import to_json_schema
|
from jinja2schema import to_json_schema
|
||||||
|
from werkzeug.exceptions import BadRequest
|
||||||
|
|
||||||
from api.extensions import db
|
from api.extensions import db
|
||||||
from api.lib.cmdb.attribute import AttributeManager
|
from api.lib.cmdb.attribute import AttributeManager
|
||||||
|
@ -23,6 +25,7 @@ from api.lib.cmdb.const import ValueTypeEnum
|
||||||
from api.lib.cmdb.history import AttributeHistoryManger
|
from api.lib.cmdb.history import AttributeHistoryManger
|
||||||
from api.lib.cmdb.resp_format import ErrFormat
|
from api.lib.cmdb.resp_format import ErrFormat
|
||||||
from api.lib.cmdb.utils import TableMap
|
from api.lib.cmdb.utils import TableMap
|
||||||
|
from api.lib.cmdb.utils import ValueDeserializeError
|
||||||
from api.lib.cmdb.utils import ValueTypeMap
|
from api.lib.cmdb.utils import ValueTypeMap
|
||||||
from api.lib.utils import handle_arg_list
|
from api.lib.utils import handle_arg_list
|
||||||
from api.models.cmdb import CI
|
from api.models.cmdb import CI
|
||||||
|
@ -80,7 +83,7 @@ class AttributeValueManager(object):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _deserialize_value(value_type, value):
|
def _deserialize_value(alias, value_type, value):
|
||||||
if not value:
|
if not value:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@ -88,6 +91,8 @@ class AttributeValueManager(object):
|
||||||
try:
|
try:
|
||||||
v = deserialize(value)
|
v = deserialize(value)
|
||||||
return v
|
return v
|
||||||
|
except ValueDeserializeError as e:
|
||||||
|
return abort(400, ErrFormat.attribute_value_invalid2.format(alias, e))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return abort(400, ErrFormat.attribute_value_invalid.format(value))
|
return abort(400, ErrFormat.attribute_value_invalid.format(value))
|
||||||
|
|
||||||
|
@ -124,7 +129,7 @@ class AttributeValueManager(object):
|
||||||
|
|
||||||
def _validate(self, attr, value, value_table, ci=None, type_id=None, ci_id=None, type_attr=None):
|
def _validate(self, attr, value, value_table, ci=None, type_id=None, ci_id=None, type_attr=None):
|
||||||
ci = ci or {}
|
ci = ci or {}
|
||||||
v = self._deserialize_value(attr.value_type, value)
|
v = self._deserialize_value(attr.alias, attr.value_type, value)
|
||||||
|
|
||||||
attr.is_choice and value and self._check_is_choice(attr, attr.value_type, v)
|
attr.is_choice and value and self._check_is_choice(attr, attr.value_type, v)
|
||||||
attr.is_unique and self._check_is_unique(
|
attr.is_unique and self._check_is_unique(
|
||||||
|
@ -240,6 +245,8 @@ class AttributeValueManager(object):
|
||||||
value = self._validate(attr, value, value_table, ci=None, type_id=type_id, ci_id=ci_id,
|
value = self._validate(attr, value, value_table, ci=None, type_id=type_id, ci_id=ci_id,
|
||||||
type_attr=ci_attr2type_attr.get(attr.id))
|
type_attr=ci_attr2type_attr.get(attr.id))
|
||||||
ci_dict[key] = value
|
ci_dict[key] = value
|
||||||
|
except BadRequest as e:
|
||||||
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
current_app.logger.warning(str(e))
|
current_app.logger.warning(str(e))
|
||||||
|
|
||||||
|
|
Binary file not shown.
|
@ -7,7 +7,7 @@ msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: PROJECT VERSION\n"
|
"Project-Id-Version: PROJECT VERSION\n"
|
||||||
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
|
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
|
||||||
"POT-Creation-Date: 2024-03-01 13:49+0800\n"
|
"POT-Creation-Date: 2024-03-29 10:42+0800\n"
|
||||||
"PO-Revision-Date: 2023-12-25 20:21+0800\n"
|
"PO-Revision-Date: 2023-12-25 20:21+0800\n"
|
||||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||||
"Language: zh\n"
|
"Language: zh\n"
|
||||||
|
@ -322,7 +322,7 @@ msgstr "无效的属性值: {}"
|
||||||
|
|
||||||
#: api/lib/cmdb/resp_format.py:94
|
#: api/lib/cmdb/resp_format.py:94
|
||||||
msgid "{} Invalid value: {}"
|
msgid "{} Invalid value: {}"
|
||||||
msgstr "无效的值: {}"
|
msgstr "{} 无效的值: {}"
|
||||||
|
|
||||||
#: api/lib/cmdb/resp_format.py:95
|
#: api/lib/cmdb/resp_format.py:95
|
||||||
msgid "{} is not in the predefined values"
|
msgid "{} is not in the predefined values"
|
||||||
|
@ -336,6 +336,10 @@ msgstr "属性 {} 的值必须是唯一的, 当前值 {} 已存在"
|
||||||
msgid "Attribute {} value must exist"
|
msgid "Attribute {} value must exist"
|
||||||
msgstr "属性 {} 值必须存在"
|
msgstr "属性 {} 值必须存在"
|
||||||
|
|
||||||
|
#: api/lib/cmdb/resp_format.py:99
|
||||||
|
msgid "Out of range value, the maximum value is 2147483647"
|
||||||
|
msgstr "超过最大值限制, 最大值是2147483647"
|
||||||
|
|
||||||
#: api/lib/cmdb/resp_format.py:101
|
#: api/lib/cmdb/resp_format.py:101
|
||||||
msgid "Unknown error when adding or modifying attribute value: {}"
|
msgid "Unknown error when adding or modifying attribute value: {}"
|
||||||
msgstr "新增或者修改属性值未知错误: {}"
|
msgstr "新增或者修改属性值未知错误: {}"
|
||||||
|
|
Loading…
Reference in New Issue