|
11 | 11 | import uuid_utils.compat as uuid |
12 | 12 | from django.core import signing |
13 | 13 | from django.core.cache import cache |
14 | | -from django.db.models import Exists, OuterRef |
15 | 14 | from django.utils.translation import gettext_lazy as _ |
16 | 15 | from rest_framework import serializers |
17 | 16 |
|
18 | | -from application.models import Application, Chat |
19 | | -from application.models.application_access_token import ApplicationAccessToken |
20 | 17 | from common.auth.common import FileToken |
21 | 18 | from common.constants.cache_version import Cache_Version |
22 | 19 | from common.database_model_manage.database_model_manage import DatabaseModelManage |
23 | | -from common.db.search import page_search |
24 | 20 | from common.exception.app_exception import AppApiException |
25 | 21 | from common.log.log import record_log |
26 | 22 | from common.utils.common import password_verify, needs_password_upgrade, password_encrypt |
|
30 | 26 | from portal.models import Portal |
31 | 27 | from system_manage.models.chat_user import ( |
32 | 28 | ChatUser, |
33 | | - ResourceChatUserAuthorize, |
34 | | - ResourceChatUserGroupAuthorize, |
35 | | - ResourceType, |
36 | | - UserGroupRelation, |
37 | 29 | ) |
38 | 30 | from users.serializers.login import LoginRequest |
39 | 31 |
|
@@ -116,110 +108,6 @@ def edit(self, instance, with_valid=True): |
116 | 108 | return PortalSerializer.Model(portal).data |
117 | 109 |
|
118 | 110 |
|
119 | | -class PortalApplicationAuthMixin: |
120 | | - """门户应用授权过滤公共逻辑""" |
121 | | - |
122 | | - @staticmethod |
123 | | - def get_authorized_application_queryset(user_id): |
124 | | - public_apps = ApplicationAccessToken.objects.filter(application_id=OuterRef("id"), authentication=False) |
125 | | - if not ChatUser.objects.filter(id=user_id).exists(): |
126 | | - return Application.objects.filter(Exists(public_apps)) |
127 | | - authed_token_exists = ApplicationAccessToken.objects.filter(application_id=OuterRef("id"), authentication=True) |
128 | | - direct_auth = ResourceChatUserAuthorize.objects.filter( |
129 | | - resource_id=OuterRef("id"), resource_type=ResourceType.APPLICATION.value, is_auth=True, user_id=user_id |
130 | | - ) |
131 | | - user_groups = UserGroupRelation.objects.filter(user_id=user_id).values_list("group_id", flat=True) |
132 | | - group_auth = ResourceChatUserGroupAuthorize.objects.filter( |
133 | | - resource_id=OuterRef("id"), |
134 | | - resource_type=ResourceType.APPLICATION.value, |
135 | | - is_auth=True, |
136 | | - user_group_id__in=user_groups, |
137 | | - ) |
138 | | - return Application.objects.filter( |
139 | | - Exists(public_apps) | (Exists(authed_token_exists) & (Exists(direct_auth) | Exists(group_auth))) |
140 | | - ) |
141 | | - |
142 | | - |
143 | | -class ApplicationResponseSerializer(serializers.Serializer): |
144 | | - id = serializers.CharField(required=True) |
145 | | - name = serializers.CharField(required=True) |
146 | | - desc = serializers.CharField(required=True) |
147 | | - icon = serializers.CharField(required=True) |
148 | | - type = serializers.CharField(required=True) |
149 | | - dialogue_number = serializers.IntegerField(required=True) |
150 | | - prologue = serializers.CharField(required=True) |
151 | | - is_publish = serializers.BooleanField(required=True) |
152 | | - |
153 | | - |
154 | | -class PortalApplicationSerializer(serializers.Serializer): |
155 | | - class Query(PortalApplicationAuthMixin, serializers.Serializer): |
156 | | - name = serializers.CharField( |
157 | | - required=False, allow_blank=True, label=_("Application Name"), help_text=_("Application name") |
158 | | - ) |
159 | | - |
160 | | - def get_query_set(self): |
161 | | - queryset = Application.objects.filter(is_publish=True) |
162 | | - name = self.data.get("name") |
163 | | - if name: |
164 | | - queryset = queryset.filter(name__icontains=name) |
165 | | - return queryset.order_by("-create_time") |
166 | | - |
167 | | - def page(self, current_page, page_size, user_id, with_valid=True): |
168 | | - if with_valid: |
169 | | - self.is_valid(raise_exception=True) |
170 | | - queryset = self.get_query_set() |
171 | | - queryset = queryset.filter(id__in=self.get_authorized_application_queryset(user_id).values("id")) |
172 | | - return page_search( |
173 | | - current_page, |
174 | | - page_size, |
175 | | - queryset, |
176 | | - post_records_handler=lambda app: ApplicationResponseSerializer(app).data, |
177 | | - ) |
178 | | - |
179 | | - |
180 | | -class PortalHistoricalConversationResponseSerializer(serializers.Serializer): |
181 | | - id = serializers.CharField(required=True) |
182 | | - abstract = serializers.CharField(required=True) |
183 | | - create_time = serializers.CharField(required=True) |
184 | | - update_time = serializers.CharField(required=True) |
185 | | - application = serializers.SerializerMethodField() |
186 | | - |
187 | | - def get_application(self, chat): |
188 | | - return { |
189 | | - "id": str(chat.application_id), |
190 | | - "name": chat.application.name, |
191 | | - "icon": chat.application.icon, |
192 | | - } |
193 | | - |
194 | | - |
195 | | -class PortalHistoricalConversationSerializer(serializers.Serializer): |
196 | | - class Query(PortalApplicationAuthMixin, serializers.Serializer): |
197 | | - name = serializers.CharField( |
198 | | - required=False, allow_blank=True, label=_("Application Name"), help_text=_("Application name") |
199 | | - ) |
200 | | - |
201 | | - def get_query_set(self, user_id): |
202 | | - queryset = Chat.objects.filter( |
203 | | - chat_user_id=user_id, |
204 | | - is_deleted=False, |
205 | | - application_id__in=self.get_authorized_application_queryset(user_id).values("id"), |
206 | | - ) |
207 | | - name = self.data.get("name") |
208 | | - if name: |
209 | | - queryset = queryset.filter(application__name__icontains=name) |
210 | | - return queryset.select_related("application").order_by("-update_time", "id") |
211 | | - |
212 | | - def page(self, current_page, page_size, user_id, with_valid=True): |
213 | | - if with_valid: |
214 | | - self.is_valid(raise_exception=True) |
215 | | - return page_search( |
216 | | - current_page, |
217 | | - page_size, |
218 | | - self.get_query_set(user_id), |
219 | | - post_records_handler=lambda chat: PortalHistoricalConversationResponseSerializer(chat).data, |
220 | | - ) |
221 | | - |
222 | | - |
223 | 111 | class PortalLoginSerializer(serializers.Serializer): |
224 | 112 | @staticmethod |
225 | 113 | def login(instance): |
|
0 commit comments