-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Import and Export function lib #2294
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
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 |
---|---|---|
|
@@ -8,11 +8,12 @@ | |
""" | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework.decorators import action | ||
from rest_framework.parsers import MultiPartParser | ||
from rest_framework.request import Request | ||
from rest_framework.views import APIView | ||
|
||
from common.auth import TokenAuth, has_permissions | ||
from common.constants.permission_constants import RoleConstants | ||
from common.constants.permission_constants import RoleConstants, Permission, Group, Operate | ||
from common.response import result | ||
from function_lib.serializers.function_lib_serializer import FunctionLibSerializer | ||
from function_lib.swagger_api.function_lib_api import FunctionLibApi | ||
|
@@ -109,3 +110,30 @@ def get(self, request: Request, current_page: int, page_size: int): | |
'user_id': request.user.id, | ||
'select_user_id': request.query_params.get('select_user_id')}).page( | ||
current_page, page_size)) | ||
|
||
class Import(APIView): | ||
authentication_classes = [TokenAuth] | ||
parser_classes = [MultiPartParser] | ||
|
||
@action(methods="POST", detail=False) | ||
@swagger_auto_schema(operation_summary=_("Import function"), operation_id=_("Import function"), | ||
manual_parameters=FunctionLibApi.Import.get_request_params_api(), | ||
tags=[_("function")] | ||
) | ||
@has_permissions(RoleConstants.ADMIN, RoleConstants.USER) | ||
def post(self, request: Request): | ||
return result.success(FunctionLibSerializer.Import( | ||
data={'user_id': request.user.id, 'file': request.FILES.get('file')}).import_()) | ||
|
||
class Export(APIView): | ||
authentication_classes = [TokenAuth] | ||
|
||
@action(methods="GET", detail=False) | ||
@swagger_auto_schema(operation_summary=_("Export function"), operation_id=_("Export function"), | ||
manual_parameters=FunctionLibApi.Export.get_request_params_api(), | ||
tags=[_("function")] | ||
) | ||
@has_permissions(RoleConstants.ADMIN, RoleConstants.USER) | ||
def get(self, request: Request, id: str): | ||
return FunctionLibSerializer.Operate( | ||
data={'id': id, 'user_id': request.user.id}).export() | ||
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. The provided Python code is mostly clear and well-documented, but there are a few areas that can be optimized or improved: Optimizations/Recommendations
Here's a refined version of the class Import(APIView):
authentication_classes = [TokenAuth]
parser_classes = [MultiPartParser]
@action(methods="POST", detail=False)
@swagger_auto_schema(operation_summary=_("Import function"), operation_id=_("Import function"),
manual_parameters=[
FunctionLibApi.Import.get_request_params_api().get("file")
],
tags=[_("function")]
)
def post(self, request: Request):
try:
# Read the uploaded file content
file_content = request.FILES['file'].read()
return result.success(
FunctionLibSerializer.Import(data={
'user_id': request.user.id,
'data': file_content # Assuming imported data comes from a field named "data"
}).import_()
except FileNotFoundError:
return result.failure(_("File not found"), status_code=404)
except Exception as e:
return result.failure(str(e), log_exception=True) # Log exceptions for debugging purposes This refactored version reduces redundancy by importing necessary elements locally and improves error handling by explicitly catching and responding to file-related errors. |
Uh oh!
There was an error while loading. Please reload this page.