-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: application list #3160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: application list #3160
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| @desc: | ||
| """ | ||
| import hashlib | ||
| import os | ||
| import re | ||
| from typing import Dict | ||
|
|
||
|
|
@@ -17,10 +18,15 @@ | |
| from django.utils.translation import gettext_lazy as _ | ||
| from rest_framework import serializers | ||
|
|
||
| from application.models.application import Application, ApplicationTypeChoices, ApplicationKnowledgeMapping | ||
| from application.models.application import Application, ApplicationTypeChoices, ApplicationKnowledgeMapping, \ | ||
| ApplicationFolder | ||
| from application.models.application_access_token import ApplicationAccessToken | ||
| from common.database_model_manage.database_model_manage import DatabaseModelManage | ||
| from common.db.search import native_search, native_page_search | ||
| from common.exception.app_exception import AppApiException | ||
| from common.utils.common import get_file_content | ||
| from knowledge.models import Knowledge | ||
| from maxkb.conf import PROJECT_DIR | ||
| from models_provider.models import Model | ||
|
|
||
|
|
||
|
|
@@ -227,11 +233,85 @@ def to_application_model(user_id: str, application: Dict): | |
| ) | ||
|
|
||
|
|
||
| class ApplicationQueryRequest(serializers.Serializer): | ||
| folder_id = serializers.CharField(required=False, label=_("folder id")) | ||
| name = serializers.CharField(required=False, label=_('Application Name')) | ||
| desc = serializers.CharField(required=False, label=_("Application Description")) | ||
| user_id = serializers.UUIDField(required=False, label=_("User ID")) | ||
|
|
||
|
|
||
| class ApplicationListResponse(serializers.Serializer): | ||
| id = serializers.CharField(required=True, label=_("Primary key id"), help_text=_("Primary key id")) | ||
| name = serializers.CharField(required=True, label=_("Application Name"), help_text=_("Application Name")) | ||
| desc = serializers.CharField(required=True, label=_("Application Description"), | ||
| help_text=_("Application Description")) | ||
| is_publish = serializers.BooleanField(required=True, label=_("Model id"), help_text=_("Model id")) | ||
| type = serializers.CharField(required=True, label=_("Application type"), help_text=_("Application type")) | ||
| resource_type = serializers.CharField(required=True, label=_("Resource type"), help_text=_("Resource type")) | ||
| user_id = serializers.CharField(required=True, label=_('Affiliation user'), help_text=_("Affiliation user")) | ||
| create_time = serializers.CharField(required=True, label=_('Creation time'), help_text=_("Creation time")) | ||
| update_time = serializers.CharField(required=True, label=_('Modification time'), help_text=_("Modification time")) | ||
|
|
||
|
|
||
| class Query(serializers.Serializer): | ||
| workspace_id = serializers.CharField(required=False, label=_('workspace id')) | ||
|
|
||
| def get_query_set(self, instance: Dict): | ||
| folder_query_set = QuerySet(ApplicationFolder) | ||
| application_query_set = QuerySet(Application) | ||
| workspace_id = self.data.get('workspace_id') | ||
| user_id = instance.get('user_id') | ||
| desc = instance.get('desc') | ||
| name = instance.get('name') | ||
| if workspace_id is not None: | ||
| folder_query_set = folder_query_set.filter(workspace_id=workspace_id) | ||
| application_query_set = application_query_set.filter(workspace_id=workspace_id) | ||
| if user_id is not None: | ||
| folder_query_set = folder_query_set.filter(user_id=user_id) | ||
| application_query_set = application_query_set.filter(user_id=user_id) | ||
| folder_id = instance.get('folder_id') | ||
| if folder_id is not None: | ||
| folder_query_set = folder_query_set.filter(parent=folder_id) | ||
| application_query_set = application_query_set.filter(folder_id=folder_id) | ||
| if name is not None: | ||
| folder_query_set = folder_query_set.filter(name__contains=name) | ||
| application_query_set = application_query_set.filter(name__contains=name) | ||
| if desc is not None: | ||
| folder_query_set = folder_query_set.filter(desc__contains=desc) | ||
| application_query_set = application_query_set.filter(desc__contains=desc) | ||
| application_query_set = application_query_set.order_by("-update_time") | ||
| return { | ||
| 'folder_query_set': folder_query_set, | ||
| 'application_query_set': application_query_set | ||
| } | ||
|
|
||
| @staticmethod | ||
| def is_x_pack_ee(): | ||
| workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping") | ||
| role_permission_mapping_model = DatabaseModelManage.get_model("role_permission_mapping_model") | ||
| return workspace_user_role_mapping_model is not None and role_permission_mapping_model is not None | ||
|
|
||
| def list(self, instance: Dict): | ||
| self.is_valid(raise_exception=True) | ||
| ApplicationQueryRequest(data=instance).is_valid(raise_exception=True) | ||
| return native_search(self.get_query_set(instance), select_string=get_file_content( | ||
| os.path.join(PROJECT_DIR, "apps", "application", 'sql', | ||
| 'list_application_ee.sql' if self.is_x_pack_ee() else 'list_application.sql'))) | ||
|
|
||
| def page(self, current_page: int, page_size: int, instance: Dict): | ||
| self.is_valid(raise_exception=True) | ||
| ApplicationQueryRequest(data=instance).is_valid(raise_exception=True) | ||
| return native_page_search(current_page, page_size, self.get_query_set(instance), get_file_content( | ||
| os.path.join(PROJECT_DIR, "apps", "application", 'sql', | ||
| 'list_application_ee.sql' if self.is_x_pack_ee() else 'list_application.sql')), | ||
| ) | ||
|
|
||
|
|
||
| class ApplicationSerializer(serializers.Serializer): | ||
| workspace_id = serializers.CharField(required=True, label=_('workspace id')) | ||
| user_id = serializers.UUIDField(required=True, label=_("User ID")) | ||
|
|
||
| def insert(self, instance: Dict, with_valid=True): | ||
| def insert(self, instance: Dict): | ||
| application_type = instance.get('type') | ||
| if 'WORK_FLOW' == application_type: | ||
| return self.insert_workflow(instance) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are the key points to consider when reviewing the provided code:
Suggestions for Optimization / Clean-up
These areas may warrant further attention based on specific requirements context beyond what was highlighted here. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| select * | ||
| from (select "id"::text, | ||
| "name", | ||
| "desc", | ||
| "is_publish", | ||
| "type", | ||
| 'application' as "resource_type", | ||
| "workspace_id", | ||
| "folder_id", | ||
| "user_id", | ||
| "create_time", | ||
| "update_time" | ||
| from application | ||
| where id in (select target | ||
| from workspace_user_resource_permission | ||
| where auth_target_type = 'APPLICATION' | ||
| and 'VIEW' = any (permission_list)) | ||
| UNION | ||
| select "id", | ||
| "name", | ||
| "desc", | ||
| true as "is_publish", | ||
| 'folder' as "type", | ||
| 'folder' as "resource_type", | ||
| "workspace_id", | ||
| "parent_id" as "folder_id", | ||
| "user_id", | ||
| "create_time", | ||
| "update_time" | ||
| from application_folder ${folder_query_set}) temp | ||
| ${application_query_set} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| select * | ||
| from (select "id"::text, | ||
| "name", | ||
| "desc", | ||
| "is_publish", | ||
| "type", | ||
| 'application' as "resource_type", | ||
| "workspace_id", | ||
| "folder_id", | ||
| "user_id", | ||
| "create_time", | ||
| "update_time" | ||
| from application | ||
| where id in (select target | ||
| from workspace_user_resource_permission | ||
| where auth_target_type = 'APPLICATION' | ||
| and case | ||
| when auth_type = 'ROLE' then | ||
| 'APPLICATION_READ' in (select permission_id | ||
| from role_permission | ||
| where role_id in (select role_id | ||
| from user_role_relation)) | ||
| else | ||
| 'VIEW' = any (permission_list) | ||
| end) | ||
| UNION | ||
| select "id", | ||
| "name", | ||
| "desc", | ||
| true as "is_publish", | ||
| 'folder' as "type", | ||
| 'folder' as "resource_type", | ||
| "workspace_id", | ||
| "parent_id" as "folder_id", | ||
| "user_id", | ||
| "create_time", | ||
| "update_time" | ||
| from application_folder ${folder_query_set}) temp | ||
| ${application_query_set} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,10 +11,12 @@ | |
| from rest_framework.request import Request | ||
| from rest_framework.views import APIView | ||
|
|
||
| from application.api.application_api import ApplicationCreateAPI | ||
| from application.serializers.application import ApplicationSerializer | ||
| from application.api.application_api import ApplicationCreateAPI, ApplicationQueryAPI | ||
| from application.serializers.application import ApplicationSerializer, Query | ||
| from common import result | ||
| from common.auth import TokenAuth | ||
| from common.auth.authentication import has_permissions | ||
| from common.constants.permission_constants import PermissionConstants | ||
|
|
||
|
|
||
| class Application(APIView): | ||
|
|
@@ -30,6 +32,38 @@ class Application(APIView): | |
| responses=ApplicationCreateAPI.get_response(), | ||
| tags=[_('Application')] # type: ignore | ||
| ) | ||
| @has_permissions(PermissionConstants.APPLICATION_READ.get_workspace_permission()) | ||
| def post(self, request: Request, workspace_id: str): | ||
| return result.success( | ||
| ApplicationSerializer(data={'workspace_id': workspace_id, 'user_id': request.user.id}).insert(request.data)) | ||
|
|
||
| @extend_schema( | ||
| methods=['GET'], | ||
| description=_('Get the application list'), | ||
| summary=_('Get the application list'), | ||
| operation_id=_('Get the application list'), # type: ignore | ||
| parameters=ApplicationQueryAPI.get_parameters(), | ||
| responses=ApplicationQueryAPI.get_response(), | ||
| tags=[_('Application')] # type: ignore | ||
| ) | ||
| @has_permissions(PermissionConstants.APPLICATION_READ.get_workspace_permission()) | ||
| def get(self, request: Request, workspace_id: str): | ||
| return result.success(Query(data={'workspace_id': workspace_id, 'user_id': request.user.id}).list(request.data)) | ||
|
|
||
| class Page(APIView): | ||
| authentication_classes = [TokenAuth] | ||
|
|
||
| @extend_schema( | ||
| methods=['GET'], | ||
| description=_('Get the application list by page'), | ||
| summary=_('Get the application list by page'), | ||
| operation_id=_('Get the application list by page'), # type: ignore | ||
| parameters=ApplicationQueryAPI.get_parameters(), | ||
| responses=ApplicationQueryAPI.get_page_response(), | ||
| tags=[_('Application')] # type: ignore | ||
| ) | ||
| @has_permissions(PermissionConstants.APPLICATION_READ.get_workspace_permission()) | ||
| def get(self, request: Request, workspace_id: str, current_page: int, page_size: int): | ||
| return result.success( | ||
| Query(data={'workspace_id': workspace_id, 'user_id': request.user.id}).page(current_page, page_size, | ||
| request.query_params)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no major discrepancies with this Python code that requires adjustments according to your specified criteria. The function of the class and its members seems well-defined and optimized for their intended functionality within an RESTful framework. However, here is a few minor points for review: Minor Changes
Final RecommendationsThe provided script looks clean and functional within typical RESTful API requirements. For further optimizations, I'd recommend leveraging Django Rest Framework extensions, reviewing existing permissions management practices to optimize security measures, and ensuring robust response serialization techniques. If more context about how this code will specifically interact with a database or additional middleware layers would aid in suggesting tailored performance tweaks, feel free to add! |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a concise review of the provided code, including potential issues and areas for optimization:
Potential Issues and Recommendations
Naming Conventions: Use lowercase with underscores rather than camel case for functions and variables to adhere to Python naming conventions.
Class Imports: Remove unnecessary imports that can increase file size without added functionality.
import openai: This is not used anywhere in the snippet.APIMixininstead of usingfrom ... import *.Documentation Consistency: Ensure consistent use of Markdown comments throughout the codebase for better readability.
Code Readability: Minor formatting improvements such as correct indentation might enhance readability.
Variable Naming: Consider renaming
get_data()to more descriptive names that convey its purpose.These changes should make the codebase cleaner and potentially reduce any bugs due to incorrect handling or inefficient execution patterns caused by these minor optimizations.