2019-03-18 02:15:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-06-19 03:31:38 +00:00
|
|
|
import uuid
|
2019-03-18 02:15:33 +00:00
|
|
|
from abc import abstractmethod
|
|
|
|
|
|
|
|
|
|
|
|
class BaseBackend:
|
|
|
|
@classmethod
|
|
|
|
@abstractmethod
|
2019-06-19 03:31:38 +00:00
|
|
|
def filter(cls, username=None, assets=None, latest=True):
|
2019-03-18 02:15:33 +00:00
|
|
|
"""
|
|
|
|
:param username: 用户名
|
2019-06-19 03:31:38 +00:00
|
|
|
:param assets: <Asset>对象
|
2019-03-18 02:15:33 +00:00
|
|
|
:param latest: 是否是最新记录
|
|
|
|
:return: 元素为<AuthBook>的可迭代对象(<list> or <QuerySet>)
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class AssetUserQuerySet(list):
|
|
|
|
def order_by(self, *ordering):
|
|
|
|
_ordering = []
|
|
|
|
reverse = False
|
|
|
|
for i in ordering:
|
|
|
|
if i[0] == '-':
|
|
|
|
reverse = True
|
|
|
|
i = i[1:]
|
|
|
|
_ordering.append(i)
|
|
|
|
self.sort(key=lambda obj: [getattr(obj, j) for j in _ordering], reverse=reverse)
|
|
|
|
return self
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
def filter_in(self, kwargs):
|
|
|
|
in_kwargs = {}
|
|
|
|
queryset = []
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
if len(v) == 0:
|
|
|
|
return self
|
|
|
|
if k.find("__in") >= 0:
|
|
|
|
in_kwargs[k] = v
|
|
|
|
for k in in_kwargs:
|
|
|
|
kwargs.pop(k)
|
|
|
|
|
|
|
|
if len(in_kwargs) == 0:
|
|
|
|
return self
|
|
|
|
for i in self:
|
|
|
|
matched = True
|
|
|
|
for k, v in in_kwargs.items():
|
|
|
|
key = k.split('__')[0]
|
|
|
|
attr = getattr(i, key, None)
|
|
|
|
# 如果属性或者value中是uuid,则转换成string
|
|
|
|
if isinstance(v[0], uuid.UUID):
|
|
|
|
v = [str(i) for i in v]
|
|
|
|
if isinstance(attr, uuid.UUID):
|
|
|
|
attr = str(attr)
|
|
|
|
if attr not in v:
|
|
|
|
matched = False
|
|
|
|
if matched:
|
|
|
|
queryset.append(i)
|
|
|
|
return AssetUserQuerySet(queryset)
|
|
|
|
|
|
|
|
def filter_equal(self, kwargs):
|
|
|
|
def filter_it(obj):
|
|
|
|
wanted = []
|
|
|
|
real = []
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
wanted.append(v)
|
|
|
|
value = getattr(obj, k)
|
|
|
|
if isinstance(value, uuid.UUID):
|
|
|
|
value = str(value)
|
|
|
|
real.append(value)
|
|
|
|
return wanted == real
|
|
|
|
if len(kwargs) > 0:
|
|
|
|
queryset = AssetUserQuerySet([i for i in self if filter_it(i)])
|
|
|
|
else:
|
|
|
|
queryset = self
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter(self, **kwargs):
|
|
|
|
queryset = self.filter_in(kwargs).filter_equal(kwargs)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def __or__(self, other):
|
|
|
|
self.extend(other)
|
|
|
|
return self
|