|
7 | 7 | @desc:
|
8 | 8 | """
|
9 | 9 | import hashlib
|
| 10 | +import os |
10 | 11 | import re
|
11 | 12 | from typing import Dict
|
12 | 13 |
|
|
17 | 18 | from django.utils.translation import gettext_lazy as _
|
18 | 19 | from rest_framework import serializers
|
19 | 20 |
|
20 |
| -from application.models.application import Application, ApplicationTypeChoices, ApplicationKnowledgeMapping |
| 21 | +from application.models.application import Application, ApplicationTypeChoices, ApplicationKnowledgeMapping, \ |
| 22 | + ApplicationFolder |
21 | 23 | from application.models.application_access_token import ApplicationAccessToken
|
| 24 | +from common.database_model_manage.database_model_manage import DatabaseModelManage |
| 25 | +from common.db.search import native_search, native_page_search |
22 | 26 | from common.exception.app_exception import AppApiException
|
| 27 | +from common.utils.common import get_file_content |
23 | 28 | from knowledge.models import Knowledge
|
| 29 | +from maxkb.conf import PROJECT_DIR |
24 | 30 | from models_provider.models import Model
|
25 | 31 |
|
26 | 32 |
|
@@ -227,11 +233,85 @@ def to_application_model(user_id: str, application: Dict):
|
227 | 233 | )
|
228 | 234 |
|
229 | 235 |
|
| 236 | +class ApplicationQueryRequest(serializers.Serializer): |
| 237 | + folder_id = serializers.CharField(required=False, label=_("folder id")) |
| 238 | + name = serializers.CharField(required=False, label=_('Application Name')) |
| 239 | + desc = serializers.CharField(required=False, label=_("Application Description")) |
| 240 | + user_id = serializers.UUIDField(required=False, label=_("User ID")) |
| 241 | + |
| 242 | + |
| 243 | +class ApplicationListResponse(serializers.Serializer): |
| 244 | + id = serializers.CharField(required=True, label=_("Primary key id"), help_text=_("Primary key id")) |
| 245 | + name = serializers.CharField(required=True, label=_("Application Name"), help_text=_("Application Name")) |
| 246 | + desc = serializers.CharField(required=True, label=_("Application Description"), |
| 247 | + help_text=_("Application Description")) |
| 248 | + is_publish = serializers.BooleanField(required=True, label=_("Model id"), help_text=_("Model id")) |
| 249 | + type = serializers.CharField(required=True, label=_("Application type"), help_text=_("Application type")) |
| 250 | + resource_type = serializers.CharField(required=True, label=_("Resource type"), help_text=_("Resource type")) |
| 251 | + user_id = serializers.CharField(required=True, label=_('Affiliation user'), help_text=_("Affiliation user")) |
| 252 | + create_time = serializers.CharField(required=True, label=_('Creation time'), help_text=_("Creation time")) |
| 253 | + update_time = serializers.CharField(required=True, label=_('Modification time'), help_text=_("Modification time")) |
| 254 | + |
| 255 | + |
| 256 | +class Query(serializers.Serializer): |
| 257 | + workspace_id = serializers.CharField(required=False, label=_('workspace id')) |
| 258 | + |
| 259 | + def get_query_set(self, instance: Dict): |
| 260 | + folder_query_set = QuerySet(ApplicationFolder) |
| 261 | + application_query_set = QuerySet(Application) |
| 262 | + workspace_id = self.data.get('workspace_id') |
| 263 | + user_id = instance.get('user_id') |
| 264 | + desc = instance.get('desc') |
| 265 | + name = instance.get('name') |
| 266 | + if workspace_id is not None: |
| 267 | + folder_query_set = folder_query_set.filter(workspace_id=workspace_id) |
| 268 | + application_query_set = application_query_set.filter(workspace_id=workspace_id) |
| 269 | + if user_id is not None: |
| 270 | + folder_query_set = folder_query_set.filter(user_id=user_id) |
| 271 | + application_query_set = application_query_set.filter(user_id=user_id) |
| 272 | + folder_id = instance.get('folder_id') |
| 273 | + if folder_id is not None: |
| 274 | + folder_query_set = folder_query_set.filter(parent=folder_id) |
| 275 | + application_query_set = application_query_set.filter(folder_id=folder_id) |
| 276 | + if name is not None: |
| 277 | + folder_query_set = folder_query_set.filter(name__contains=name) |
| 278 | + application_query_set = application_query_set.filter(name__contains=name) |
| 279 | + if desc is not None: |
| 280 | + folder_query_set = folder_query_set.filter(desc__contains=desc) |
| 281 | + application_query_set = application_query_set.filter(desc__contains=desc) |
| 282 | + application_query_set = application_query_set.order_by("-update_time") |
| 283 | + return { |
| 284 | + 'folder_query_set': folder_query_set, |
| 285 | + 'application_query_set': application_query_set |
| 286 | + } |
| 287 | + |
| 288 | + @staticmethod |
| 289 | + def is_x_pack_ee(): |
| 290 | + workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping") |
| 291 | + role_permission_mapping_model = DatabaseModelManage.get_model("role_permission_mapping_model") |
| 292 | + return workspace_user_role_mapping_model is not None and role_permission_mapping_model is not None |
| 293 | + |
| 294 | + def list(self, instance: Dict): |
| 295 | + self.is_valid(raise_exception=True) |
| 296 | + ApplicationQueryRequest(data=instance).is_valid(raise_exception=True) |
| 297 | + return native_search(self.get_query_set(instance), select_string=get_file_content( |
| 298 | + os.path.join(PROJECT_DIR, "apps", "application", 'sql', |
| 299 | + 'list_application_ee.sql' if self.is_x_pack_ee() else 'list_application.sql'))) |
| 300 | + |
| 301 | + def page(self, current_page: int, page_size: int, instance: Dict): |
| 302 | + self.is_valid(raise_exception=True) |
| 303 | + ApplicationQueryRequest(data=instance).is_valid(raise_exception=True) |
| 304 | + return native_page_search(current_page, page_size, self.get_query_set(instance), get_file_content( |
| 305 | + os.path.join(PROJECT_DIR, "apps", "application", 'sql', |
| 306 | + 'list_application_ee.sql' if self.is_x_pack_ee() else 'list_application.sql')), |
| 307 | + ) |
| 308 | + |
| 309 | + |
230 | 310 | class ApplicationSerializer(serializers.Serializer):
|
231 | 311 | workspace_id = serializers.CharField(required=True, label=_('workspace id'))
|
232 | 312 | user_id = serializers.UUIDField(required=True, label=_("User ID"))
|
233 | 313 |
|
234 |
| - def insert(self, instance: Dict, with_valid=True): |
| 314 | + def insert(self, instance: Dict): |
235 | 315 | application_type = instance.get('type')
|
236 | 316 | if 'WORK_FLOW' == application_type:
|
237 | 317 | return self.insert_workflow(instance)
|
|
0 commit comments