1010import uuid_utils .compat as uuid
1111from django .core import signing
1212from django .core .cache import cache
13- from django .db .models import Exists , OuterRef , QuerySet
13+ from django .db .models import Exists , OuterRef
1414from django .utils .translation import gettext_lazy as _
1515from rest_framework import serializers
1616
17- from application .models import Application
17+ from application .models import Application , Chat
1818from application .models .application_access_token import ApplicationAccessToken
1919from common .auth .common import FileToken
2020from common .constants .authentication_type import AuthenticationType
@@ -80,7 +80,7 @@ def _handle_file_field(self, portal, field_name, value):
8080 old_url = getattr (portal , field_name )
8181 if old_url :
8282 old_file_id = old_url .split ("/" )[- 1 ]
83- QuerySet ( File ) .filter (id = old_file_id ).delete ()
83+ File . objects .filter (id = old_file_id ).delete ()
8484 new_url = self ._upload_file (value )
8585 setattr (portal , field_name , new_url )
8686 else :
@@ -103,6 +103,42 @@ def edit(self, instance, with_valid=True):
103103 return PortalSerializer .Model (portal ).data
104104
105105
106+ class PortalApplicationAuthMixin :
107+ """门户应用授权过滤公共逻辑"""
108+
109+ @staticmethod
110+ def get_authorized_application_queryset (user_id ):
111+ public_apps = ApplicationAccessToken .objects .filter (
112+ application_id = OuterRef ('id' ),
113+ authentication = False
114+ )
115+ if not ChatUser .objects .filter (id = user_id ).exists ():
116+ return Application .objects .filter (Exists (public_apps ))
117+ authed_token_exists = ApplicationAccessToken .objects .filter (
118+ application_id = OuterRef ('id' ),
119+ authentication = True
120+ )
121+ direct_auth = ResourceChatUserAuthorize .objects .filter (
122+ resource_id = OuterRef ('id' ),
123+ resource_type = ResourceType .APPLICATION .value ,
124+ is_auth = True ,
125+ user_id = user_id
126+ )
127+ user_groups = UserGroupRelation .objects .filter (
128+ user_id = user_id
129+ ).values_list ('group_id' , flat = True )
130+ group_auth = ResourceChatUserGroupAuthorize .objects .filter (
131+ resource_id = OuterRef ('id' ),
132+ resource_type = ResourceType .APPLICATION .value ,
133+ is_auth = True ,
134+ user_group_id__in = user_groups
135+ )
136+ return Application .objects .filter (
137+ Exists (public_apps ) |
138+ (Exists (authed_token_exists ) & (Exists (direct_auth ) | Exists (group_auth )))
139+ )
140+
141+
106142class ApplicationResponseSerializer (serializers .Serializer ):
107143 id = serializers .CharField (required = True )
108144 name = serializers .CharField (required = True )
@@ -116,62 +152,72 @@ class ApplicationResponseSerializer(serializers.Serializer):
116152
117153class PortalApplicationSerializer (serializers .Serializer ):
118154
119- class Query (serializers .Serializer ):
155+ class Query (PortalApplicationAuthMixin , serializers .Serializer ):
120156 name = serializers .CharField (required = False , allow_blank = True , label = _ ('Application Name' ),
121157 help_text = _ ('Application name' ))
122158
123159 def get_query_set (self ):
124- queryset = Application .objects .filter (is_publish = True ).only (
125- 'id' , 'name' , 'desc' , 'icon' , 'type' ,
126- 'dialogue_number' , 'prologue' , 'is_publish' , 'create_time'
127- )
160+ queryset = Application .objects .filter (is_publish = True )
128161 name = self .data .get ('name' )
129162 if name :
130163 queryset = queryset .filter (name__icontains = name )
131164 return queryset .order_by ('-create_time' )
132165
133- def _apply_auth_filter (self , queryset , user_id ):
134- chat_user_exists = ChatUser .objects .filter (id = user_id ).exists ()
135- public_apps = ApplicationAccessToken .objects .filter (
136- application_id = OuterRef ('id' ),
137- authentication = False
138- )
139- if not chat_user_exists :
140- return queryset .filter (Exists (public_apps ))
141- authed_token_exists = ApplicationAccessToken .objects .filter (
142- application_id = OuterRef ('id' ),
143- authentication = True
144- )
145- direct_auth = ResourceChatUserAuthorize .objects .filter (
146- resource_id = OuterRef ('id' ),
147- resource_type = ResourceType .APPLICATION .value ,
148- is_auth = True ,
149- user_id = user_id
166+ def page (self , current_page , page_size , user_id , with_valid = True ):
167+ if with_valid :
168+ self .is_valid (raise_exception = True )
169+ queryset = self .get_query_set ()
170+ queryset = queryset .filter (
171+ id__in = self .get_authorized_application_queryset (user_id ).values ('id' )
150172 )
151- user_groups = UserGroupRelation .objects .filter (
152- user_id = user_id
153- ).values_list ('group_id' , flat = True )
154- group_auth = ResourceChatUserGroupAuthorize .objects .filter (
155- resource_id = OuterRef ('id' ),
156- resource_type = ResourceType .APPLICATION .value ,
157- is_auth = True ,
158- user_group_id__in = user_groups
173+ return page_search (
174+ current_page ,
175+ page_size ,
176+ queryset ,
177+ post_records_handler = lambda app : ApplicationResponseSerializer (app ).data ,
159178 )
160- return queryset .filter (
161- Exists (public_apps ) |
162- (Exists (authed_token_exists ) & (Exists (direct_auth ) | Exists (group_auth )))
179+
180+
181+ class PortalHistoricalConversationResponseSerializer (serializers .Serializer ):
182+ id = serializers .CharField (required = True )
183+ abstract = serializers .CharField (required = True )
184+ create_time = serializers .CharField (required = True )
185+ update_time = serializers .CharField (required = True )
186+ application = serializers .SerializerMethodField ()
187+
188+ def get_application (self , chat ):
189+ return {
190+ 'id' : str (chat .application_id ),
191+ 'name' : chat .application .name ,
192+ 'icon' : chat .application .icon ,
193+ }
194+
195+
196+ class PortalHistoricalConversationSerializer (serializers .Serializer ):
197+
198+ class Query (PortalApplicationAuthMixin , serializers .Serializer ):
199+ name = serializers .CharField (required = False , allow_blank = True , label = _ ('Application Name' ),
200+ help_text = _ ('Application name' ))
201+
202+ def get_query_set (self , user_id ):
203+ queryset = Chat .objects .filter (
204+ chat_user_id = user_id ,
205+ is_deleted = False ,
206+ application_id__in = self .get_authorized_application_queryset (user_id ).values ('id' )
163207 )
208+ name = self .data .get ('name' )
209+ if name :
210+ queryset = queryset .filter (application__name__icontains = name )
211+ return queryset .select_related ('application' ).order_by ('-update_time' , 'id' )
164212
165213 def page (self , current_page , page_size , user_id , with_valid = True ):
166214 if with_valid :
167215 self .is_valid (raise_exception = True )
168- queryset = self .get_query_set ()
169- queryset = self ._apply_auth_filter (queryset , user_id )
170216 return page_search (
171217 current_page ,
172218 page_size ,
173- queryset ,
174- post_records_handler = lambda app : ApplicationResponseSerializer ( app ).data ,
219+ self . get_query_set ( user_id ) ,
220+ post_records_handler = lambda chat : PortalHistoricalConversationResponseSerializer ( chat ).data ,
175221 )
176222
177223
0 commit comments