From d176ccde4bc596f07624737a5aefd4869d264d7b Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 20 Jul 2022 16:52:01 +0800 Subject: [PATCH] perf: stash --- apps/assets/api/system_user.py | 5 +++-- apps/assets/models/user.py | 17 ++++++++++------- apps/assets/urls/api_urls.py | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/apps/assets/api/system_user.py b/apps/assets/api/system_user.py index 5113cc30a..89d5d909b 100644 --- a/apps/assets/api/system_user.py +++ b/apps/assets/api/system_user.py @@ -82,9 +82,10 @@ class SystemUserAssetAccountApi(generics.RetrieveUpdateDestroyAPIView): def get_object(self): 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() - return system_user.get_account(user_id, asset_id) + account = system_user.get_account(user_id, asset_id) + return account class SystemUserAuthInfoApi(generics.RetrieveUpdateDestroyAPIView): diff --git a/apps/assets/models/user.py b/apps/assets/models/user.py index 0f6b51e0f..9e37b2fee 100644 --- a/apps/assets/models/user.py +++ b/apps/assets/models/user.py @@ -7,6 +7,8 @@ import logging from django.db import models from django.utils.translation import ugettext_lazy as _ 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 users.models import User @@ -205,13 +207,20 @@ class SystemUser(ProtocolMixin, BaseUser): pass 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): + from .account import Account username = self.username if self.username_same_with_user: user = get_object_or_404(User, id=user_id) username = user.username + return get_object_or_404(Account, asset_id=asset_id, username=username) def get_account(self, user_id, asset_id): 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: 准备废弃 class AdminUser(BaseUser): """ diff --git a/apps/assets/urls/api_urls.py b/apps/assets/urls/api_urls.py index 5876c5030..8f2d4c8cf 100644 --- a/apps/assets/urls/api_urls.py +++ b/apps/assets/urls/api_urls.py @@ -49,7 +49,7 @@ urlpatterns = [ path('system-users//assets/', api.SystemUserAssetsListView.as_view(), name='system-user-assets'), path('system-users//assets//auth-info/', api.SystemUserAssetAuthInfoApi.as_view(), name='system-user-asset-auth-info'), path('system-users//applications//auth-info/', api.SystemUserAppAuthInfoApi.as_view(), name='system-user-app-auth-info'), - path('system-users//assets//account/', api.SystemUserAssetAccountApi.as_view(), name='system-user-asset-account'), + path('system-users//assets//users//account/', api.SystemUserAssetAccountApi.as_view(), name='system-user-asset-account'), path('system-users//temp-auth/', api.SystemUserTempAuthInfoApi.as_view(), name='system-user-asset-temp-info'), path('system-users//tasks/', api.SystemUserTaskApi.as_view(), name='system-user-task-create'), path('system-users//cmd-filter-rules/', api.SystemUserCommandFilterRuleListApi.as_view(), name='system-user-cmd-filter-rule-list'),