refactor: 重构校验

This commit is contained in:
shaohuzhang1 2024-10-18 18:40:30 +08:00 committed by shaohuzhang1
parent d5bbf48d01
commit ad6d617caa
4 changed files with 16 additions and 14 deletions

View File

@ -226,8 +226,8 @@ class ApplicationSerializer(serializers.Serializer):
is_draggable = 'false' is_draggable = 'false'
show_guide = 'true' show_guide = 'true'
float_icon = f"{self.data.get('protocol')}://{self.data.get('host')}/ui/MaxKB.gif" 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_cache = DBModelManage.get_model('xpack_cache')
'XPACK_LICENSE_IS_VALID') else False) X_PACK_LICENSE_IS_VALID = False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID', False)
# 获取接入的query参数 # 获取接入的query参数
query = self.get_query_api_input(application_access_token.application, params) 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.show_source = instance.get('show_source')
application_access_token.save() application_access_token.save()
application_setting_model = DBModelManage.get_model('application_setting') application_setting_model = DBModelManage.get_model('application_setting')
X_PACK_LICENSE_IS_VALID = (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, xpack_cache = DBModelManage.get_model('xpack_cache')
'XPACK_LICENSE_IS_VALID') else False) 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: if application_setting_model is not None and X_PACK_LICENSE_IS_VALID:
application_setting, _ = application_setting_model.objects.get_or_create( application_setting, _ = application_setting_model.objects.get_or_create(
application_id=self.data.get('application_id')) application_id=self.data.get('application_id'))
@ -736,8 +736,8 @@ class ApplicationSerializer(serializers.Serializer):
if application_access_token is None: if application_access_token is None:
raise AppUnauthorizedFailed(500, "非法用户") raise AppUnauthorizedFailed(500, "非法用户")
application_setting_model = DBModelManage.get_model('application_setting') application_setting_model = DBModelManage.get_model('application_setting')
X_PACK_LICENSE_IS_VALID = (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, xpack_cache = DBModelManage.get_model('xpack_cache')
'XPACK_LICENSE_IS_VALID') else False) X_PACK_LICENSE_IS_VALID = False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID', False)
application_setting_dict = {} application_setting_dict = {}
if application_setting_model is not None and X_PACK_LICENSE_IS_VALID: if application_setting_model is not None and X_PACK_LICENSE_IS_VALID:
application_setting = QuerySet(application_setting_model).filter( application_setting = QuerySet(application_setting_model).filter(

View File

@ -10,10 +10,10 @@ import importlib
from functools import reduce from functools import reduce
from typing import Dict, List from typing import Dict, List
from django.conf import settings
from django.db.models import QuerySet from django.db.models import QuerySet
from ..exception.app_exception import AppApiException from ..exception.app_exception import AppApiException
from ..models.db_model_manage import DBModelManage
def sub_array(array: List, item_num=10): 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 valid_license(model=None, count=None, message=None):
def inner(func): def inner(func):
def run(*args, **kwargs): def run(*args, **kwargs):
if (not (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, xpack_cache = DBModelManage.get_model('xpack_cache')
'XPACK_LICENSE_IS_VALID') else None) if ((not False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID', False))
and QuerySet( and QuerySet(
model).count() >= count): model).count() >= count):
error = message or f'超出限制{count},请联系我们https://fit2cloud.com/)。' error = message or f'超出限制{count},请联系我们https://fit2cloud.com/)。'

View File

@ -14,9 +14,9 @@ from rest_framework import serializers
from application.models import Application from application.models import Application
from common.exception.app_exception import AppApiException from common.exception.app_exception import AppApiException
from common.models.db_model_manage import DBModelManage
from common.util.field_message import ErrMessage from common.util.field_message import ErrMessage
from dataset.models import DataSet from dataset.models import DataSet
from smartdoc import settings
from users.models import User from users.models import User
model_message_dict = { model_message_dict = {
@ -40,8 +40,8 @@ class ValidSerializer(serializers.Serializer):
if is_valid: if is_valid:
self.is_valid(raise_exception=True) self.is_valid(raise_exception=True)
model_value = model_message_dict.get(self.data.get('valid_type')) model_value = model_message_dict.get(self.data.get('valid_type'))
if not (settings.XPACK_LICENSE_IS_VALID if hasattr(settings, xpack_cache = DBModelManage.get_model('xpack_cache')
'XPACK_LICENSE_IS_VALID') else None): 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'): if self.data.get('valid_count') != model_value.get('count'):
raise AppApiException(400, model_value.get('message')) raise AppApiException(400, model_value.get('message'))
if QuerySet( if QuerySet(

View File

@ -28,6 +28,7 @@ from common.constants.permission_constants import RoleConstants, get_permission_
from common.db.search import page_search from common.db.search import page_search
from common.exception.app_exception import AppApiException from common.exception.app_exception import AppApiException
from common.mixins.api_mixin import ApiMixin 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.response.result import get_api_response
from common.util.common import valid_license from common.util.common import valid_license
from common.util.field_message import ErrMessage from common.util.field_message import ErrMessage
@ -45,9 +46,10 @@ class SystemSerializer(ApiMixin, serializers.Serializer):
@staticmethod @staticmethod
def get_profile(): def get_profile():
version = os.environ.get('MAXKB_VERSION') version = os.environ.get('MAXKB_VERSION')
xpack_cache = DBModelManage.get_model('xpack_cache')
return {'version': version, 'IS_XPACK': hasattr(settings, 'IS_XPACK'), 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': False if xpack_cache is None else xpack_cache.get('XPACK_LICENSE_IS_VALID',
'XPACK_LICENSE_IS_VALID') else False)} False)}
@staticmethod @staticmethod
def get_response_body_api(): def get_response_body_api():