from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import permissions from django.core.cache import cache import json from service.utils import raschet as raschet_alg import pandas as pd from io import BytesIO from django.http import HttpResponse class ao_and_rayons(APIView): permission_classes = [permissions.AllowAny] def get(self, request, format=None): d = cache.get('ao_and_rayons') if d is None: data = json.loads(open('ao_and_rayons.json', 'r').read()) cache.set('ao_and_rayons', data, 60 * 60 * 24) return Response(d) class raschet(APIView): permission_classes = [permissions.AllowAny] def post(self, request, format=None): df_points, df_nets = raschet_alg(**request.data) with BytesIO() as b: # Use the StringIO object as the filehandle. writer = pd.ExcelWriter(b, engine='xlsxwriter') if df_points is not None: df_points.to_excel(writer, sheet_name='Точки', index=False) if df_nets is not None: df_nets.to_excel(writer, sheet_name='Полигоны', index=False) writer.save() # Set up the Http response. filename = f'Выгрузка.xlsx' response = HttpResponse( b.getvalue(), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) response['Content-Disposition'] = 'attachment; filename=%s' % filename return response