api_graphicFile.py 2.1 KB

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