mirror of https://github.com/jumpserver/jumpserver
commit
57c84f1329
|
@ -1,5 +1,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
|
import functools
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from django.core.cache import cache
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,13 +13,16 @@ def on_transaction_commit(func):
|
||||||
"""
|
"""
|
||||||
如果不调用on_commit, 对象创建时添加多对多字段值失败
|
如果不调用on_commit, 对象创建时添加多对多字段值失败
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def inner(*args, **kwargs):
|
def inner(*args, **kwargs):
|
||||||
transaction.on_commit(lambda: func(*args, **kwargs))
|
transaction.on_commit(lambda: func(*args, **kwargs))
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
|
|
||||||
class Singleton(object):
|
class Singleton(object):
|
||||||
""" 单例类 """
|
""" 单例类 """
|
||||||
|
|
||||||
def __init__(self, cls):
|
def __init__(self, cls):
|
||||||
self._cls = cls
|
self._cls = cls
|
||||||
self._instance = {}
|
self._instance = {}
|
||||||
|
@ -22,3 +31,38 @@ class Singleton(object):
|
||||||
if self._cls not in self._instance:
|
if self._cls not in self._instance:
|
||||||
self._instance[self._cls] = self._cls()
|
self._instance[self._cls] = self._cls()
|
||||||
return self._instance[self._cls]
|
return self._instance[self._cls]
|
||||||
|
|
||||||
|
|
||||||
|
def _run_func_if_is_last(ttl, func, *args, **kwargs):
|
||||||
|
ix = uuid.uuid4().__str__()
|
||||||
|
key = f'DELAY_RUN_{func.__name__}'
|
||||||
|
cache.set(key, ix, ttl)
|
||||||
|
st = (ttl - 2 > 1) and ttl - 2 or 1
|
||||||
|
time.sleep(st)
|
||||||
|
got = cache.get(key, None)
|
||||||
|
|
||||||
|
if ix == got:
|
||||||
|
func(*args, **kwargs)
|
||||||
|
cache.delete(key)
|
||||||
|
|
||||||
|
|
||||||
|
def delay_run(ttl=5):
|
||||||
|
def inner(func):
|
||||||
|
@functools.wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
t = threading.Thread(target=_run_func_if_is_last, args=(ttl, func, *args), kwargs=kwargs)
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
|
@delay_run(ttl=10)
|
||||||
|
def run_it_many(username, year=2000):
|
||||||
|
print("Hello, %s, now is %s" % (username, year))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
for i in range(20):
|
||||||
|
run_it_many('test', 2000)
|
||||||
|
|
|
@ -69,8 +69,8 @@ class RoleViewSet(JMSModelViewSet):
|
||||||
role.users_amount = role_user_amount_mapper.get(role.id, 0)
|
role.users_amount = role_user_amount_mapper.get(role.id, 0)
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def filter_queryset(self, queryset):
|
def page_queryset(self, queryset):
|
||||||
queryset = super().filter_queryset(queryset)
|
queryset = super().page_queryset(queryset)
|
||||||
queryset = self.set_users_amount(queryset)
|
queryset = self.set_users_amount(queryset)
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
|
@ -2,27 +2,27 @@
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from rest_framework.decorators import action
|
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework_bulk import BulkModelViewSet
|
from rest_framework_bulk import BulkModelViewSet
|
||||||
|
|
||||||
from common.api import CommonApiMixin
|
from common.api import CommonApiMixin
|
||||||
from common.utils import get_logger
|
|
||||||
from common.api import SuggestionMixin
|
from common.api import SuggestionMixin
|
||||||
|
from common.utils import get_logger
|
||||||
from orgs.utils import current_org, tmp_to_root_org
|
from orgs.utils import current_org, tmp_to_root_org
|
||||||
from rbac.models import Role, RoleBinding
|
from rbac.models import Role, RoleBinding
|
||||||
from users.utils import LoginBlockUtil, MFABlockUtils
|
from users.utils import LoginBlockUtil, MFABlockUtils
|
||||||
from .mixins import UserQuerysetMixin
|
from .mixins import UserQuerysetMixin
|
||||||
from ..notifications import ResetMFAMsg
|
|
||||||
from .. import serializers
|
from .. import serializers
|
||||||
|
from ..filters import UserFilter
|
||||||
|
from ..models import User
|
||||||
|
from ..notifications import ResetMFAMsg
|
||||||
from ..serializers import (
|
from ..serializers import (
|
||||||
UserSerializer,
|
UserSerializer,
|
||||||
MiniUserSerializer, InviteSerializer
|
MiniUserSerializer, InviteSerializer
|
||||||
)
|
)
|
||||||
from ..models import User
|
|
||||||
from ..signals import post_user_create
|
from ..signals import post_user_create
|
||||||
from ..filters import UserFilter
|
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
@ -53,13 +53,8 @@ class UserViewSet(CommonApiMixin, UserQuerysetMixin, SuggestionMixin, BulkModelV
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def paginate_queryset(self, queryset):
|
def paginate_queryset(self, queryset):
|
||||||
page = super().paginate_queryset(queryset)
|
queryset = super().paginate_queryset(queryset) or queryset
|
||||||
|
return self.set_users_roles_for_cache(queryset)
|
||||||
if page:
|
|
||||||
page = self.set_users_roles_for_cache(page)
|
|
||||||
else:
|
|
||||||
self.set_users_roles_for_cache(queryset)
|
|
||||||
return page
|
|
||||||
|
|
||||||
@action(methods=['get'], detail=False, url_path='suggestions')
|
@action(methods=['get'], detail=False, url_path='suggestions')
|
||||||
def match(self, request, *args, **kwargs):
|
def match(self, request, *args, **kwargs):
|
||||||
|
|
Loading…
Reference in New Issue