perf: stash

pull/8566/head
ibuler 2022-07-20 16:52:01 +08:00
parent c9becca633
commit d176ccde4b
3 changed files with 14 additions and 10 deletions

View File

@ -82,9 +82,10 @@ class SystemUserAssetAccountApi(generics.RetrieveUpdateDestroyAPIView):
def get_object(self): def get_object(self):
asset_id = self.kwargs.get('asset_id') asset_id = self.kwargs.get('asset_id')
user_id = self.request.query_params.get("user_id") user_id = self.kwargs.get("user_id")
system_user = super().get_object() system_user = super().get_object()
return system_user.get_account(user_id, asset_id) account = system_user.get_account(user_id, asset_id)
return account
class SystemUserAuthInfoApi(generics.RetrieveUpdateDestroyAPIView): class SystemUserAuthInfoApi(generics.RetrieveUpdateDestroyAPIView):

View File

@ -7,6 +7,8 @@ import logging
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.core.validators import MinValueValidator, MaxValueValidator from django.core.validators import MinValueValidator, MaxValueValidator
from django.shortcuts import get_object_or_404
from django.core.cache import cache
from common.utils import signer from common.utils import signer
from users.models import User from users.models import User
@ -205,13 +207,20 @@ class SystemUser(ProtocolMixin, BaseUser):
pass pass
def get_manual_account(self, user_id, asset_id): def get_manual_account(self, user_id, asset_id):
pass cache_key = 'manual_account_{}_{}_{}'.format(self.id, user_id, asset_id)
return cache.get(cache_key)
def create_manual_account(self, user_id, asset_id, account, ttl=300):
cache_key = 'manual_account_{}_{}_{}'.format(self.id, user_id, asset_id)
cache.set(cache_key, account, ttl)
def get_auto_account(self, user_id, asset_id): def get_auto_account(self, user_id, asset_id):
from .account import Account
username = self.username username = self.username
if self.username_same_with_user: if self.username_same_with_user:
user = get_object_or_404(User, id=user_id) user = get_object_or_404(User, id=user_id)
username = user.username username = user.username
return get_object_or_404(Account, asset_id=asset_id, username=username)
def get_account(self, user_id, asset_id): def get_account(self, user_id, asset_id):
if self.login_mode == self.LOGIN_AUTO: if self.login_mode == self.LOGIN_AUTO:
@ -228,12 +237,6 @@ class SystemUser(ProtocolMixin, BaseUser):
] ]
class SystemUserAccount(models.Model):
system_user = models.ForeignKey('SystemUser', on_delete=models.CASCADE, related_name='accounts')
account = models.ForeignKey('assets.Account', on_delete=models.CASCADE, related_name='system_users')
date_created = models.DateTimeField(auto_now_add=True)
# Deprecated: 准备废弃 # Deprecated: 准备废弃
class AdminUser(BaseUser): class AdminUser(BaseUser):
""" """

View File

@ -49,7 +49,7 @@ urlpatterns = [
path('system-users/<uuid:pk>/assets/', api.SystemUserAssetsListView.as_view(), name='system-user-assets'), path('system-users/<uuid:pk>/assets/', api.SystemUserAssetsListView.as_view(), name='system-user-assets'),
path('system-users/<uuid:pk>/assets/<uuid:asset_id>/auth-info/', api.SystemUserAssetAuthInfoApi.as_view(), name='system-user-asset-auth-info'), path('system-users/<uuid:pk>/assets/<uuid:asset_id>/auth-info/', api.SystemUserAssetAuthInfoApi.as_view(), name='system-user-asset-auth-info'),
path('system-users/<uuid:pk>/applications/<uuid:app_id>/auth-info/', api.SystemUserAppAuthInfoApi.as_view(), name='system-user-app-auth-info'), path('system-users/<uuid:pk>/applications/<uuid:app_id>/auth-info/', api.SystemUserAppAuthInfoApi.as_view(), name='system-user-app-auth-info'),
path('system-users/<uuid:pk>/assets/<uuid:asset_id>/account/', api.SystemUserAssetAccountApi.as_view(), name='system-user-asset-account'), path('system-users/<uuid:pk>/assets/<uuid:asset_id>/users/<uuid:user_id>/account/', api.SystemUserAssetAccountApi.as_view(), name='system-user-asset-account'),
path('system-users/<uuid:pk>/temp-auth/', api.SystemUserTempAuthInfoApi.as_view(), name='system-user-asset-temp-info'), path('system-users/<uuid:pk>/temp-auth/', api.SystemUserTempAuthInfoApi.as_view(), name='system-user-asset-temp-info'),
path('system-users/<uuid:pk>/tasks/', api.SystemUserTaskApi.as_view(), name='system-user-task-create'), path('system-users/<uuid:pk>/tasks/', api.SystemUserTaskApi.as_view(), name='system-user-task-create'),
path('system-users/<uuid:pk>/cmd-filter-rules/', api.SystemUserCommandFilterRuleListApi.as_view(), name='system-user-cmd-filter-rule-list'), path('system-users/<uuid:pk>/cmd-filter-rules/', api.SystemUserCommandFilterRuleListApi.as_view(), name='system-user-cmd-filter-rule-list'),