feat: 个人页面接口

This commit is contained in:
xinwen
2021-12-13 10:50:10 +08:00
committed by Jiangjie.Bai
parent 64f721875b
commit e3b620089a
4 changed files with 74 additions and 3 deletions

View File

@@ -3,6 +3,7 @@
import os
import tarfile
from django.db.models import F, Max
from django.shortcuts import get_object_or_404, reverse
from django.utils.translation import ugettext as _
from django.utils.encoding import escape_uri_path
@@ -11,8 +12,11 @@ from django.core.files.storage import default_storage
from rest_framework import viewsets, views
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework import generics
from common.utils import data_to_json
from assets.models import Asset
from common.const.http import GET
from common.utils import get_logger, get_object_or_none
from common.mixins.api import AsyncApiMixin
@@ -26,14 +30,63 @@ from ..utils import find_session_replay_local, download_session_replay
from ..models import Session
from .. import serializers
from terminal.utils import is_session_approver
from assets.serializers import AssetSerializer
__all__ = [
'SessionViewSet', 'SessionReplayViewSet', 'SessionJoinValidateAPI'
'SessionViewSet', 'SessionReplayViewSet', 'SessionJoinValidateAPI',
'MySessionAPIView', 'MySessionAssetAPIView',
]
logger = get_logger(__name__)
class MySessionAPIView(generics.ListAPIView):
permission_classes = (IsAuthenticated, )
serializer_class = serializers.SessionSerializer
def get_queryset(self):
with tmp_to_root_org():
user = self.request.user
qs = Session.objects.filter(user_id=user.id)
return qs
class MySessionAssetAPIView(generics.ListAPIView):
queryset = Asset.objects.all()
permission_classes = (IsAuthenticated, )
serializer_class = AssetSerializer
def list(self, request, *args, **kwargs):
with tmp_to_root_org():
user = self.request.user
asset_ids = Session.objects.filter(user_id=user.id).exclude(
asset_id='' # xrdp bug 没有提交 asset_id已修复但要兼容旧数据
).values_list('asset_id').annotate(
max_date_start=Max(F('date_start'))
).order_by('-max_date_start').values_list('asset_id', flat=True)
page = self.paginate_queryset(asset_ids)
if page is not None:
serializer = self._to_serializer(page)
return self.get_paginated_response(serializer.data)
serializer = self._to_serializer(asset_ids)
return Response(serializer.data)
def _to_serializer(self, asset_ids):
assets_qs = Asset.objects.filter(id__in=list(asset_ids))
serializer_cls = self.get_serializer_class()
if hasattr(serializer_cls, 'setup_eager_loading'):
assets_qs = serializer_cls.setup_eager_loading(assets_qs)
id_asset_map = {str(asset.id): asset for asset in assets_qs}
assets = []
for i in asset_ids:
assets.append(id_asset_map[i])
serializer = self.get_serializer(assets, many=True)
return serializer
class SessionViewSet(OrgBulkModelViewSet):
model = Session
serializer_classes = {