perf: 添加 token

pull/8873/head
ibuler 2022-08-15 18:31:57 +08:00
parent d402ba5d92
commit 0bf8878228
6 changed files with 69 additions and 45 deletions

View File

@ -0,0 +1,36 @@
# Generated by Django 3.2.14 on 2022-08-15 10:11
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('assets', '0108_auto_20220811_1511'),
]
operations = [
migrations.RemoveField(
model_name='account',
name='type',
),
migrations.RemoveField(
model_name='historicalaccount',
name='type',
),
migrations.AddField(
model_name='account',
name='privileged',
field=models.BooleanField(default=False, verbose_name='Privileged account'),
),
migrations.AddField(
model_name='historicalaccount',
name='privileged',
field=models.BooleanField(default=False, verbose_name='Privileged account'),
),
migrations.AlterField(
model_name='platform',
name='su_enabled',
field=models.BooleanField(default=False, verbose_name='Su enabled'),
),
]

View File

@ -0,0 +1,24 @@
# Generated by Django 3.2.14 on 2022-08-15 10:31
import common.db.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('assets', '0109_auto_20220815_1811'),
]
operations = [
migrations.AddField(
model_name='account',
name='token',
field=common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Token'),
),
migrations.AddField(
model_name='historicalaccount',
name='token',
field=common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Token'),
),
]

View File

@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _
from simple_history.models import HistoricalRecords
from common.db.models import JMSBaseModel
from common.db import fields
from .base import BaseUser, AbsConnectivity
@ -10,11 +11,8 @@ __all__ = ['Account']
class Account(BaseUser, AbsConnectivity):
class Type(models.TextChoices):
common = 'common', _('Common user')
admin = 'admin', _('Admin user')
type = models.CharField(max_length=16, choices=Type.choices, default=Type.common, verbose_name=_("Type"))
token = fields.EncryptTextField(blank=True, null=True, verbose_name=_('Token'))
privileged = models.BooleanField(verbose_name=_("Privileged account"), default=False)
asset = models.ForeignKey('assets.Asset', on_delete=models.CASCADE, verbose_name=_('Asset'))
version = models.IntegerField(default=0, verbose_name=_('Version'))
history = HistoricalRecords()

View File

@ -13,12 +13,11 @@ from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.db.models import QuerySet
from common.utils import random_string, signer
from common.utils import random_string
from common.utils import (
ssh_key_string_to_obj, ssh_key_gen, get_logger, lazyproperty
ssh_key_string_to_obj, ssh_key_gen, get_logger
)
from common.utils.encode import ssh_pubkey_gen
from common.validators import alphanumeric
from common.db import fields
from orgs.mixins.models import OrgModelMixin
@ -188,36 +187,9 @@ class BaseUser(OrgModelMixin, AuthMixin):
APPS_AMOUNT_CACHE_KEY = "APP_USER_{}_APPS_AMOUNT"
APP_USER_CACHE_TIME = 600
def get_related_assets(self):
assets = self.assets.filter(org_id=self.org_id)
return assets
def get_related_apps(self):
from applications.models import Account
apps = Account.objects.filter(systemuser=self)
return apps
def get_username(self):
return self.username
@lazyproperty
def assets_amount(self):
cache_key = self.ASSETS_AMOUNT_CACHE_KEY.format(self.id)
cached = cache.get(cache_key)
if not cached:
cached = self.get_related_assets().count()
cache.set(cache_key, cached, self.ASSET_USER_CACHE_TIME)
return cached
@property
def apps_amount(self):
cache_key = self.APPS_AMOUNT_CACHE_KEY.format(self.id)
cached = cache.get(cache_key)
if not cached:
cached = self.get_related_apps().count()
cache.set(cache_key, cached, self.APP_USER_CACHE_TIME)
return cached
def expire_assets_amount(self):
cache_key = self.ASSETS_AMOUNT_CACHE_KEY.format(self.id)
cache.delete(cache_key)

View File

@ -35,7 +35,7 @@ class Platform(models.Model):
)
# Accounts
# 这应该和账号有关
su_enabled = models.BooleanField(default=False)
su_enabled = models.BooleanField(default=False, verbose_name=_("Su enabled"))
su_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("SU method"))
ping_enabled = models.BooleanField(default=False)
ping_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Ping method"))

View File

@ -12,19 +12,13 @@ from common.drf.serializers import SecretReadableMixin
class AccountSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
ip = serializers.ReadOnlyField(label=_("IP"))
asset = serializers.ReadOnlyField(label=_("Asset"))
asset_name = serializers.ReadOnlyField(label=_("Asset"))
platform = serializers.ReadOnlyField(label=_("Platform"))
date_created = serializers.DateTimeField(
label=_('Date created'), format="%Y/%m/%d %H:%M:%S", read_only=True
)
date_updated = serializers.DateTimeField(
label=_('Date updated'), format="%Y/%m/%d %H:%M:%S", read_only=True
)
class Meta:
model = Account
fields_mini = [
'id', 'type', 'username', 'ip', 'name',
'id', 'privileged', 'username', 'ip', 'asset_name',
'platform', 'version'
]
fields_write_only = ['password', 'private_key', 'public_key', 'passphrase']
@ -59,7 +53,7 @@ class AccountSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
""" Perform necessary eager loading of data. """
queryset = queryset.prefetch_related('asset')\
.annotate(ip=F('asset__ip')) \
.annotate(asset=F('asset__name'))
.annotate(asset_name=F('asset__name'))
return queryset