From 317a16aa21f03f792d65eea6bfe3e9d6265b31e4 Mon Sep 17 00:00:00 2001 From: pycook Date: Mon, 23 Dec 2019 18:51:33 +0800 Subject: [PATCH] support JSON type --- cmdb-api/api/lib/cmdb/const.py | 1 + cmdb-api/api/lib/cmdb/history.py | 6 ++++-- cmdb-api/api/lib/cmdb/utils.py | 15 ++++++++++--- cmdb-api/api/lib/utils.py | 5 +++++ cmdb-api/api/models/cmdb.py | 11 ++++++++++ cmdb-api/api/views/cmdb/ci.py | 2 +- cmdb-ui/.env | 2 +- cmdb-ui/src/views/cmdb/ci/index.vue | 4 +++- .../cmdb/ci/modules/CreateInstanceForm.vue | 3 ++- .../views/cmdb/ci/modules/EditableCell.vue | 21 ++++++++++++++++--- .../src/views/cmdb/ci/modules/SearchForm.vue | 3 ++- .../cmdb/modeling/attributes/module/const.js | 3 ++- 12 files changed, 62 insertions(+), 14 deletions(-) diff --git a/cmdb-api/api/lib/cmdb/const.py b/cmdb-api/api/lib/cmdb/const.py index e5bb8a7..323057f 100644 --- a/cmdb-api/api/lib/cmdb/const.py +++ b/cmdb-api/api/lib/cmdb/const.py @@ -11,6 +11,7 @@ class ValueTypeEnum(BaseEnum): DATETIME = "3" DATE = "4" TIME = "5" + JSON = "6" class CIStatusEnum(BaseEnum): diff --git a/cmdb-api/api/lib/cmdb/history.py b/cmdb-api/api/lib/cmdb/history.py index de820d0..f8394c0 100644 --- a/cmdb-api/api/lib/cmdb/history.py +++ b/cmdb-api/api/lib/cmdb/history.py @@ -1,6 +1,8 @@ # -*- coding:utf-8 -*- +import json + from flask import abort from flask import g @@ -105,8 +107,8 @@ class AttributeHistoryManger(object): for attr_id, operate_type, old, new in history_list or []: AttributeHistory.create(attr_id=attr_id, operate_type=operate_type, - old=old, - new=new, + old=json.dumps(old) if isinstance(old, (dict, list)) else old, + new=json.dumps(new) if isinstance(new, (dict, list)) else new, ci_id=ci_id, record_id=record.id) diff --git a/cmdb-api/api/lib/cmdb/utils.py b/cmdb-api/api/lib/cmdb/utils.py index f07ae2d..2f12abb 100644 --- a/cmdb-api/api/lib/cmdb/utils.py +++ b/cmdb-api/api/lib/cmdb/utils.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import datetime +import json import six from markupsafe import escape @@ -33,15 +34,17 @@ class ValueTypeMap(object): ValueTypeEnum.TIME: lambda x: escape(x).encode('utf-8').decode('utf-8'), ValueTypeEnum.DATETIME: str2datetime, ValueTypeEnum.DATE: str2datetime, + ValueTypeEnum.JSON: lambda x: json.loads(x) if isinstance(x, six.string_types) else x, } serialize = { ValueTypeEnum.INT: int, ValueTypeEnum.FLOAT: float, - ValueTypeEnum.TEXT: lambda x: x if isinstance(x, six.text_type) else str(x), - ValueTypeEnum.TIME: lambda x: x if isinstance(x, six.text_type) else str(x), + ValueTypeEnum.TEXT: lambda x: x if isinstance(x, six.string_types) else str(x), + ValueTypeEnum.TIME: lambda x: x if isinstance(x, six.string_types) else str(x), ValueTypeEnum.DATE: lambda x: x.strftime("%Y-%m-%d"), ValueTypeEnum.DATETIME: lambda x: x.strftime("%Y-%m-%d %H:%M:%S"), + ValueTypeEnum.JSON: lambda x: json.loads(x) if isinstance(x, six.string_types) else x, } serialize2 = { @@ -51,6 +54,7 @@ class ValueTypeMap(object): ValueTypeEnum.TIME: lambda x: x.decode() if not isinstance(x, six.string_types) else x, ValueTypeEnum.DATE: lambda x: x.decode() if not isinstance(x, six.string_types) else x, ValueTypeEnum.DATETIME: lambda x: x.decode() if not isinstance(x, six.string_types) else x, + ValueTypeEnum.JSON: lambda x: json.loads(x) if isinstance(x, six.string_types) else x, } choice = { @@ -66,12 +70,14 @@ class ValueTypeMap(object): ValueTypeEnum.DATE: model.CIValueDateTime, ValueTypeEnum.TIME: model.CIValueText, ValueTypeEnum.FLOAT: model.CIValueFloat, + ValueTypeEnum.JSON: model.CIValueJson, 'index_{0}'.format(ValueTypeEnum.INT): model.CIIndexValueInteger, 'index_{0}'.format(ValueTypeEnum.TEXT): model.CIIndexValueText, 'index_{0}'.format(ValueTypeEnum.DATETIME): model.CIIndexValueDateTime, 'index_{0}'.format(ValueTypeEnum.DATE): model.CIIndexValueDateTime, 'index_{0}'.format(ValueTypeEnum.TIME): model.CIIndexValueText, 'index_{0}'.format(ValueTypeEnum.FLOAT): model.CIIndexValueFloat, + 'index_{0}'.format(ValueTypeEnum.JSON): model.CIValueJson, } table_name = { @@ -81,12 +87,14 @@ class ValueTypeMap(object): ValueTypeEnum.DATE: 'c_value_datetime', ValueTypeEnum.TIME: 'c_value_texts', ValueTypeEnum.FLOAT: 'c_value_floats', + ValueTypeEnum.JSON: 'c_value_json', 'index_{0}'.format(ValueTypeEnum.INT): 'c_value_index_integers', 'index_{0}'.format(ValueTypeEnum.TEXT): 'c_value_index_texts', 'index_{0}'.format(ValueTypeEnum.DATETIME): 'c_value_index_datetime', 'index_{0}'.format(ValueTypeEnum.DATE): 'c_value_index_datetime', 'index_{0}'.format(ValueTypeEnum.TIME): 'c_value_index_texts', 'index_{0}'.format(ValueTypeEnum.FLOAT): 'c_value_index_floats', + 'index_{0}'.format(ValueTypeEnum.JSON): 'c_value_json', } es_type = { @@ -95,7 +103,8 @@ class ValueTypeMap(object): ValueTypeEnum.DATETIME: 'text', ValueTypeEnum.DATE: 'text', ValueTypeEnum.TIME: 'text', - ValueTypeEnum.FLOAT: 'float' + ValueTypeEnum.FLOAT: 'float', + ValueTypeEnum.JSON: 'object' } diff --git a/cmdb-api/api/lib/utils.py b/cmdb-api/api/lib/utils.py index 4aa3b7b..3798a22 100644 --- a/cmdb-api/api/lib/utils.py +++ b/cmdb-api/api/lib/utils.py @@ -1,5 +1,7 @@ # -*- coding:utf-8 -*- +import json + import redis import six from elasticsearch import Elasticsearch @@ -26,6 +28,9 @@ def get_page_size(page_size): def handle_arg_list(arg): + if isinstance(arg, six.string_types) and arg.startswith('['): + return json.loads(arg) + return list(filter(lambda x: x != "", arg.strip().split(","))) if isinstance(arg, six.string_types) else arg diff --git a/cmdb-api/api/models/cmdb.py b/cmdb-api/api/models/cmdb.py index 5b8dec9..31a22dc 100644 --- a/cmdb-api/api/models/cmdb.py +++ b/cmdb-api/api/models/cmdb.py @@ -250,6 +250,17 @@ class CIValueDateTime(Model): attr = db.relationship("Attribute", backref="c_value_datetime.attr_id") +class CIValueJson(Model): + __tablename__ = "c_value_json" + + ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False) + attr_id = db.Column(db.Integer, db.ForeignKey('c_attributes.id'), nullable=False) + value = db.Column(db.JSON, nullable=False) + + ci = db.relationship("CI", backref="c_value_json.ci_id") + attr = db.relationship("Attribute", backref="c_value_json.attr_id") + + # history class OperationRecord(Model): __tablename__ = "c_records" diff --git a/cmdb-api/api/views/cmdb/ci.py b/cmdb-api/api/views/cmdb/ci.py index 0e9ed9b..e5c7606 100644 --- a/cmdb-api/api/views/cmdb/ci.py +++ b/cmdb-api/api/views/cmdb/ci.py @@ -73,7 +73,7 @@ class CIView(APIView): ci_dict[k] = v.strip() if isinstance(v, six.string_types) else v return ci_dict - @has_perm_from_args("ci_type", ResourceTypeEnum.CI, PermEnum.ADD) + @has_perm_from_args("ci_type", ResourceTypeEnum.CI, PermEnum.ADD, lambda x: CITypeCache.get(x).name) def post(self): ci_type = request.values.get("ci_type") _no_attribute_policy = request.values.get("_no_attribute_policy", ExistPolicy.IGNORE) diff --git a/cmdb-ui/.env b/cmdb-ui/.env index 580d9f3..f98cf96 100644 --- a/cmdb-ui/.env +++ b/cmdb-ui/.env @@ -1,3 +1,3 @@ NODE_ENV=production VUE_APP_PREVIEW=false -VUE_APP_API_BASE_URL=http://127.0.0.1:5000/api +VUE_APP_API_BASE_URL=http://127.0.0.1:5001/api diff --git a/cmdb-ui/src/views/cmdb/ci/index.vue b/cmdb-ui/src/views/cmdb/ci/index.vue index 87b8dfc..301ec12 100644 --- a/cmdb-ui/src/views/cmdb/ci/index.vue +++ b/cmdb-ui/src/views/cmdb/ci/index.vue @@ -94,7 +94,8 @@ var valueTypeMap = { '2': 'text', '3': 'datetime', '4': 'date', - '5': 'time' + '5': 'time', + '6': 'json' } export default { @@ -270,6 +271,7 @@ export default { const col = {} col.title = item.alias col.dataIndex = item.name + col.value_type = item.value_type if (index !== prefAttrList.length - 1) { col.width = 100 } diff --git a/cmdb-ui/src/views/cmdb/ci/modules/CreateInstanceForm.vue b/cmdb-ui/src/views/cmdb/ci/modules/CreateInstanceForm.vue index 0a46b20..2a28348 100644 --- a/cmdb-ui/src/views/cmdb/ci/modules/CreateInstanceForm.vue +++ b/cmdb-ui/src/views/cmdb/ci/modules/CreateInstanceForm.vue @@ -61,7 +61,8 @@ var valueTypeMap = { '2': 'text', '3': 'datetime', '4': 'date', - '5': 'time' + '5': 'time', + '6': 'json' } export default { diff --git a/cmdb-ui/src/views/cmdb/ci/modules/EditableCell.vue b/cmdb-ui/src/views/cmdb/ci/modules/EditableCell.vue index 47c19a9..0ff561e 100644 --- a/cmdb-ui/src/views/cmdb/ci/modules/EditableCell.vue +++ b/cmdb-ui/src/views/cmdb/ci/modules/EditableCell.vue @@ -1,7 +1,7 @@