views.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Create your views here.
  2. from rest_framework.views import APIView
  3. from rest_framework.viewsets import ModelViewSet
  4. from background.Object import AmazonS3Util
  5. from background.serializers import ProductInfoSerializer, VideoInSerializer, QuickStartInfoSerializer, \
  6. UpgradeFirmwareInfoSerializer
  7. from background.models import ProductInfo, VideoInfo, QuickStartInfo, UpgradeFirmwareInfo
  8. from rest_framework.response import Response
  9. class ProductInfoSet(ModelViewSet):
  10. queryset = ProductInfo.objects.all()
  11. serializer_class = ProductInfoSerializer
  12. class VideoInfoSet(ModelViewSet):
  13. queryset = VideoInfo.objects.all()
  14. serializer_class = VideoInSerializer
  15. filterset_fields = ['id', 'product_info_id', 'title']
  16. class QuickStartInfoSet(ModelViewSet):
  17. queryset = QuickStartInfo.objects.all()
  18. serializer_class = QuickStartInfoSerializer
  19. filterset_fields = ['id', 'product_info_id', 'title']
  20. class UpgradeFirmwareInfoSet(ModelViewSet):
  21. queryset = UpgradeFirmwareInfo.objects.all()
  22. serializer_class = UpgradeFirmwareInfoSerializer
  23. filterset_fields = ['id', 'product_info_id', 'title']
  24. class GetUploadUrlView(APIView):
  25. def get(self, request):
  26. file_name = request.GET.get('file_name', None)
  27. file_type = request.GET.get('file_type', None)
  28. if not all([file_name]):
  29. return Response({'code': 444, 'result': {'error_msg': 'file_name参数有误'}})
  30. try:
  31. s3_obj = AmazonS3Util()
  32. if file_type == '1': # 视频
  33. key_name = 'vsees/video_file/{file_name}'.format(file_name=file_name)
  34. elif file_type == '2': # 说明书
  35. key_name = 'vsees/quick_start_file/{file_name}'.format(file_name=file_name)
  36. elif file_type == '3': # 升级文件
  37. key_name = 'vsees/upgrade_firmware_file/{file_name}'.format(file_name=file_name)
  38. else:
  39. return Response({'code': 444, 'result': {'error_msg': 'file_type参数有误'}})
  40. bucket_name = 'ansjerfilemanager'
  41. upload_url = s3_obj.generate_put_obj_url(bucket_name, key_name)
  42. return Response({'code': 0, 'result': {'uploadUrl': upload_url}})
  43. except Exception as e:
  44. return Response(
  45. {'code': 500, 'result': {'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))}})