Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions apps/application/views/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@date:2025/5/26 16:51
@desc:
"""
from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema
from rest_framework.parsers import MultiPartParser
Expand All @@ -14,11 +15,22 @@

from application.api.application_api import ApplicationCreateAPI, ApplicationQueryAPI, ApplicationImportAPI, \
ApplicationExportAPI, ApplicationOperateAPI, ApplicationEditAPI
from application.models import Application
from application.serializers.application import ApplicationSerializer, Query, ApplicationOperateSerializer
from common import result
from common.auth import TokenAuth
from common.auth.authentication import has_permissions
from common.constants.permission_constants import PermissionConstants
from common.log.log import log


def get_application_operation_object(application_id):
application_model = QuerySet(model=Application).filter(id=application_id).first()
if application_model is not None:
return{
'name': application_model.name
}
return {}


class Application(APIView):
Expand All @@ -35,6 +47,8 @@ class Application(APIView):
tags=[_('Application')] # type: ignore
)
@has_permissions(PermissionConstants.APPLICATION_READ.get_workspace_permission())
@log(menu='Application', operate='Create an application',
get_operation_object=lambda r,k: {'name': r.data.get('name')})
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))
Expand Down Expand Up @@ -85,11 +99,14 @@ class Import(APIView):
tags=[_('Application')] # type: ignore
)
@has_permissions(PermissionConstants.APPLICATION_READ)
@log(menu='Application', operate="Import Application")
def post(self, request: Request, workspace_id: str):
return result.success(ApplicationSerializer(
data={'user_id': request.user.id, 'workspace_id': workspace_id,
}).import_({'file': request.FILES.get('file')}))



class Export(APIView):
authentication_classes = [TokenAuth]

Expand All @@ -104,6 +121,8 @@ class Export(APIView):
tags=[_('Application')] # type: ignore
)
@has_permissions(PermissionConstants.APPLICATION_EXPORT.get_workspace_application_permission())
@log(menu='Application', operate="Export Application",
get_operation_object=lambda r, k: get_application_operation_object(k.get('application_id')))
def post(self, request: Request, workspace_id: str, application_id: str):
return ApplicationOperateSerializer(
data={'application_id': application_id,
Expand All @@ -122,6 +141,9 @@ class Operate(APIView):
tags=[_('Application')] # type: ignore
)
@has_permissions(PermissionConstants.APPLICATION_DELETE.get_workspace_application_permission())
@log(menu='Application', operate='Deleting application',
get_operation_object=lambda r, k: get_application_operation_object(k.get('application_id'))
)
def delete(self, request: Request, workspace_id: str, application_id: str):
return result.success(ApplicationOperateSerializer(
data={'application_id': application_id, 'user_id': request.user.id}).delete(
Expand All @@ -138,6 +160,8 @@ def delete(self, request: Request, workspace_id: str, application_id: str):
tags=[_('Application')] # type: ignore
)
@has_permissions(PermissionConstants.APPLICATION_EDIT.get_workspace_application_permission())
@log(menu='Application', operate="Modify the application",
get_operation_object=lambda r, k: get_application_operation_object(k.get('application_id')))
def put(self, request: Request, workspace_id: str, application_id: str):
return result.success(
ApplicationOperateSerializer(
Expand Down Expand Up @@ -172,6 +196,9 @@ class Publish(APIView):
responses=result.DefaultResultSerializer,
tags=[_('Application')] # type: ignore
)
@log(menu='Application', operate='Publishing an application',
get_operation_object=lambda r,k: get_application_operation_object(k.get('application_id'))
)
def put(self, request: Request, application_id: str):
return result.success(
ApplicationOperateSerializer(
Expand Down
14 changes: 14 additions & 0 deletions apps/application/views/application_api_key.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
from django.db.models import QuerySet
from drf_spectacular.utils import extend_schema
from rest_framework.request import Request
from rest_framework.views import APIView
from django.utils.translation import gettext_lazy as _

from application.api.application_api_key import ApplicationKeyCreateAPI
from application.models import ApplicationApiKey
from application.serializers.application_api_key import ApplicationKeySerializer
from common.auth import TokenAuth
from common.log.log import log
from common.result import result, success


def get_application_operation_object(application_api_key_id):
application_api_key_model = QuerySet(model=ApplicationApiKey).filter(id=application_api_key_id).first()
if application_api_key_model is not None:
return {
"name": application_api_key_model.name
}
return {}


class ApplicationKey(APIView):
authentication_classes = [TokenAuth]

Expand All @@ -20,6 +32,8 @@ class ApplicationKey(APIView):
parameters=ApplicationKeyCreateAPI.get_parameters(),
tags=[_('Application Api Key')] # type: ignore
)
@log(menu='Application', operate="Add ApiKey",
get_operation_object=lambda r, k: get_application_operation_object(k.get('application_api_key_id')))
def post(self,request: Request, application_id: str, workspace_id: str):
return result.success(ApplicationKeySerializer(
data={'application_id': application_id, 'user_id': request.user.id,
Expand Down
4 changes: 4 additions & 0 deletions apps/application/views/application_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
from application.api.application_version import ApplicationVersionListAPI, ApplicationVersionPageAPI, \
ApplicationVersionAPI, ApplicationVersionOperateAPI
from application.serializers.application_version import ApplicationVersionSerializer
from application.views import get_application_operation_object
from common import result
from common.auth import TokenAuth
from common.auth.authentication import has_permissions
from common.constants.permission_constants import PermissionConstants
from common.log.log import log


class ApplicationVersionView(APIView):
Expand Down Expand Up @@ -88,6 +90,8 @@ def get(self, request: Request, workspace_id: str, application_id: str, work_flo
responses=ApplicationVersionOperateAPI.get_response(),
tags=[_('Application/Version')] # type: ignore
)
@log(menu='Application', operate="Modify application version information",
get_operation_object=lambda r, k: get_application_operation_object(k.get('application_id')))
def put(self, request: Request, workspace_id: str, application_id: str, work_flow_version_id: str):
return result.success(
ApplicationVersionSerializer.Operate(
Expand Down
26 changes: 26 additions & 0 deletions apps/folders/views/folder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema
from rest_framework.request import Request
Expand All @@ -6,11 +7,29 @@
from common.auth import TokenAuth
from common.auth.authentication import has_permissions
from common.constants.permission_constants import Permission, Group, Operate
from common.log.log import log
from common.result import result
from folders.api.folder import FolderCreateAPI, FolderEditAPI, FolderReadAPI, FolderTreeReadAPI, FolderDeleteAPI
from folders.models.folder import FolderCreateRequest, FolderEditRequest
from folders.serializers.folder import FolderSerializer, FolderTreeSerializer


def get_folder_create_operation_object(folder_name):
folder_model = QuerySet(model=FolderCreateRequest).filter(name=folder_name).first()
if folder_model is not None:
return {
'name': folder_model.name
}
return {}

def get_folder_edit_operation_object(folder_name):
folder_model = QuerySet(model=FolderEditRequest).filter(name=folder_name).first()
if folder_model is not None:
return {
'name': folder_model.name
}
return {}

class FolderView(APIView):
authentication_classes = [TokenAuth]

Expand All @@ -26,6 +45,9 @@ class FolderView(APIView):
)
@has_permissions(lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.CREATE,
resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}"))
@log(menu='folder', operate='Create folder',
get_operation_object=lambda r,k: get_folder_create_operation_object(k.get('folder_name'))
)
def post(self, request: Request, workspace_id: str, source: str):
return result.success(FolderSerializer.Create(
data={'user_id': request.user.id,
Expand Down Expand Up @@ -64,6 +86,9 @@ class Operate(APIView):
)
@has_permissions(lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.EDIT,
resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}"))
@log(menu='folder', operate='Edit folder',
get_operation_object=lambda r, k: get_folder_edit_operation_object(k.get('folder_name'))
)
def put(self, request: Request, workspace_id: str, source: str, folder_id: str):
return result.success(FolderSerializer.Operate(
data={'id': folder_id, 'workspace_id': workspace_id, 'source': source}
Expand Down Expand Up @@ -96,6 +121,7 @@ def get(self, request: Request, workspace_id: str, source: str, folder_id: str):
)
@has_permissions(lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.DELETE,
resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}"))
@log(menu='folder', operate='Delete folder')
def delete(self, request: Request, workspace_id: str, source: str, folder_id: str):
return result.success(FolderSerializer.Operate(
data={'id': folder_id, 'workspace_id': workspace_id, 'source': source}
Expand Down
14 changes: 14 additions & 0 deletions apps/knowledge/serializers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,17 @@ def or_get(exists_problem_list, content, knowledge_id, document_id, paragraph_id
problem = Problem(id=uuid.uuid7(), content=content, knowledge_id=knowledge_id)
problem_content_dict[content] = problem, True
return problem, document_id, paragraph_id



def get_knowledge_operation_object(knowledge_id: str):
knowledge_model = QuerySet(model=Knowledge).filter(id=knowledge_id).first()
if knowledge_model is not None:
return {
"name": knowledge_model.name,
"desc": knowledge_model.desc,
"type": knowledge_model.type,
"create_time": knowledge_model.create_time,
"update_time": knowledge_model.update_time
}
return {}
35 changes: 35 additions & 0 deletions apps/knowledge/views/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.db.models import QuerySet

from knowledge.models import Document



def get_document_operation_object(document_id: str):
document_model = QuerySet(model=Document).filter(id=document_id).first()
if document_model is not None:
return {
"name": document_model.name,
"type": document_model.type,
}
return {}

def get_document_operation_object_batch(document_id_list: str):
document_model_list = QuerySet(model=Document).filter(id__in=document_id_list)
if document_model_list is not None:
return {
"name": f'[{",".join([document_model.name for document_model in document_model_list])}]',
'document_list': [{'name': document_model.name, 'type': document_model.type} for document_model in
document_model_list]
}
return {}


def get_knowledge_document_operation_object(knowledge_dict: dict, document_dict: dict):
return {
'name': f'{knowledge_dict.get("name", "")}/{document_dict.get("name", "")}',
'dataset_name': knowledge_dict.get("name", ""),
'dataset_desc': knowledge_dict.get("desc", ""),
'dataset_type': knowledge_dict.get("type", ""),
'document_name': document_dict.get("name", ""),
'document_type': document_dict.get("type", ""),
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code is mostly clear and well-structured, but there are a few areas where you can make improvements for better performance and clarity:

  1. Function Naming: The function names can be more descriptive to improve readability. For example:

    • get_document_operation_object should be renamed to something like retrieve_document_details.
    • get_document_operation_object_batch could become batch_retrieve_document_details.
    • get_knowledge_document_operation_object might be clearer as compose_document_and_knowledge.
  2. Use of F-Strings: While f-strings are generally preferred, they can be overused. Replace all instances with .format() or directly interpolate strings in Python version >= 3.6.

  3. Return Statement: Ensure that each function returns a dictionary unless specified otherwise. This makes the code easier to understand and maintain.

  4. Null Check: Instead of checking if document_model is not None, use bool(document_model) which is more concise.

  5. Efficient Query Execution: Directly pass variables to method calls rather than using string concatenation within methods.

Here's the updated code incorporating these suggestions:

@@ -0,0 +1,35 @@
+from django.db.models import QuerySet
+
+from knowledge.models import Document

+
+
+def retrieve_document_details(document_id: str) -> dict:
+    document_model = QuerySet(model=Document).filter(id=document_id).first()
+    if document_model:
+        return {
+            "name": document_model.name,
+            "type": document_model.type,
+        }
+    return {}

+def batch_retrieve_document_details(document_id_list: str) -> dict:
+    document_models = QuerySet(Document).filter(id__in=document_id_list)
+    if document_models.exists():
+        return {
+            "name": ",".join(map(str, [doc.name for doc in document_models])),
+            'document_list': [
+                {'name': doc.name, 'type': doc.type} for doc in document_models
+            ],
+        }
+    return {}  

+def compose_document_and_knowledge(knowledge_dict: dict, document_dict: dict) -> dict:
+    name_part = "{}/{}".format(
        knowledge_dict.get("name", ""), 
        document_dict.get("name", "")
    )
    
    dataset_name = knowledge_dict.get("name", "")  
    dataset_desc = knowledge_dict.get("desc", "")
    dataset_type = knowledge_dict.get("type", "")
    
    document_name = document_dict.get("name", "")  
    document_type = document_dict.get("type", "")

    return {
        "name": name_part,
        "dataset_name": dataset_name,
        "dataset_desc": dataset_desc,
        "dataset_type": dataset_type,
        "document_name": document_name,
        "document_type": document_type,
    }

These changes focus on improving code clarity, readability, and efficiency without altering its functionality. Make sure to test the functions after making these changes to ensure they behave as expected.

Loading
Loading