init
This commit is contained in:
51
web/commons/models/mixin/insensitive.py
Normal file
51
web/commons/models/mixin/insensitive.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from nc_http.tools.helpers import camelize
|
||||
|
||||
|
||||
class InsensitiveMixin:
|
||||
__table__ = None
|
||||
_sensitive_columns = []
|
||||
|
||||
def to_insensitive_dict(self, is_camelized=True):
|
||||
"""
|
||||
对象转字典且不带敏感数据 <例:密码、密钥、手机号...>
|
||||
:return:
|
||||
"""
|
||||
if self._sensitive_columns:
|
||||
_ = {}
|
||||
for column in self.__table__.columns:
|
||||
if column in self._sensitive_columns:
|
||||
continue
|
||||
key = getattr(column, 'quote', None) or column.name
|
||||
if is_camelized:
|
||||
_[camelize(key)] = getattr(self, key, None)
|
||||
else:
|
||||
_[key] = getattr(self, key, None)
|
||||
else:
|
||||
_ = self.to_dict()
|
||||
return _
|
||||
|
||||
@classmethod
|
||||
def get_list(cls, query, paging=None, fields=None):
|
||||
if fields:
|
||||
query = query.with_entities(*fields.values())
|
||||
|
||||
if paging:
|
||||
page = query.paginate(paging['page'], paging['limit'])
|
||||
paging['total'] = page.total
|
||||
result = page.items
|
||||
else:
|
||||
result = query.all()
|
||||
|
||||
if fields:
|
||||
columns = fields.keys()
|
||||
result = [dict(zip(columns, _)) for _ in result]
|
||||
else:
|
||||
if cls._sensitive_columns: # 数据脱敏
|
||||
result = [_.to_insensitive_dict() for _ in result]
|
||||
else:
|
||||
result = [_.to_dict() for _ in result]
|
||||
|
||||
if paging:
|
||||
return result, paging
|
||||
|
||||
return result
|
Reference in New Issue
Block a user