code format

This commit is contained in:
pycook
2019-11-07 19:17:10 +08:00
parent c521dd447e
commit cce10d39ea
32 changed files with 205 additions and 253 deletions

View File

@@ -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,

View File

@@ -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))

View File

@@ -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)

View File

@@ -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)

View File

@@ -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"

View File

@@ -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)

View File

@@ -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(

View File

@@ -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

View File

@@ -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:

View File

@@ -4,10 +4,10 @@
import hashlib
import requests
from future.moves.urllib.parse import urlparse
from flask import abort
from flask import g
from flask import current_app
from flask import g
from future.moves.urllib.parse import urlparse
def build_api_key(path, params):

View File

@@ -1,40 +1,40 @@
# -*- coding:utf-8 -*-
from flask import current_app
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.mime.image import MIMEImage
import smtplib
import time
from email import Utils
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from flask import current_app
def send_mail(sender, receiver, subject, content, ctype="html", pics=()):
"""subject and body are unicode objects"""
if not sender:
sender = current_app.config.get("DEFAULT_MAIL_SENDER")
smtpserver = current_app.config.get("MAIL_SERVER")
smtp_server = current_app.config.get("MAIL_SERVER")
if ctype == "html":
msg = MIMEText(content, 'html', 'utf-8')
else:
msg = MIMEText(content, 'plain', 'utf-8')
if len(pics) != 0:
msgRoot = MIMEMultipart('related')
msgText = MIMEText(content, 'html', 'utf-8')
msgRoot.attach(msgText)
msg_root = MIMEMultipart('related')
msg_text = MIMEText(content, 'html', 'utf-8')
msg_root.attach(msg_text)
i = 1
for pic in pics:
fp = open(pic, "rb")
image = MIMEImage(fp.read())
fp.close()
image.add_header('Content-ID', '<img%02d>' % i)
msgRoot.attach(image)
msg_root.attach(image)
i += 1
msg = msgRoot
msg = msg_root
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = sender
@@ -43,7 +43,9 @@ def send_mail(sender, receiver, subject, content, ctype="html", pics=()):
msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
smtp = smtplib.SMTP()
smtp.connect(smtpserver, 25)
# smtp.login(username, password)
smtp.connect(smtp_server, 25)
username, password = current_app.config.get("MAIL_USERNAME"), current_app.config.get("MAIL_PASSWORD")
if username and password:
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

View File

@@ -4,11 +4,11 @@
from functools import wraps
import jwt
from flask import abort
from flask import current_app
from flask import g
from flask import request
from flask import session
from flask import g
from flask import abort
from flask_login import login_user
from api.models.account import User
@@ -54,7 +54,7 @@ def _auth_with_token():
return True
except jwt.ExpiredSignatureError:
return False
except (jwt.InvalidTokenError, Exception) as e:
except (jwt.InvalidTokenError, Exception):
return False

View File

@@ -1,7 +1,7 @@
# -*- coding:utf-8 -*-
import six
import redis
import six
from flask import current_app
@@ -52,7 +52,7 @@ class RedisHandler(object):
try:
value = self.r.hmget(self.prefix, key_ids)
except Exception as e:
current_app.logger.error("get redis error, %s" % str(e))
current_app.logger.error("get redis error, {0}".format(str(e)))
return
return value
@@ -60,7 +60,7 @@ class RedisHandler(object):
try:
self.r.hmset(self.prefix, obj)
except Exception as e:
current_app.logger.error("set redis error, %s" % str(e))
current_app.logger.error("set redis error, {0}".format(str(e)))
def add(self, obj):
self._set(obj)
@@ -69,6 +69,6 @@ class RedisHandler(object):
try:
ret = self.r.hdel(self.prefix, key_id)
if not ret:
current_app.logger.warn("[%d] is not in redis" % key_id)
current_app.logger.warn("[%d] is not in redis".format(key_id))
except Exception as e:
current_app.logger.error("delete redis key error, %s" % str(e))
current_app.logger.error("delete redis key error, {0}".format(str(e)))