feat: ADRF async views (phase1) + native async serializers (phase2) + async cache infra
This commit is contained in:
+13
-13
@@ -1,4 +1,4 @@
|
||||
from rest_framework.views import APIView
|
||||
from adrf.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from rest_framework.permissions import AllowAny
|
||||
@@ -43,7 +43,7 @@ class GlobalSearchView(APIView):
|
||||
],
|
||||
responses={200: success_response}
|
||||
)
|
||||
def get(self, request):
|
||||
async def get(self, request):
|
||||
q = request.query_params.get('q', '').strip()
|
||||
content_type = request.query_params.get('type', 'all')
|
||||
page = int(request.query_params.get('page', 1))
|
||||
@@ -65,7 +65,7 @@ class GlobalSearchView(APIView):
|
||||
Q(title__icontains=q) | Q(excerpt__icontains=q) | Q(content__icontains=q)
|
||||
).filter(status='published').order_by('-created_at')[:100]
|
||||
|
||||
for article in articles:
|
||||
async for article in articles:
|
||||
cover_url = ''
|
||||
if article.cover_image and hasattr(article.cover_image, 'url'):
|
||||
cover_url = request.build_absolute_uri(article.cover_image.url)
|
||||
@@ -90,7 +90,7 @@ class GlobalSearchView(APIView):
|
||||
Q(name__icontains=q) | Q(description__icontains=q)
|
||||
).order_by('-created_at')[:100]
|
||||
|
||||
for tool in tools:
|
||||
async for tool in tools:
|
||||
results.append({
|
||||
'id': tool.id,
|
||||
'type': '工具',
|
||||
@@ -106,7 +106,7 @@ class GlobalSearchView(APIView):
|
||||
Q(title__icontains=q) | Q(description__icontains=q)
|
||||
).order_by('-created_at')[:100]
|
||||
|
||||
for course in courses:
|
||||
async for course in courses:
|
||||
cover_url = ''
|
||||
if course.cover_image and hasattr(course.cover_image, 'url'):
|
||||
cover_url = request.build_absolute_uri(course.cover_image.url)
|
||||
@@ -126,7 +126,7 @@ class GlobalSearchView(APIView):
|
||||
Q(name__icontains=q) | Q(description__icontains=q) | Q(url_path__icontains=q)
|
||||
).filter(is_enabled=True).order_by('-created_at')[:100]
|
||||
|
||||
for api in apis:
|
||||
async for api in apis:
|
||||
desc = api.description or f'{api.method} {api.url_path}'
|
||||
results.append({
|
||||
'id': api.id,
|
||||
@@ -175,7 +175,7 @@ class SearchSuggestionsView(APIView):
|
||||
],
|
||||
responses={200: success_response}
|
||||
)
|
||||
def get(self, request):
|
||||
async def get(self, request):
|
||||
q = request.query_params.get('q', '').strip()
|
||||
limit = int(request.query_params.get('limit', 8))
|
||||
|
||||
@@ -187,22 +187,22 @@ class SearchSuggestionsView(APIView):
|
||||
article_titles = Article.objects.filter(
|
||||
title__icontains=q
|
||||
).filter(status='published').values_list('title', flat=True)[:5]
|
||||
suggestions.update(article_titles)
|
||||
suggestions.update([t async for t in article_titles])
|
||||
|
||||
tool_names = Tool.objects.filter(
|
||||
name__icontains=q
|
||||
).values_list('name', flat=True)[:5]
|
||||
suggestions.update(tool_names)
|
||||
suggestions.update([t async for t in tool_names])
|
||||
|
||||
course_titles = Course.objects.filter(
|
||||
title__icontains=q
|
||||
).values_list('title', flat=True)[:5]
|
||||
suggestions.update(course_titles)
|
||||
suggestions.update([t async for t in course_titles])
|
||||
|
||||
api_names = ApiItem.objects.filter(
|
||||
name__icontains=q
|
||||
).filter(is_enabled=True).values_list('name', flat=True)[:5]
|
||||
suggestions.update(api_names)
|
||||
suggestions.update([t async for t in api_names])
|
||||
|
||||
result = list(suggestions)[:limit]
|
||||
return create_standardized_response(data=result, code=ResponseCode.SUCCESS)
|
||||
@@ -217,7 +217,7 @@ class HotKeywordsView(APIView):
|
||||
operation_description='返回热门搜索关键词列表',
|
||||
responses={200: success_response}
|
||||
)
|
||||
def get(self, request):
|
||||
async def get(self, request):
|
||||
hot_keywords = [
|
||||
'React Hooks',
|
||||
'RESTful API',
|
||||
@@ -230,4 +230,4 @@ class HotKeywordsView(APIView):
|
||||
'Vue3',
|
||||
'Python',
|
||||
]
|
||||
return create_standardized_response(data=hot_keywords, code=ResponseCode.SUCCESS)
|
||||
return create_standardized_response(data=hot_keywords, code=ResponseCode.SUCCESS)
|
||||
|
||||
Reference in New Issue
Block a user