ao/rayons+load_excel

dev
AlexP077 3 years ago committed by Dmitry Titov
parent a13e84fca1
commit 90f9f437ae

@ -5,4 +5,14 @@ from service import models
class PlacementPointSerializer(serializers.ModelSerializer):
class Meta:
model = models.PlacementPoint
fields = '__all__'
class AOSerializer(serializers.ModelSerializer):
class Meta:
model = models.AO
fields = '__all__'
class RayonSerializer(serializers.ModelSerializer):
class Meta:
model = models.AO
fields = '__all__'

@ -8,6 +8,8 @@ from service import views
router = routers.DefaultRouter()
router.register('', views.PlacementPointViewSet)
router.register('/ao', views.AOViewSet)
router.register('/rayon', views.RayonViewSet)
schema_view = get_schema_view(
openapi.Info(
@ -27,8 +29,8 @@ urlpatterns = [
path('placement_points',
include([*router.urls,
url(r'/update_status', views.update_status.as_view(), name='update_status'),
url(r'/update_fact', views.update_fact.as_view(), name='update_fact')]), name='placement_points'),
path('ao_and_rayons/', views.ao_and_rayons.as_view(), name='ao_and_rayons'),
url(r'/update_fact', views.update_fact.as_view(), name='update_fact'),
url(r'/to_excel', views.get_excel.as_view(), name='to_excel')]), name='placement_points'),
path('raschet/', views.raschet.as_view(), name='ao_and_rayons'),
url(r'load_csv/', views.refresh_placement_points.as_view(), name='upload_placement_points'),
url(r'upload_ao_and_rayons/', views.load_ao_and_rayons.as_view(), name='upload_ao_and_rayons'),

@ -1,9 +1,7 @@
import json
import warnings
from io import BytesIO
import pandas as pd
from django.core.cache import cache
from django.db.models import Q
from django.http import HttpResponse
from rest_framework import permissions, status
@ -25,18 +23,6 @@ def rename_result_dataset(dataframe, rename_dict, reverse=False):
return dataframe.rename(columns=rename_dict)
class ao_and_rayons(GenericAPIView):
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)
d = data
return Response(d)
class raschet(GenericAPIView):
permission_classes = [permissions.AllowAny]
@ -180,6 +166,16 @@ class raschet(GenericAPIView):
return response
class RayonViewSet(ReadOnlyModelViewSet):
serializer_class = serializers.RayonSerializer
queryset = models.Rayon.objects
class AOViewSet(ReadOnlyModelViewSet):
serializer_class = serializers.AOSerializer
queryset = models.AO.objects
class PlacementPointViewSet(ReadOnlyModelViewSet):
serializer_class = serializers.PlacementPointSerializer
queryset = models.PlacementPoint.objects
@ -324,7 +320,8 @@ class update_status(APIView):
excluded = self.request.GET.get('excluded[]')
if not new_status:
return Response({'message': 'No status provided'}, status=http_status.HTTP_400_BAD_REQUEST)
if not any([location_ids, prediction_first,plan_first,plan_current,prediction_current, categories, status, delta, fact, age]):
if not any([location_ids, prediction_first, plan_first, plan_current, prediction_current, categories, status,
delta, fact, age]):
qs = models.PlacementPoint.objects.none()
if location_ids:
location_ids = list(location_ids.split(','))
@ -363,7 +360,8 @@ class update_status(APIView):
inclded = list(included.split(','))
qs2 = models.PlacementPoint.objects.filter(location_id__in=inclded).all()
qs = (qs | qs2).distinct()
if not any([location_ids, prediction_first,plan_first,plan_current,prediction_current, categories, status, delta, fact, age, excluded, included]):
if not any([location_ids, prediction_first, plan_first, plan_current, prediction_current, categories, status,
delta, fact, age, excluded, included]):
return Response({'message': 'Empty queryset'}, status=http_status.HTTP_200_OK)
qs.update(**{'status': new_status})
@ -381,3 +379,71 @@ class update_fact(APIView):
return Response(status=http_status.HTTP_404_NOT_FOUND)
qs.update(**{'fact': fact})
return Response({'message': 'fact updated'}, status=http_status.HTTP_200_OK)
class get_excel(APIView):
def get(self, request):
qs = models.PlacementPoint.objects.all()
location_ids = self.request.GET.get('location_ids[]')
prediction_first = self.request.GET.get('prediction_first[]')
prediction_current = self.request.GET.get('prediction_current[]')
categories = self.request.GET.get('categories[]')
status = self.request.GET.get('status[]')
delta = self.request.GET.get('delta[]')
fact = self.request.GET.get('fact[]')
age = self.request.GET.get('age[]')
included = self.request.GET.get('included[]')
excluded = self.request.GET.get('excluded[]')
plan_first = self.request.GET.get('plan_first[]')
plan_current = self.request.GET.get('plan_current[]')
if location_ids:
location_ids = list(location_ids.split(','))
qs = qs.filter(location_id__in=location_ids)
if prediction_first:
prediction_first = list(prediction_first.split(','))
qs = qs.filter(prediction_first__range=prediction_first)
if prediction_current:
prediction_current = list(prediction_current.split(','))
qs = qs.filter(prediction_current__range=prediction_current)
if categories:
categories = list(categories.split(','))
qs = qs.filter(category__in=categories)
if status:
status = list(status.split(','))
qs = qs.filter(status__in=status)
if delta:
delta = list(delta.split(','))
qs = qs.filter(delta__range=delta)
if fact:
fact = list(fact.split(','))
qs = qs.filter(fact__range=fact)
if age:
age = list(age.split(','))
qs = qs.filter(age__range=age)
if plan_first:
plan_first = list(plan_first.split(','))
qs = qs.filter(plan_first__range=plan_first)
if plan_current:
plan_current = list(plan_current.split(','))
qs = qs.filter(plan_current__range=plan_current)
if excluded:
excluded = list(excluded.split(','))
qs = qs.filter(~Q(location_id__in=excluded))
if included:
inclded = list(included.split(','))
qs2 = models.PlacementPoint.objects.filter(location_id__in=inclded).all()
qs = (qs | qs2).distinct()
data = pd.DataFrame(list(qs.values()))
data['start_date'] = data['start_date'].dt.tz_localize(None)
data['sample_trn']=data['sample_trn'].astype(int)
with BytesIO() as b:
with pd.ExcelWriter(b) as writer:
data.to_excel(writer, sheet_name="Placement Points", index=False)
filename = "placement_points.xlsx"
res = HttpResponse(
b.getvalue(),
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
res['Content-Disposition'] = f'attachment; filename={filename}'
return res

Loading…
Cancel
Save