mirror of
https://github.com/veops/cmdb.git
synced 2025-08-22 11:42:30 +08:00
code format
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
from flask import current_app
|
||||
from flask import abort
|
||||
from flask import current_app
|
||||
|
||||
from api.extensions import db
|
||||
from api.models.cmdb import Attribute
|
||||
from api.models.cmdb import CITypeAttribute
|
||||
from api.models.cmdb import PreferenceShowAttributes
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.lib.cmdb.const import type_map
|
||||
from api.lib.decorator import kwargs_required
|
||||
from api.models.cmdb import Attribute
|
||||
from api.models.cmdb import CITypeAttribute
|
||||
from api.models.cmdb import PreferenceShowAttributes
|
||||
|
||||
|
||||
class AttributeManager(object):
|
||||
@@ -97,7 +97,8 @@ class AttributeManager(object):
|
||||
alias = kwargs.pop("alias", "")
|
||||
alias = name if not alias else alias
|
||||
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))
|
||||
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,
|
||||
|
@@ -10,73 +10,84 @@ from api.models.cmdb import RelationType
|
||||
|
||||
|
||||
class AttributeCache(object):
|
||||
PREFIX_ID = 'Field::ID::{0}'
|
||||
PREFIX_NAME = 'Field::Name::{0}'
|
||||
PREFIX_ALIAS = 'Field::Alias::{0}'
|
||||
|
||||
@classmethod
|
||||
def get(cls, key):
|
||||
if key is None:
|
||||
return
|
||||
attr = cache.get('Field::Name::{0}'.format(key)) \
|
||||
or cache.get('Field::ID::{0}'.format(key)) \
|
||||
or cache.get('Field::Alias::{0}'.format(key))
|
||||
attr = cache.get(cls.PREFIX_NAME.format(key))
|
||||
attr = attr or cache.get(cls.PREFIX_ID.format(key))
|
||||
attr = attr or cache.get(cls.PREFIX_ALIAS.format(key))
|
||||
|
||||
if attr is None:
|
||||
attr = Attribute.get_by(name=key, first=True, to_dict=False) \
|
||||
or Attribute.get_by_id(key) \
|
||||
or Attribute.get_by(alias=key, first=True, to_dict=False)
|
||||
attr = Attribute.get_by(name=key, first=True, to_dict=False)
|
||||
attr = attr or Attribute.get_by_id(key)
|
||||
attr = attr or Attribute.get_by(alias=key, first=True, to_dict=False)
|
||||
if attr is not None:
|
||||
cls.set(attr)
|
||||
return attr
|
||||
|
||||
@classmethod
|
||||
def set(cls, attr):
|
||||
cache.set('Field::ID::{0}'.format(attr.id), attr)
|
||||
cache.set('Field::Name::{0}'.format(attr.name), attr)
|
||||
cache.set('Field::Alias::{0}'.format(attr.alias), attr)
|
||||
cache.set(cls.PREFIX_ID.format(attr.id), attr)
|
||||
cache.set(cls.PREFIX_NAME.format(attr.name), attr)
|
||||
cache.set(cls.PREFIX_ALIAS.format(attr.alias), attr)
|
||||
|
||||
@classmethod
|
||||
def clean(cls, attr):
|
||||
cache.delete('Field::ID::{0}'.format(attr.id))
|
||||
cache.delete('Field::Name::{0}'.format(attr.name))
|
||||
cache.delete('Field::Alias::{0}'.format(attr.alias))
|
||||
cache.delete(cls.PREFIX_ID.format(attr.id))
|
||||
cache.delete(cls.PREFIX_NAME.format(attr.name))
|
||||
cache.delete(cls.PREFIX_ALIAS.format(attr.alias))
|
||||
|
||||
|
||||
class CITypeCache(object):
|
||||
PREFIX_ID = "CIType::ID::{0}"
|
||||
PREFIX_NAME = "CIType::Name::{0}"
|
||||
PREFIX_ALIAS = "CIType::Alias::{0}"
|
||||
|
||||
@classmethod
|
||||
def get(cls, key):
|
||||
if key is None:
|
||||
return
|
||||
ct = cache.get("CIType::ID::{0}".format(key)) or \
|
||||
cache.get("CIType::Name::{0}".format(key)) or \
|
||||
cache.get("CIType::Alias::{0}".format(key))
|
||||
ct = cache.get(cls.PREFIX_NAME.format(key))
|
||||
ct = ct or cache.get(cls.PREFIX_ID.format(key))
|
||||
ct = ct or cache.get(cls.PREFIX_ALIAS.format(key))
|
||||
if ct is None:
|
||||
ct = CIType.get_by(name=key, first=True, to_dict=False) or \
|
||||
CIType.get_by_id(key) or \
|
||||
CIType.get_by(alias=key, first=True, to_dict=False)
|
||||
ct = CIType.get_by(name=key, first=True, to_dict=False)
|
||||
ct = ct or CIType.get_by_id(key)
|
||||
ct = ct or CIType.get_by(alias=key, first=True, to_dict=False)
|
||||
if ct is not None:
|
||||
cls.set(ct)
|
||||
return ct
|
||||
|
||||
@classmethod
|
||||
def set(cls, ct):
|
||||
cache.set("CIType::Name::{0}".format(ct.name), ct)
|
||||
cache.set("CIType::ID::{0}".format(ct.id), ct)
|
||||
cache.set("CIType::Alias::{0}".format(ct.alias), ct)
|
||||
cache.set(cls.PREFIX_NAME.format(ct.name), ct)
|
||||
cache.set(cls.PREFIX_ID.format(ct.id), ct)
|
||||
cache.set(cls.PREFIX_ALIAS.format(ct.alias), ct)
|
||||
|
||||
@classmethod
|
||||
def clean(cls, key):
|
||||
ct = cls.get(key)
|
||||
if ct is not None:
|
||||
cache.delete("CIType::Name::{0}".format(ct.name))
|
||||
cache.delete("CIType::ID::{0}".format(ct.id))
|
||||
cache.delete("CIType::Alias::{0}".format(ct.alias))
|
||||
cache.delete(cls.PREFIX_NAME.format(ct.name))
|
||||
cache.delete(cls.PREFIX_ID.format(ct.id))
|
||||
cache.delete(cls.PREFIX_ALIAS.format(ct.alias))
|
||||
|
||||
|
||||
class RelationTypeCache(object):
|
||||
PREFIX_ID = "RelationType::ID::{0}"
|
||||
PREFIX_NAME = "RelationType::Name::{0}"
|
||||
|
||||
@classmethod
|
||||
def get(cls, key):
|
||||
if key is None:
|
||||
return
|
||||
ct = cache.get("RelationType::ID::{0}".format(key)) or \
|
||||
cache.get("RelationType::Name::{0}".format(key))
|
||||
ct = cache.get(cls.PREFIX_NAME.format(key))
|
||||
ct = ct or cache.get(cls.PREFIX_ID.format(key))
|
||||
if ct is None:
|
||||
ct = RelationType.get_by(name=key, first=True, to_dict=False) or RelationType.get_by_id(key)
|
||||
if ct is not None:
|
||||
@@ -85,15 +96,15 @@ class RelationTypeCache(object):
|
||||
|
||||
@classmethod
|
||||
def set(cls, ct):
|
||||
cache.set("RelationType::Name::{0}".format(ct.name), ct)
|
||||
cache.set("RelationType::ID::{0}".format(ct.id), ct)
|
||||
cache.set(cls.PREFIX_NAME.format(ct.name), ct)
|
||||
cache.set(cls.PREFIX_ID.format(ct.id), ct)
|
||||
|
||||
@classmethod
|
||||
def clean(cls, key):
|
||||
ct = cls.get(key)
|
||||
if ct is not None:
|
||||
cache.delete("RelationType::Name::{0}".format(ct.name))
|
||||
cache.delete("RelationType::ID::{0}".format(ct.id))
|
||||
cache.delete(cls.PREFIX_NAME.format(ct.name))
|
||||
cache.delete(cls.PREFIX_ID.format(ct.id))
|
||||
|
||||
|
||||
class CITypeAttributeCache(object):
|
||||
@@ -101,13 +112,16 @@ class CITypeAttributeCache(object):
|
||||
key is type_id or type_name
|
||||
"""
|
||||
|
||||
PREFIX_ID = "CITypeAttribute::ID::{0}"
|
||||
PREFIX_NAME = "CITypeAttribute::Name::{0}"
|
||||
|
||||
@classmethod
|
||||
def get(cls, key):
|
||||
if key is None:
|
||||
return
|
||||
|
||||
attrs = cache.get("CITypeAttribute::Name::{0}".format(key)) \
|
||||
or cache.get("CITypeAttribute::ID::{0}".format(key))
|
||||
attrs = cache.get(cls.PREFIX_NAME.format(key))
|
||||
attrs = attrs or cache.get(cls.PREFIX_ID.format(key))
|
||||
if not attrs:
|
||||
attrs = CITypeAttribute.get_by(type_id=key, to_dict=False)
|
||||
if not attrs:
|
||||
@@ -122,13 +136,13 @@ class CITypeAttributeCache(object):
|
||||
def set(cls, key, values):
|
||||
ci_type = CITypeCache.get(key)
|
||||
if ci_type is not None:
|
||||
cache.set("CITypeAttribute::ID::{0}".format(ci_type.id), values)
|
||||
cache.set("CITypeAttribute::Name::{0}".format(ci_type.name), values)
|
||||
cache.set(cls.PREFIX_ID.format(ci_type.id), values)
|
||||
cache.set(cls.PREFIX_NAME.format(ci_type.name), values)
|
||||
|
||||
@classmethod
|
||||
def clean(cls, key):
|
||||
ci_type = CITypeCache.get(key)
|
||||
attrs = cls.get(key)
|
||||
if attrs is not None and ci_type:
|
||||
cache.delete("CITypeAttribute::ID::{0}".format(ci_type.id))
|
||||
cache.delete("CITypeAttribute::Name::{0}".format(ci_type.name))
|
||||
cache.delete(cls.PREFIX_ID.format(ci_type.id))
|
||||
cache.delete(cls.PREFIX_NAME.format(ci_type.name))
|
||||
|
@@ -167,10 +167,10 @@ class CIManager(object):
|
||||
|
||||
unique_key = AttributeCache.get(ci_type.unique_id) or abort(400, 'illegality unique attribute')
|
||||
|
||||
unique_value = ci_dict.get(unique_key.name) or \
|
||||
ci_dict.get(unique_key.alias) or \
|
||||
ci_dict.get(unique_key.id) or \
|
||||
abort(400, '{0} missing'.format(unique_key.name))
|
||||
unique_value = ci_dict.get(unique_key.name)
|
||||
unique_value = unique_value or ci_dict.get(unique_key.alias)
|
||||
unique_value = unique_value or ci_dict.get(unique_key.id)
|
||||
unique_value = unique_value or abort(400, '{0} missing'.format(unique_key.name))
|
||||
|
||||
existed = cls.ci_is_exist(unique_key, unique_value)
|
||||
if existed is not None:
|
||||
@@ -425,8 +425,8 @@ class CIRelationManager(object):
|
||||
|
||||
def get_second_cis(self, first_ci_id, relation_type_id=None, page=1, per_page=None, **kwargs):
|
||||
second_cis = db.session.query(CI.id).filter(CI.deleted.is_(False)).join(
|
||||
CIRelation, CIRelation.second_ci_id == CI.id).filter(
|
||||
CIRelation.first_ci_id == first_ci_id)
|
||||
CIRelation, CIRelation.second_ci_id == CI.id).filter(
|
||||
CIRelation.first_ci_id == first_ci_id)
|
||||
|
||||
if relation_type_id is not None:
|
||||
second_cis = second_cis.filter(CIRelation.relation_type_id == relation_type_id)
|
||||
|
@@ -1,21 +1,21 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
|
||||
from flask import current_app
|
||||
from flask import abort
|
||||
from flask import current_app
|
||||
|
||||
from api.models.cmdb import CITypeAttribute
|
||||
from api.models.cmdb import CIType
|
||||
from api.models.cmdb import CITypeGroup
|
||||
from api.models.cmdb import CITypeGroupItem
|
||||
from api.models.cmdb import CITypeRelation
|
||||
from api.models.cmdb import CITypeAttributeGroup
|
||||
from api.models.cmdb import CITypeAttributeGroupItem
|
||||
from api.lib.cmdb.attribute import AttributeManager
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.lib.cmdb.cache import CITypeAttributeCache
|
||||
from api.lib.cmdb.cache import CITypeCache
|
||||
from api.lib.cmdb.attribute import AttributeManager
|
||||
from api.lib.decorator import kwargs_required
|
||||
from api.models.cmdb import CIType
|
||||
from api.models.cmdb import CITypeAttribute
|
||||
from api.models.cmdb import CITypeAttributeGroup
|
||||
from api.models.cmdb import CITypeAttributeGroupItem
|
||||
from api.models.cmdb import CITypeGroup
|
||||
from api.models.cmdb import CITypeGroupItem
|
||||
from api.models.cmdb import CITypeRelation
|
||||
|
||||
|
||||
class CITypeManager(object):
|
||||
@@ -54,8 +54,7 @@ class CITypeManager(object):
|
||||
unique_key = kwargs.pop("unique_key", None)
|
||||
unique_key = AttributeCache.get(unique_key) or abort(404, "Unique key is not defined")
|
||||
|
||||
CIType.get_by(name=kwargs['name'], first=True) and \
|
||||
abort(404, "CIType <{0}> is already existed".format(kwargs.get("name")))
|
||||
CIType.get_by(name=kwargs['name']) and abort(404, "CIType <{0}> is already existed".format(kwargs.get("name")))
|
||||
|
||||
kwargs["alias"] = kwargs["name"] if not kwargs.get("alias") else kwargs["alias"]
|
||||
|
||||
@@ -349,8 +348,8 @@ class CITypeAttributeGroupManager(object):
|
||||
:param attr_order:
|
||||
:return:
|
||||
"""
|
||||
existed = CITypeAttributeGroup.get_by(type_id=type_id, name=name, first=True, to_dict=False) \
|
||||
or CITypeAttributeGroup.create(type_id=type_id, name=name, order=group_order)
|
||||
existed = CITypeAttributeGroup.get_by(type_id=type_id, name=name, first=True, to_dict=False)
|
||||
existed = existed or CITypeAttributeGroup.create(type_id=type_id, name=name, order=group_order)
|
||||
existed.update(order=group_order)
|
||||
attr_order = dict(attr_order)
|
||||
current_app.logger.info(attr_order)
|
||||
@@ -382,7 +381,7 @@ class CITypeAttributeGroupManager(object):
|
||||
@staticmethod
|
||||
def delete(group_id):
|
||||
group = CITypeAttributeGroup.get_by_id(group_id) \
|
||||
or abort(404, "AttributeGroup <{0}> does not exist".format(group_id))
|
||||
or abort(404, "AttributeGroup <{0}> does not exist".format(group_id))
|
||||
group.soft_delete()
|
||||
|
||||
items = CITypeAttributeGroupItem.get_by(group_id=group_id, to_dict=False)
|
||||
|
@@ -5,19 +5,19 @@ import datetime
|
||||
import six
|
||||
from markupsafe import escape
|
||||
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.models.cmdb import Attribute
|
||||
from api.models.cmdb import TextChoice
|
||||
from api.models.cmdb import FloatChoice
|
||||
from api.models.cmdb import IntegerChoice
|
||||
from api.models.cmdb import CIValueText
|
||||
from api.models.cmdb import CIValueInteger
|
||||
from api.models.cmdb import CIValueFloat
|
||||
from api.models.cmdb import CIValueDateTime
|
||||
from api.models.cmdb import CIIndexValueDateTime
|
||||
from api.models.cmdb import CIIndexValueFloat
|
||||
from api.models.cmdb import CIIndexValueInteger
|
||||
from api.models.cmdb import CIIndexValueText
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.models.cmdb import CIValueDateTime
|
||||
from api.models.cmdb import CIValueFloat
|
||||
from api.models.cmdb import CIValueInteger
|
||||
from api.models.cmdb import CIValueText
|
||||
from api.models.cmdb import FloatChoice
|
||||
from api.models.cmdb import IntegerChoice
|
||||
from api.models.cmdb import TextChoice
|
||||
|
||||
|
||||
def string2int(x):
|
||||
@@ -144,5 +144,6 @@ class PermEnum(object):
|
||||
class RoleEnum(object):
|
||||
CONFIG = "admin"
|
||||
|
||||
|
||||
CMDB_QUEUE = "cmdb_async"
|
||||
REDIS_PREFIX = "CMDB_CI"
|
||||
|
@@ -1,22 +1,20 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
|
||||
from flask import g
|
||||
from flask import abort
|
||||
|
||||
from flask import g
|
||||
|
||||
from api.extensions import db
|
||||
from api.models.cmdb import Attribute
|
||||
from api.models.cmdb import OperationRecord
|
||||
from api.models.cmdb import AttributeHistory
|
||||
from api.models.cmdb import CIRelationHistory
|
||||
from api.models.account import UserCache
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.lib.cmdb.cache import RelationTypeCache
|
||||
from api.models.account import UserCache
|
||||
from api.models.cmdb import Attribute
|
||||
from api.models.cmdb import AttributeHistory
|
||||
from api.models.cmdb import CIRelationHistory
|
||||
from api.models.cmdb import OperationRecord
|
||||
|
||||
|
||||
class AttributeHistoryManger(object):
|
||||
|
||||
@staticmethod
|
||||
def get_records(start, end, username, page, page_size):
|
||||
records = db.session.query(OperationRecord).filter(OperationRecord.deleted.is_(False))
|
||||
@@ -113,7 +111,6 @@ class AttributeHistoryManger(object):
|
||||
|
||||
|
||||
class CIRelationHistoryManager(object):
|
||||
|
||||
@staticmethod
|
||||
def add(rel_obj, operate_type=CIRelationHistory.ADD):
|
||||
record = OperationRecord.create(uid=g.user.uid)
|
||||
|
@@ -2,22 +2,21 @@
|
||||
|
||||
import six
|
||||
import toposort
|
||||
from flask import g
|
||||
from flask import abort
|
||||
from flask import g
|
||||
|
||||
from api.extensions import db
|
||||
from api.lib.cmdb.attribute import AttributeManager
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.lib.cmdb.cache import CITypeCache
|
||||
from api.lib.cmdb.cache import CITypeAttributeCache
|
||||
from api.lib.cmdb.cache import CITypeCache
|
||||
from api.models.cmdb import CITypeAttribute
|
||||
from api.models.cmdb import PreferenceRelationView
|
||||
from api.models.cmdb import PreferenceShowAttributes
|
||||
from api.models.cmdb import PreferenceTreeView
|
||||
from api.models.cmdb import PreferenceRelationView
|
||||
from api.models.cmdb import CITypeAttribute
|
||||
from api.lib.cmdb.attribute import AttributeManager
|
||||
|
||||
|
||||
class PreferenceManager(object):
|
||||
|
||||
@staticmethod
|
||||
def get_types(instance=False, tree=False):
|
||||
types = db.session.query(PreferenceShowAttributes.type_id).filter(
|
||||
|
@@ -8,7 +8,6 @@ import time
|
||||
from flask import current_app
|
||||
|
||||
from api.extensions import db
|
||||
from api.lib.utils import handle_arg_list
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.lib.cmdb.cache import CITypeCache
|
||||
from api.lib.cmdb.ci import CIManager
|
||||
@@ -17,6 +16,7 @@ from api.lib.cmdb.const import TableMap
|
||||
from api.lib.cmdb.query_sql import FACET_QUERY
|
||||
from api.lib.cmdb.query_sql import QUERY_CI_BY_ATTR_NAME
|
||||
from api.lib.cmdb.query_sql import QUERY_CI_BY_TYPE
|
||||
from api.lib.utils import handle_arg_list
|
||||
from api.models.cmdb import Attribute
|
||||
from api.models.cmdb import CI
|
||||
|
||||
|
@@ -2,18 +2,17 @@
|
||||
|
||||
|
||||
import markupsafe
|
||||
|
||||
from flask import abort
|
||||
|
||||
from api.extensions import db
|
||||
from api.lib.utils import handle_arg_list
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.lib.cmdb.attribute import AttributeManager
|
||||
from api.lib.cmdb.const import type_map
|
||||
from api.lib.cmdb.const import TableMap
|
||||
from api.lib.cmdb.cache import AttributeCache
|
||||
from api.lib.cmdb.const import ExistPolicy
|
||||
from api.lib.cmdb.const import OperateType
|
||||
from api.lib.cmdb.const import TableMap
|
||||
from api.lib.cmdb.const import type_map
|
||||
from api.lib.cmdb.history import AttributeHistoryManger
|
||||
from api.lib.utils import handle_arg_list
|
||||
from api.models.cmdb import Attribute
|
||||
|
||||
|
||||
@@ -87,10 +86,10 @@ class AttributeValueManager(object):
|
||||
|
||||
@staticmethod
|
||||
def __check_is_unique(value_table, attr_id, ci_id, value):
|
||||
db.session.query(value_table.attr_id).filter(
|
||||
existed = db.session.query(value_table.attr_id).filter(
|
||||
value_table.attr_id == attr_id).filter(value_table.deleted.is_(False)).filter(
|
||||
value_table.value == value).filter(value_table.ci_id != ci_id).first() \
|
||||
and abort(400, "attribute <{0}> value {1} must be unique".format(attr_id, value))
|
||||
value_table.value == value).filter(value_table.ci_id != ci_id).first()
|
||||
existed and abort(400, "attribute <{0}> value {1} must be unique".format(attr_id, value))
|
||||
|
||||
def _validate(self, attr, value, value_table, ci_id):
|
||||
v = self.__deserialize_value(attr.value_type, value)
|
||||
@@ -131,7 +130,7 @@ class AttributeValueManager(object):
|
||||
value_list = handle_arg_list(value) if attr.is_list else [value]
|
||||
if not isinstance(value, list):
|
||||
value_list = [value]
|
||||
|
||||
|
||||
for v in value_list:
|
||||
v = self._validate(attr, v, value_table, ci_id)
|
||||
if not v and attr.value_type != Attribute.TEXT:
|
||||
|
Reference in New Issue
Block a user