From d040162d867b2cf500dcbecd9efe00c8f6cdd843 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 16 Dec 2022 15:53:59 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20session=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=EF=BC=8C=E6=B7=BB=E5=8A=A0=20Comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0114_remove_redundant_macos.py | 15 +++++++---- .../user_permission/tree/node_with_asset.py | 26 +++++++++---------- .../migrations/0062_auto_20221216_1529.py | 23 ++++++++++++++++ apps/terminal/models/session/session.py | 19 +++++++------- 4 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 apps/terminal/migrations/0062_auto_20221216_1529.py diff --git a/apps/assets/migrations/0114_remove_redundant_macos.py b/apps/assets/migrations/0114_remove_redundant_macos.py index 1d8183e12..d24a3e74a 100644 --- a/apps/assets/migrations/0114_remove_redundant_macos.py +++ b/apps/assets/migrations/0114_remove_redundant_macos.py @@ -7,14 +7,19 @@ def migrate_del_macos(apps, schema_editor): db_alias = schema_editor.connection.alias asset_model = apps.get_model('assets', 'Asset') platform_model = apps.get_model('assets', 'Platform') - old_macos = platform_model.objects.using(db_alias).get( + old_macos = platform_model.objects.using(db_alias).filter( name='MacOS', type='macos' - ) - new_macos = platform_model.objects.using(db_alias).get( + ).first() + new_macos = platform_model.objects.using(db_alias).filter( name='macOS', type='unix' - ) + ).first() + + if not old_macos or not new_macos: + return + asset_model.objects.using(db_alias).filter( - platform=old_macos).update(platform=new_macos) + platform=old_macos + ).update(platform=new_macos) platform_model.objects.using(db_alias).filter(id=old_macos.id).delete() diff --git a/apps/perms/api/user_permission/tree/node_with_asset.py b/apps/perms/api/user_permission/tree/node_with_asset.py index 98b7cd261..66f195541 100644 --- a/apps/perms/api/user_permission/tree/node_with_asset.py +++ b/apps/perms/api/user_permission/tree/node_with_asset.py @@ -4,25 +4,24 @@ from urllib.parse import parse_qsl from django.conf import settings from django.db.models import F, Value, CharField from rest_framework.generics import ListAPIView +from rest_framework.generics import get_object_or_404 from rest_framework.request import Request from rest_framework.response import Response -from rest_framework.generics import get_object_or_404 +from assets.api import SerializeToTreeNodeMixin from assets.models import Asset, Account from assets.utils import KubernetesTree -from assets.api import SerializeToTreeNodeMixin +from common.utils import get_object_or_none, lazyproperty +from common.utils.common import timeit from perms.hands import Node from perms.models import PermNode +from perms.utils import PermAccountUtil +from perms.utils.permission import AssetPermissionUtil from perms.utils.user_permission import ( UserGrantedNodesQueryUtils, UserGrantedAssetsQueryUtils, ) -from perms.utils import PermAccountUtil -from perms.utils.permission import AssetPermissionUtil -from common.utils import get_object_or_none, lazyproperty -from common.utils.common import timeit - -from ..mixin import SelfOrPKUserMixin from .mixin import RebuildTreeMixin +from ..mixin import SelfOrPKUserMixin __all__ = [ 'UserGrantedK8sAsTreeApi', @@ -31,8 +30,10 @@ __all__ = [ ] -class BaseUserNodeWithAssetAsTreeApi(SelfOrPKUserMixin, RebuildTreeMixin, SerializeToTreeNodeMixin, - ListAPIView): +class BaseUserNodeWithAssetAsTreeApi( + SelfOrPKUserMixin, RebuildTreeMixin, + SerializeToTreeNodeMixin, ListAPIView +): def list(self, request, *args, **kwargs): nodes, assets = self.get_nodes_assets() @@ -129,10 +130,7 @@ class UserPermedNodeChildrenWithAssetsAsTreeApi(BaseUserNodeWithAssetAsTreeApi): return self.query_node_key -class UserGrantedK8sAsTreeApi( - SelfOrPKUserMixin, - ListAPIView -): +class UserGrantedK8sAsTreeApi(SelfOrPKUserMixin, ListAPIView): """ 用户授权的K8s树 """ @staticmethod diff --git a/apps/terminal/migrations/0062_auto_20221216_1529.py b/apps/terminal/migrations/0062_auto_20221216_1529.py new file mode 100644 index 000000000..295cc8b55 --- /dev/null +++ b/apps/terminal/migrations/0062_auto_20221216_1529.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.14 on 2022-12-16 07:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('terminal', '0061_rename_system_user_command_account'), + ] + + operations = [ + migrations.AddField( + model_name='session', + name='comment', + field=models.TextField(blank=True, null=True, verbose_name='Comment'), + ), + migrations.AddField( + model_name='session', + name='type', + field=models.CharField(db_index=True, default='normal', max_length=16), + ), + ] diff --git a/apps/terminal/models/session/session.py b/apps/terminal/models/session/session.py index 0a095a401..0241f10e5 100644 --- a/apps/terminal/models/session/session.py +++ b/apps/terminal/models/session/session.py @@ -3,24 +3,23 @@ from __future__ import unicode_literals import os import uuid -from django.db import models -from django.utils.translation import ugettext_lazy as _ -from django.utils import timezone from django.conf import settings -from django.core.files.storage import default_storage from django.core.cache import cache +from django.core.files.storage import default_storage +from django.db import models +from django.utils import timezone +from django.utils.translation import ugettext_lazy as _ -from assets.models import Asset from assets.const import Protocol -from users.models import User -from orgs.mixins.models import OrgModelMixin -from django.db.models import TextChoices +from assets.models import Asset from common.utils import get_object_or_none, lazyproperty +from orgs.mixins.models import OrgModelMixin from terminal.backends import get_multi_command_storage +from users.models import User class Session(OrgModelMixin): - class LOGIN_FROM(TextChoices): + class LOGIN_FROM(models.TextChoices): ST = 'ST', 'SSH Terminal' RT = 'RT', 'RDP Terminal' WT = 'WT', 'Web Terminal' @@ -34,6 +33,7 @@ class Session(OrgModelMixin): account = models.CharField(max_length=128, verbose_name=_("Account"), db_index=True) protocol = models.CharField(default='ssh', max_length=16, db_index=True) login_from = models.CharField(max_length=2, choices=LOGIN_FROM.choices, default="ST", verbose_name=_("Login from")) + type = models.CharField(max_length=16, default='normal', db_index=True) remote_addr = models.CharField(max_length=128, verbose_name=_("Remote addr"), blank=True, null=True) is_success = models.BooleanField(default=True, db_index=True) is_finished = models.BooleanField(default=False, db_index=True) @@ -42,6 +42,7 @@ class Session(OrgModelMixin): terminal = models.ForeignKey('terminal.Terminal', null=True, on_delete=models.DO_NOTHING, db_constraint=False) date_start = models.DateTimeField(verbose_name=_("Date start"), db_index=True, default=timezone.now) date_end = models.DateTimeField(verbose_name=_("Date end"), null=True) + comment = models.TextField(blank=True, null=True, verbose_name=_("Comment")) upload_to = 'replay' ACTIVE_CACHE_KEY_PREFIX = 'SESSION_ACTIVE_{}'