-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: swagger #2909
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
fix: swagger #2909
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 |
---|---|---|
|
@@ -15,19 +15,11 @@ | |
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.urls import path, re_path, include | ||
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView | ||
from rest_framework import permissions | ||
from common.auth import AnonymousAuthentication | ||
from django.views import static | ||
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView | ||
|
||
from maxkb import settings | ||
|
||
SpectacularSwaggerView.permission_classes = [permissions.AllowAny] | ||
SpectacularSwaggerView.authentication_classes = [AnonymousAuthentication] | ||
SpectacularAPIView.permission_classes = [permissions.AllowAny] | ||
SpectacularAPIView.authentication_classes = [AnonymousAuthentication] | ||
SpectacularRedocView.permission_classes = [permissions.AllowAny] | ||
SpectacularRedocView.authentication_classes = [AnonymousAuthentication] | ||
urlpatterns = [ | ||
path("api/", include("users.urls")), | ||
] | ||
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 several issues in the code snippet you provided:
Here's an optimized version of the code: # Corrected lines:
from django.urls import path, re_path, include
from drf_spectacular.views import (
SpectacularAPIView,
SpectacularRedocView,
SpectacularSwaggerView,
)
from rest_framework.permissions import AllowAny
urlpatterns = [
# Include user-related URLs using the 'include' function
path("api/users/", include("users.urls")),
]
# Simplified configuration of views:
SpectacularAPIView.produces_formatters = ['application/vnd.openapi+json', 'application/yaml'] Changes Made:
These changes make the code cleaner and maintainable while ensuring that the required functionality remains intact. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,12 @@ | |
@date:2025/4/14 19:25 | ||
@desc: | ||
""" | ||
from drf_spectacular.utils import extend_schema | ||
from rest_framework.views import APIView | ||
from django.utils.translation import gettext_lazy as _ | ||
from drf_spectacular.utils import extend_schema | ||
from rest_framework.request import Request | ||
from rest_framework.views import APIView | ||
|
||
from common.auth import TokenAuth | ||
from common.auth.authenticate import TokenAuth | ||
from common.auth.authentication import has_permissions | ||
from common.constants.permission_constants import PermissionConstants | ||
from common.result import result | ||
|
@@ -36,7 +36,7 @@ class TestPermissionsUserView(APIView): | |
|
||
@extend_schema(methods=['GET'], | ||
description=_("Get current user information"), | ||
operation_id=_("Get current user information"), | ||
operation_id="测试", | ||
tags=[_("User management")], | ||
responses=UserProfileAPI.get_response()) | ||
@has_permissions(PermissionConstants.USER_EDIT) | ||
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 a few issues and suggestions to make in the provided code: Issues:
Suggestion:To ensure clarity and consistency in naming conventions, you should either update the operation IDs or specify the exact path for the import statements in Here's how you can address these issues: # Example updates based on your suggestion
## Step 1: Update or clarify the import paths if necessary
# Uncomment the line below if it points to an incorrect location
#import common.auth.authenticate as authenticate
## Step 2: Correct the operation IDs or provide more context
@extend_schema(methods=['GET'],
description=_("Get current user information"),
#operation_id=_("Get current user information"), # Remove this or change
operation_id="GetUserInfo", # Replace '测试' with a valid identifier
tags=[_("User management")],
responses=UserProfileAPI.get_response())
@has_permissions(PermissionConstants.USER_EDIT)
class TestPermissionsUserView(APIView): This ensures that the project remains clear and avoids potential conflicts between different modules. |
||
|
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.
The provided Django code has several improvements and optimizations:
Improvements/Suggestions
Remove Deprecated
core.cache
: The linefrom django.core import cache
can be removed because it is already imported in other parts of the file.Update Authentication Classes:
Enhanced Error Handling: Improved error handling by catching more specific exceptions and providing clear messages.
Comments: Minor improvements to comments for better readability.
Removed Unused Functionality: Ensure that all unused functions (like
new_instance_by_class_path
) are deleted to keep the code concise.Optimizations
Use List Comprehension: Replace nested loops with list comprehensions where applicable to improve performance.
Consistency in Exceptions: Ensure consistent error handling across different parts of the codebase.
Documentation: Add comments, type hints, and docstrings to clarify the purpose and functionality of each method.
Here's an updated version of the code incorporating these changes:
This version of the code should work well with modern Python environments and ensures best practices in terms of error handling, documentation, and overall structure.