UnisKB/apps/setting/serializers/valid_serializers.py

56 lines
2.4 KiB
Python
Raw Normal View History

# coding=utf-8
"""
@project: MaxKB
@Author
@file valid_serializers.py
@date2024/7/8 18:00
@desc:
"""
import re
from django.core import validators
from django.db.models import QuerySet
from rest_framework import serializers
from application.models import Application
from common.exception.app_exception import AppApiException
2024-10-18 10:40:30 +00:00
from common.models.db_model_manage import DBModelManage
from common.util.field_message import ErrMessage
from dataset.models import DataSet
from users.models import User
2025-01-13 03:15:51 +00:00
from django.utils.translation import gettext_lazy as _
model_message_dict = {
'dataset': {'model': DataSet, 'count': 50,
2025-01-13 03:15:51 +00:00
'message': _(
'The community version supports up to 50 knowledge bases. If you need more knowledge bases, please contact us (https://fit2cloud.com/).')},
'application': {'model': Application, 'count': 5,
2025-01-13 03:15:51 +00:00
'message': _(
'The community version supports up to 5 applications. If you need more applications, please contact us (https://fit2cloud.com/).')},
'user': {'model': User, 'count': 2,
2025-01-13 03:15:51 +00:00
'message': _(
'The community version supports up to 2 users. If you need more users, please contact us (https://fit2cloud.com/).')}
}
class ValidSerializer(serializers.Serializer):
2025-01-13 03:15:51 +00:00
valid_type = serializers.CharField(required=True, error_messages=ErrMessage.char(_('type')), validators=[
validators.RegexValidator(regex=re.compile("^application|dataset|user$"),
message="类型只支持:application|dataset|user", code=500)
])
2025-01-13 03:15:51 +00:00
valid_count = serializers.IntegerField(required=True, error_messages=ErrMessage.integer(_('check quantity')))
def valid(self, is_valid=True):
if is_valid:
self.is_valid(raise_exception=True)
model_value = model_message_dict.get(self.data.get('valid_type'))
2024-10-18 10:40:30 +00:00
xpack_cache = DBModelManage.get_model('xpack_cache')
2024-10-22 09:53:08 +00:00
is_license_valid = xpack_cache.get('XPACK_LICENSE_IS_VALID', False) if xpack_cache is not None else False
if not is_license_valid:
if self.data.get('valid_count') != model_value.get('count'):
raise AppApiException(400, model_value.get('message'))
if QuerySet(
model_value.get('model')).count() >= model_value.get('count'):
raise AppApiException(400, model_value.get('message'))
return True