feat: add client latest release api
This commit is contained in:
+67
-1
@@ -6,7 +6,7 @@ from django.utils.decorators import method_decorator
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.exceptions import AuthenticationFailed
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.permissions import AllowAny, IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from rest_framework.views import APIView
|
||||
@@ -48,6 +48,7 @@ from apps.billing.services import (
|
||||
get_balance_snapshot,
|
||||
query_and_apply_recharge_payment,
|
||||
)
|
||||
from apps.portal.models import DownloadRelease
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -132,6 +133,71 @@ class ModelsView(ExternalApiView):
|
||||
)
|
||||
|
||||
|
||||
def _release_unpublished_response(platform: str) -> dict:
|
||||
return {
|
||||
"platform": platform,
|
||||
"release": None,
|
||||
"message": "暂未发布",
|
||||
}
|
||||
|
||||
|
||||
def _absolute_download_url(request, release: DownloadRelease) -> str:
|
||||
download_url = release.download_url
|
||||
if not download_url:
|
||||
return ""
|
||||
if download_url.startswith(("http://", "https://")):
|
||||
return download_url
|
||||
return request.build_absolute_uri(download_url)
|
||||
|
||||
|
||||
class ClientLatestReleaseView(APIView):
|
||||
authentication_classes = ()
|
||||
permission_classes = (AllowAny,)
|
||||
|
||||
def get(self, request):
|
||||
platform = (
|
||||
request.query_params.get("platform")
|
||||
or DownloadRelease.Platform.WINDOWS
|
||||
).strip().lower()
|
||||
if platform not in DownloadRelease.Platform.values:
|
||||
return Response(
|
||||
api_error("bad_request", "参数错误"),
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
release = (
|
||||
DownloadRelease.objects.filter(platform=platform, is_current=True)
|
||||
.order_by("-created_at", "-id")
|
||||
.first()
|
||||
)
|
||||
if release is None:
|
||||
return Response(
|
||||
_release_unpublished_response(platform),
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
download_url = _absolute_download_url(request, release)
|
||||
if not download_url:
|
||||
return Response(
|
||||
_release_unpublished_response(platform),
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{
|
||||
"platform": platform,
|
||||
"release": {
|
||||
"version": release.version,
|
||||
"download_url": download_url,
|
||||
"sha256": release.sha256,
|
||||
"release_notes": release.release_notes,
|
||||
"published_at": timezone.localtime(release.updated_at).isoformat(),
|
||||
},
|
||||
},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class PortalSessionApiView(APIView):
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
permission_classes = (IsAuthenticated,)
|
||||
|
||||
Reference in New Issue
Block a user