From ad6d617caa18592f4f074064a7e03aa28705cf54 Mon Sep 17 00:00:00 2001 From: shaohuzhang1 Date: Fri, 18 Oct 2024 18:40:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serializers/application_serializers.py | 12 ++++++------ apps/common/util/common.py | 6 +++--- apps/setting/serializers/valid_serializers.py | 6 +++--- apps/users/serializers/user_serializers.py | 6 ++++-- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/apps/application/serializers/application_serializers.py b/apps/application/serializers/application_serializers.py index 8338d6a20..9a68a3e1b 100644 --- a/apps/application/serializers/application_serializers.py +++ b/apps/application/serializers/application_serializers.py @@ -226,8 +226,8 @@ class ApplicationSerializer(serializers.Serializer): is_draggable = 'false' show_guide = 'true' float_icon = f"{self.data.get('protocol')}://{self.data.get('host')}/ui/MaxKB.gif" - X_PACK_LICENSE_IS_VALID = (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, - 'XPACK_LICENSE_IS_VALID') else False) + xpack_cache = DBModelManage.get_model('xpack_cache') + X_PACK_LICENSE_IS_VALID = False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID', False) # 获取接入的query参数 query = self.get_query_api_input(application_access_token.application, params) @@ -313,8 +313,8 @@ class ApplicationSerializer(serializers.Serializer): application_access_token.show_source = instance.get('show_source') application_access_token.save() application_setting_model = DBModelManage.get_model('application_setting') - X_PACK_LICENSE_IS_VALID = (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, - 'XPACK_LICENSE_IS_VALID') else False) + xpack_cache = DBModelManage.get_model('xpack_cache') + X_PACK_LICENSE_IS_VALID = False if xpack_cache is None else xpack_cache.get("XPACK_LICENSE_IS_VALID", False) if application_setting_model is not None and X_PACK_LICENSE_IS_VALID: application_setting, _ = application_setting_model.objects.get_or_create( application_id=self.data.get('application_id')) @@ -736,8 +736,8 @@ class ApplicationSerializer(serializers.Serializer): if application_access_token is None: raise AppUnauthorizedFailed(500, "非法用户") application_setting_model = DBModelManage.get_model('application_setting') - X_PACK_LICENSE_IS_VALID = (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, - 'XPACK_LICENSE_IS_VALID') else False) + xpack_cache = DBModelManage.get_model('xpack_cache') + X_PACK_LICENSE_IS_VALID = False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID', False) application_setting_dict = {} if application_setting_model is not None and X_PACK_LICENSE_IS_VALID: application_setting = QuerySet(application_setting_model).filter( diff --git a/apps/common/util/common.py b/apps/common/util/common.py index 9a3ca0c0d..182a18a8f 100644 --- a/apps/common/util/common.py +++ b/apps/common/util/common.py @@ -10,10 +10,10 @@ import importlib from functools import reduce from typing import Dict, List -from django.conf import settings from django.db.models import QuerySet from ..exception.app_exception import AppApiException +from ..models.db_model_manage import DBModelManage def sub_array(array: List, item_num=10): @@ -76,8 +76,8 @@ def post(post_function): def valid_license(model=None, count=None, message=None): def inner(func): def run(*args, **kwargs): - if (not (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, - 'XPACK_LICENSE_IS_VALID') else None) + xpack_cache = DBModelManage.get_model('xpack_cache') + if ((not False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID', False)) and QuerySet( model).count() >= count): error = message or f'超出限制{count},请联系我们(https://fit2cloud.com/)。' diff --git a/apps/setting/serializers/valid_serializers.py b/apps/setting/serializers/valid_serializers.py index 71c7d2146..e36f07dfa 100644 --- a/apps/setting/serializers/valid_serializers.py +++ b/apps/setting/serializers/valid_serializers.py @@ -14,9 +14,9 @@ from rest_framework import serializers from application.models import Application from common.exception.app_exception import AppApiException +from common.models.db_model_manage import DBModelManage from common.util.field_message import ErrMessage from dataset.models import DataSet -from smartdoc import settings from users.models import User model_message_dict = { @@ -40,8 +40,8 @@ class ValidSerializer(serializers.Serializer): if is_valid: self.is_valid(raise_exception=True) model_value = model_message_dict.get(self.data.get('valid_type')) - if not (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, - 'XPACK_LICENSE_IS_VALID') else None): + xpack_cache = DBModelManage.get_model('xpack_cache') + if not False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID', False): if self.data.get('valid_count') != model_value.get('count'): raise AppApiException(400, model_value.get('message')) if QuerySet( diff --git a/apps/users/serializers/user_serializers.py b/apps/users/serializers/user_serializers.py index 527bf12b0..b1b600434 100644 --- a/apps/users/serializers/user_serializers.py +++ b/apps/users/serializers/user_serializers.py @@ -28,6 +28,7 @@ from common.constants.permission_constants import RoleConstants, get_permission_ from common.db.search import page_search from common.exception.app_exception import AppApiException from common.mixins.api_mixin import ApiMixin +from common.models.db_model_manage import DBModelManage from common.response.result import get_api_response from common.util.common import valid_license from common.util.field_message import ErrMessage @@ -45,9 +46,10 @@ class SystemSerializer(ApiMixin, serializers.Serializer): @staticmethod def get_profile(): version = os.environ.get('MAXKB_VERSION') + xpack_cache = DBModelManage.get_model('xpack_cache') return {'version': version, 'IS_XPACK': hasattr(settings, 'IS_XPACK'), - 'XPACK_LICENSE_IS_VALID': (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, - 'XPACK_LICENSE_IS_VALID') else False)} + 'XPACK_LICENSE_IS_VALID': False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID', + False)} @staticmethod def get_response_body_api():