api_graphicFile.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from django.http import HttpResponse
  2. from rest_framework.views import APIView
  3. from api.models import Graph , User
  4. from api.utils import *
  5. class GraphicSelectAPI(APIView):
  6. def get(self , request):
  7. user = request.user
  8. print(user)
  9. graphics = []
  10. if(user.identity == 'admin'):
  11. graphiclist = Graph.objects.all()
  12. print(graphiclist)
  13. for graph in graphiclist:
  14. graphics.append({
  15. "id": graph.id,
  16. "nodes": graph.nodes,
  17. "type": graph.type,
  18. "edges": graph.edges,
  19. "createTime": graph.create_time,
  20. "updateTime": graph.update_time,
  21. "resultId": graph.result_id,
  22. "userId": graph.user_id
  23. })
  24. else:
  25. graphiclist = Graph.objects.filter(user_id = user.id)
  26. for graph in graphiclist:
  27. graphics.append({
  28. "id": graph.id,
  29. "nodes": graph.nodes,
  30. "type": graph.type,
  31. "edges": graph.edges,
  32. "createTime": graph.create_time,
  33. "updateTime": graph.update_time,
  34. "resultId": graph.result_id,
  35. "userId": graph.user_id
  36. })
  37. return success(message = "图形数据查询成功" ,
  38. data = graphics , code = 200)
  39. class GraphicDownloadAPI(APIView):
  40. def post(self, request):
  41. try:
  42. print(request.data)
  43. graph = Graph.objects.get(id = request.data.get("id"))
  44. print(graph)
  45. except Graph.DoesNotExist:
  46. return failed(message = "文件不存在", status=404)
  47. # 创建CSV响应
  48. response = HttpResponse(content_type='text/csv')
  49. response['Content-Disposition'] = f'attachment; filename="{graph.name}.csv"'
  50. # 写入CSV内容,假设file.content是CSV格式的字符串
  51. response.write(graph.nodes)
  52. return success(message = "图形数据下载成功" ,
  53. data = response , code = 200)