Skip to content

Commit a8127ef

Browse files
committed
refactor: add valid
1 parent 50245bc commit a8127ef

File tree

17 files changed

+205
-61
lines changed

17 files changed

+205
-61
lines changed

apps/locales/en_US/LC_MESSAGES/django.po

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8366,4 +8366,10 @@ msgid "Get tool list"
83668366
msgstr ""
83678367

83688368
msgid "Setting"
8369+
msgstr ""
8370+
8371+
msgid "Get verification results"
8372+
msgstr ""
8373+
8374+
msgid "Validation"
83698375
msgstr ""

apps/locales/zh_CN/LC_MESSAGES/django.po

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8492,4 +8492,10 @@ msgid "Get tool list"
84928492
msgstr "获取工具列表"
84938493

84948494
msgid "Setting"
8495-
msgstr "设置"
8495+
msgstr "设置"
8496+
8497+
msgid "Get verification results"
8498+
msgstr "获取验证结果"
8499+
8500+
msgid "Validation"
8501+
msgstr "验证"

apps/locales/zh_Hant/LC_MESSAGES/django.po

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8492,4 +8492,10 @@ msgid "Get tool list"
84928492
msgstr "獲取工具列表"
84938493

84948494
msgid "Setting"
8495-
msgstr "設置"
8495+
msgstr "設置"
8496+
8497+
msgid "Get verification results"
8498+
msgstr "獲取驗證結果"
8499+
8500+
msgid "Validation"
8501+
msgstr "驗證"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# coding=utf-8
2+
"""
3+
@project: MaxKB
4+
@Author:虎
5+
@file: valid_serializers.py
6+
@date:2024/7/8 18:00
7+
@desc:
8+
"""
9+
import re
10+
11+
from django.core import validators
12+
from django.core.cache import cache
13+
from django.db.models import QuerySet
14+
from rest_framework import serializers
15+
16+
from application.models import Application
17+
from common.constants.cache_version import Cache_Version
18+
from common.exception.app_exception import AppApiException
19+
from knowledge.models import Knowledge
20+
from users.models import User
21+
from django.utils.translation import gettext_lazy as _
22+
23+
model_message_dict = {
24+
'dataset': {'model': Knowledge, 'count': 50,
25+
'message': _(
26+
'The community version supports up to 50 knowledge bases. If you need more knowledge bases, please contact us (https://fit2cloud.com/).')},
27+
'application': {'model': Application, 'count': 5,
28+
'message': _(
29+
'The community version supports up to 5 applications. If you need more applications, please contact us (https://fit2cloud.com/).')},
30+
'user': {'model': User, 'count': 2,
31+
'message': _(
32+
'The community version supports up to 2 users. If you need more users, please contact us (https://fit2cloud.com/).')}
33+
}
34+
35+
36+
class ValidSerializer(serializers.Serializer):
37+
valid_type = serializers.CharField(required=True, label=_('type'), validators=[
38+
validators.RegexValidator(regex=re.compile("^application|knowledge|user$"),
39+
message="类型只支持:application|knowledge|user", code=500)
40+
])
41+
valid_count = serializers.IntegerField(required=True, label=_('check quantity'))
42+
43+
def valid(self, is_valid=True):
44+
if is_valid:
45+
self.is_valid(raise_exception=True)
46+
model_value = model_message_dict.get(self.data.get('valid_type'))
47+
license_is_valid = cache.get(Cache_Version.SYSTEM.get_key(key='license_is_valid'),
48+
version=Cache_Version.SYSTEM.get_version())
49+
is_license_valid = license_is_valid if license_is_valid is not None else False
50+
if not is_license_valid:
51+
if self.data.get('valid_count') != model_value.get('count'):
52+
raise AppApiException(400, model_value.get('message'))
53+
if QuerySet(
54+
model_value.get('model')).count() >= model_value.get('count'):
55+
raise AppApiException(400, model_value.get('message'))
56+
return True

apps/system_manage/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
path('workspace/<str:workspace_id>/user_resource_permission/user/<str:user_id>',
88
views.WorkSpaceUserResourcePermissionView.as_view()),
99
path('email_setting', views.SystemSetting.Email.as_view()),
10-
path('profile', views.SystemProfile.as_view())
10+
path('profile', views.SystemProfile.as_view()),
11+
path('valid/<str:valid_type>/<int:valid_count>', views.Valid.as_view())
1112
]

apps/system_manage/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
from .user_resource_permission import *
1010
from .email_setting import *
1111
from .system_profile import *
12+
from .valid import *

apps/system_manage/views/valid.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# coding=utf-8
2+
"""
3+
@project: MaxKB
4+
@Author:虎
5+
@file: valid.py
6+
@date:2024/7/8 17:50
7+
@desc:
8+
"""
9+
from drf_spectacular.utils import extend_schema
10+
from rest_framework.request import Request
11+
from rest_framework.views import APIView
12+
13+
from common.auth import TokenAuth
14+
from django.utils.translation import gettext_lazy as _
15+
16+
from common.result import result
17+
from system_manage.serializers.valid_serializers import ValidSerializer
18+
19+
20+
class Valid(APIView):
21+
authentication_classes = [TokenAuth]
22+
23+
@extend_schema(
24+
methods=['GET'],
25+
description=_('Get verification results'),
26+
summary=_('Get verification results'),
27+
operation_id=_('Get verification results'), # type: ignore
28+
tags=[_('Validation')] # type: ignore
29+
)
30+
def get(self, request: Request, valid_type: str, valid_count: int):
31+
return result.success(ValidSerializer(data={'valid_type': valid_type, 'valid_count': valid_count}).valid())

ui/src/api/system/user-manage.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ const getSystemDefaultPassword: (
101101
}
102102

103103

104+
/**
105+
* 获取校验
106+
* @param valid_type 校验类型: application|knowledge|user
107+
* @param valid_count 校验数量: 5 | 50 | 2
108+
*/
109+
const getValid: (
110+
valid_type: string,
111+
valid_count: number,
112+
loading?: Ref<boolean>
113+
) => Promise<Result<any>> = (valid_type, valid_count, loading) => {
114+
return get(`/valid/${valid_type}/${valid_count}`, undefined, loading)
115+
}
116+
104117
export default {
105118
getUserManage,
106119
putUserManage,
@@ -109,5 +122,6 @@ export default {
109122
putUserManagePassword,
110123
resetPassword,
111124
resetCurrentPassword,
112-
getSystemDefaultPassword
125+
getSystemDefaultPassword,
126+
getValid
113127
}

ui/src/locales/lang/en-US/views/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
label: 'Username',
66
placeholder: 'Please enter username',
77
requiredMessage: 'Please enter username',
8-
lengthMessage: 'Length must be between 6 and 20 words',
8+
lengthMessage: 'Length must be between 4 and 20 words',
99
},
1010
password: {
1111
label: 'Login Password',

ui/src/locales/lang/en-US/views/user-manage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default {
2626
nick_name: {
2727
label: 'Name',
2828
placeholder: 'Please enter name',
29+
lengthMessage: 'Length must be between 2 and 20 characters',
2930
},
3031
phone: {
3132
label: 'Phone',

0 commit comments

Comments
 (0)