Skip to content

Commit c0ea76d

Browse files
committed
feat: implement resource chat user and user group management APIs with serializers and SQL queries
1 parent bdc4db3 commit c0ea76d

13 files changed

Lines changed: 903 additions & 12 deletions

‎apps/homepage/serializers/homepage.py‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,9 @@ def aggregation(self, auth, with_valid=True):
289289
auth, data["user_id"], data.get("workspace_id")
290290
),
291291
)
292-
return {
293-
"total_tokens": query.aggregate(
294-
total_tokens=Coalesce(Sum(F("message_tokens") + F("answer_tokens"), output_field=IntegerField()), 0)
295-
)["total_tokens"]
296-
}
292+
return query.aggregate(
293+
total_tokens=Coalesce(Sum(F("message_tokens") + F("answer_tokens"), output_field=IntegerField()), 0)
294+
)["total_tokens"]
297295

298296
class ChatRecordAggregation(serializers.Serializer):
299297
workspace_id = serializers.CharField(required=False, allow_null=True, label=_("Workspace ID"))
@@ -312,7 +310,7 @@ def aggregation(self, auth, with_valid=True):
312310
auth, data["user_id"], data.get("workspace_id")
313311
),
314312
)
315-
return {"total_count": query.aggregate(total_count=Count("id"))["total_count"]}
313+
return {query.aggregate(total_count=Count("id"))["total_count"]}
316314

317315
class ApplicationTokensRanking(serializers.Serializer):
318316
workspace_id = serializers.CharField(required=False, allow_null=True, label=_("Workspace ID"))
@@ -371,7 +369,7 @@ def ranking(self, auth, current_page, page_size, with_valid=True):
371369
def export(self, auth, with_valid=True):
372370
if with_valid:
373371
self.is_valid(raise_exception=True)
374-
tokens_total = SystemHomePageSerializer.TokensAggregation(data=self.data).aggregation(auth)["total_tokens"]
372+
tokens_total = SystemHomePageSerializer.TokensAggregation(data=self.data).aggregation(auth)
375373
queryset = self.get_queryset(auth, with_valid=False)
376374
workbook = openpyxl.Workbook(write_only=True)
377375
worksheet = workbook.create_sheet(title="Sheet1")
@@ -561,7 +559,7 @@ def ranking(self, auth, current_page, page_size, with_valid=True):
561559
def export(self, auth, with_valid=True):
562560
if with_valid:
563561
self.is_valid(raise_exception=True)
564-
token_count = SystemHomePageSerializer.TokensAggregation(data=self.data).aggregation(auth)["total_tokens"]
562+
token_count = SystemHomePageSerializer.TokensAggregation(data=self.data).aggregation(auth)
565563
queryset, asker_map = self.get_queryset(auth, with_valid=False)
566564
workbook = openpyxl.Workbook(write_only=True)
567565
worksheet = workbook.create_sheet(title="Sheet1")
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# coding=utf-8
2+
"""
3+
@project: MaxKB-xpack
4+
@Author:虎虎
5+
@file: resource_chat_user.py
6+
@date:2025/6/5 16:22
7+
@desc:
8+
"""
9+
10+
from drf_spectacular.types import OpenApiTypes
11+
from drf_spectacular.utils import OpenApiParameter
12+
13+
from common.mixins.api_mixin import APIMixin
14+
from common.result import ResultSerializer, ResultPageSerializer
15+
from system_manage.serializers.resource_chat_user import (
16+
ResourceChatUserResultSerializer,
17+
ResourceChatUserEditSerializer,
18+
)
19+
20+
21+
class ResourceChatUserListResult(ResultSerializer):
22+
def get_data(self):
23+
return ResourceChatUserResultSerializer(many=True)
24+
25+
26+
class ResourceChatUserPageListResult(ResultPageSerializer):
27+
def get_data(self):
28+
return ResourceChatUserResultSerializer(many=True)
29+
30+
31+
class WorkspaceResourceChatUserListAPI(APIMixin):
32+
@staticmethod
33+
def get_parameters():
34+
return [
35+
OpenApiParameter(
36+
name="workspace_id",
37+
description="工作空间id",
38+
type=OpenApiTypes.STR,
39+
location="path",
40+
required=True,
41+
),
42+
OpenApiParameter(
43+
name="resource_type",
44+
description="资源类型",
45+
type=OpenApiTypes.STR,
46+
location="path",
47+
required=True,
48+
),
49+
OpenApiParameter(
50+
name="resource_id",
51+
description="资源id",
52+
type=OpenApiTypes.STR,
53+
location="path",
54+
required=True,
55+
),
56+
OpenApiParameter(
57+
name="user_group_id",
58+
description="用户组id",
59+
type=OpenApiTypes.STR,
60+
location="path",
61+
required=False,
62+
),
63+
OpenApiParameter(
64+
name="username",
65+
description="用户名",
66+
type=OpenApiTypes.STR,
67+
required=False,
68+
),
69+
OpenApiParameter(
70+
name="nick_name",
71+
description="昵称",
72+
type=OpenApiTypes.STR,
73+
required=False,
74+
),
75+
OpenApiParameter(
76+
name="source",
77+
description="来源",
78+
type=OpenApiTypes.STR,
79+
required=False,
80+
),
81+
]
82+
83+
@staticmethod
84+
def get_response():
85+
return ResourceChatUserListResult
86+
87+
88+
class ResourceChatUserPageAPI(APIMixin):
89+
@staticmethod
90+
def get_parameters():
91+
return WorkspaceResourceChatUserListAPI.get_parameters()
92+
93+
@staticmethod
94+
def get_response():
95+
return ResourceChatUserPageListResult
96+
97+
98+
class EditResourceChatUserAPI(APIMixin):
99+
@staticmethod
100+
def get_parameters():
101+
return [
102+
OpenApiParameter(
103+
name="workspace_id",
104+
description="工作空间id",
105+
type=OpenApiTypes.STR,
106+
location="path",
107+
required=True,
108+
),
109+
OpenApiParameter(
110+
name="resource_type",
111+
description="资源类型",
112+
type=OpenApiTypes.STR,
113+
location="path",
114+
required=True,
115+
),
116+
OpenApiParameter(
117+
name="resource_id",
118+
description="资源id",
119+
type=OpenApiTypes.STR,
120+
location="path",
121+
required=True,
122+
),
123+
OpenApiParameter(
124+
name="user_group_id",
125+
description="用户组id",
126+
type=OpenApiTypes.STR,
127+
location="path",
128+
required=False,
129+
),
130+
]
131+
132+
@staticmethod
133+
def get_request():
134+
return ResourceChatUserEditSerializer
135+
136+
@staticmethod
137+
def get_response():
138+
pass
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from drf_spectacular.types import OpenApiTypes
2+
from drf_spectacular.utils import OpenApiParameter
3+
from rest_framework import serializers
4+
from django.utils.translation import gettext_lazy as _
5+
6+
from common.mixins.api_mixin import APIMixin
7+
from common.result import ResultPageSerializer
8+
from system_manage.models import UserGroup
9+
from system_manage.serializers.resource_chat_user_group import ResourceChatUserGroupEditSerializer
10+
11+
12+
class ResourceChatUserGroupListAPI(APIMixin):
13+
@staticmethod
14+
def get_parameters():
15+
return [
16+
OpenApiParameter(
17+
name="workspace_id",
18+
description="工作空间id",
19+
type=OpenApiTypes.STR,
20+
location="path",
21+
required=True,
22+
),
23+
OpenApiParameter(
24+
name="resource_type",
25+
description="资源类型",
26+
type=OpenApiTypes.STR,
27+
location="path",
28+
required=True,
29+
),
30+
OpenApiParameter(
31+
name="resource_id",
32+
description="资源id",
33+
type=OpenApiTypes.STR,
34+
location="path",
35+
required=True,
36+
),
37+
OpenApiParameter(
38+
name="user_group_name",
39+
description="用户组名",
40+
type=OpenApiTypes.STR,
41+
required=False,
42+
),
43+
]
44+
45+
46+
class ResourceChatUserResultSerializer(serializers.ModelSerializer):
47+
is_auth = serializers.BooleanField(required=True, label=_("is auth"))
48+
49+
class Meta:
50+
model = UserGroup
51+
fields = "__all__"
52+
53+
54+
class ResourceChatUserGroupEditResult(ResultPageSerializer):
55+
def get_data(self):
56+
return ResourceChatUserResultSerializer()
57+
58+
59+
class ResourceChatUserGroupEditAPI(APIMixin):
60+
@staticmethod
61+
def get_parameters():
62+
return [
63+
OpenApiParameter(
64+
name="workspace_id",
65+
description="工作空间id",
66+
type=OpenApiTypes.STR,
67+
location="path",
68+
required=True,
69+
),
70+
OpenApiParameter(
71+
name="resource_type",
72+
description="资源类型",
73+
type=OpenApiTypes.STR,
74+
location="path",
75+
required=True,
76+
),
77+
OpenApiParameter(
78+
name="resource_id",
79+
description="资源id",
80+
type=OpenApiTypes.STR,
81+
location="path",
82+
required=True,
83+
),
84+
]
85+
86+
@staticmethod
87+
def get_request():
88+
return ResourceChatUserGroupEditSerializer
89+
90+
@staticmethod
91+
def get_response():
92+
return

‎apps/system_manage/serializers/chat_user_serializer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.utils.translation import gettext_lazy as _
55
from rest_framework import serializers
66

7-
from application.models import ApplicationAccessToken, ChatUserType
7+
from application.models import ApplicationAccessToken
88
from common.auth.common import ChatToken
99
from common.auth.constants.operate_constants import Operate
1010
from common.constants.authentication_type import AuthenticationType

0 commit comments

Comments
 (0)