new_filters+address_search

dev
AlexP077 3 years ago committed by Dmitry Titov
parent 63f4ed9990
commit 2e18c5d6e9

@ -30,7 +30,10 @@ urlpatterns = [
url(r'ao_rayons',views.AOViewSet.as_view({'get': 'list'}),name='ao_and_rayons'), url(r'ao_rayons',views.AOViewSet.as_view({'get': 'list'}),name='ao_and_rayons'),
url(r'update_status', views.update_status.as_view(), name='update_status'), url(r'update_status', views.update_status.as_view(), name='update_status'),
url(r'update_fact', views.update_fact.as_view(), name='update_fact'), 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'), url(r'to_excel', views.get_excel.as_view(), name='to_excel'),
url(r'10k', views.get_top_10k.as_view(), name='top_10_k'),
url(r'address', views.search_address.as_view(), name='search_address')
]), name='placement_points'),
path('raschet/', views.raschet.as_view(), name='ao_and_rayons'), 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'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'), url(r'upload_ao_and_rayons/', views.load_ao_and_rayons.as_view(), name='upload_ao_and_rayons'),

@ -14,6 +14,8 @@ from rest_framework.viewsets import ReadOnlyModelViewSet
from service import serializers, models, pagination from service import serializers, models, pagination
from service import utils from service import utils
from service.pagination import MyPagination
from service.serializers import PlacementPointSerializer
from service.utils import raschet as raschet_alg, load_data from service.utils import raschet as raschet_alg, load_data
@ -195,6 +197,8 @@ class PlacementPointViewSet(ReadOnlyModelViewSet):
excluded = self.request.GET.get('excluded[]') excluded = self.request.GET.get('excluded[]')
plan_first = self.request.GET.get('plan_first[]') plan_first = self.request.GET.get('plan_first[]')
plan_current = self.request.GET.get('plan_current[]') plan_current = self.request.GET.get('plan_current[]')
rayons = self.request.GET.get('rayon[]')
aos = self.request.GET.get('ao[]')
if location_ids: if location_ids:
location_ids = list(location_ids.split(',')) location_ids = list(location_ids.split(','))
qs = qs.filter(location_id__in=location_ids) qs = qs.filter(location_id__in=location_ids)
@ -225,6 +229,12 @@ class PlacementPointViewSet(ReadOnlyModelViewSet):
if plan_current: if plan_current:
plan_current = list(plan_current.split(',')) plan_current = list(plan_current.split(','))
qs = qs.filter(plan_current__range=plan_current) qs = qs.filter(plan_current__range=plan_current)
if rayons:
rayons = list(rayons.split(','))
qs = qs.filter(rayon_id__in=rayons)
if aos:
aos = list(aos.split(','))
qs = qs.filter(okrug_id__in=aos)
if excluded: if excluded:
excluded = list(excluded.split(',')) excluded = list(excluded.split(','))
qs = qs.filter(~Q(location_id__in=excluded)) qs = qs.filter(~Q(location_id__in=excluded))
@ -318,11 +328,21 @@ class update_status(APIView):
age = self.request.GET.get('age[]') age = self.request.GET.get('age[]')
included = self.request.GET.get('included[]') included = self.request.GET.get('included[]')
excluded = self.request.GET.get('excluded[]') excluded = self.request.GET.get('excluded[]')
rayons = self.request.GET.get('rayon[]')
aos = self.request.GET.get('ao[]')
if not new_status: if not new_status:
return Response({'message': 'No status provided'}, status=http_status.HTTP_400_BAD_REQUEST) 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, if not any(
[location_ids, rayons, aos, prediction_first, plan_first, plan_current, prediction_current, categories,
status,
delta, fact, age]): delta, fact, age]):
qs = models.PlacementPoint.objects.none() qs = models.PlacementPoint.objects.none()
if rayons:
rayons = list(rayons.split(','))
qs = qs.filter(rayon_id__in=rayons)
if aos:
aos = list(aos.split(','))
qs = qs.filter(okrug_id__in=aos)
if location_ids: if location_ids:
location_ids = list(location_ids.split(',')) location_ids = list(location_ids.split(','))
qs = qs.filter(location_id__in=location_ids) qs = qs.filter(location_id__in=location_ids)
@ -360,7 +380,9 @@ class update_status(APIView):
inclded = list(included.split(',')) inclded = list(included.split(','))
qs2 = models.PlacementPoint.objects.filter(location_id__in=inclded).all() qs2 = models.PlacementPoint.objects.filter(location_id__in=inclded).all()
qs = (qs | qs2).distinct() qs = (qs | qs2).distinct()
if not any([location_ids, prediction_first, plan_first, plan_current, prediction_current, categories, status, if not any(
[location_ids, rayons, aos, prediction_first, plan_first, plan_current, prediction_current, categories,
status,
delta, fact, age, excluded, included]): delta, fact, age, excluded, included]):
return Response({'message': 'Empty queryset'}, status=http_status.HTTP_200_OK) return Response({'message': 'Empty queryset'}, status=http_status.HTTP_200_OK)
@ -396,6 +418,8 @@ class get_excel(APIView):
excluded = self.request.GET.get('excluded[]') excluded = self.request.GET.get('excluded[]')
plan_first = self.request.GET.get('plan_first[]') plan_first = self.request.GET.get('plan_first[]')
plan_current = self.request.GET.get('plan_current[]') plan_current = self.request.GET.get('plan_current[]')
rayons = self.request.GET.get('rayon[]')
aos = self.request.GET.get('ao[]')
if location_ids: if location_ids:
location_ids = list(location_ids.split(',')) location_ids = list(location_ids.split(','))
qs = qs.filter(location_id__in=location_ids) qs = qs.filter(location_id__in=location_ids)
@ -423,6 +447,12 @@ class get_excel(APIView):
if plan_first: if plan_first:
plan_first = list(plan_first.split(',')) plan_first = list(plan_first.split(','))
qs = qs.filter(plan_first__range=plan_first) qs = qs.filter(plan_first__range=plan_first)
if rayons:
rayons = list(rayons.split(','))
qs = qs.filter(rayon_id__in=rayons)
if aos:
aos = list(aos.split(','))
qs = qs.filter(okrug_id__in=aos)
if plan_current: if plan_current:
plan_current = list(plan_current.split(',')) plan_current = list(plan_current.split(','))
qs = qs.filter(plan_current__range=plan_current) qs = qs.filter(plan_current__range=plan_current)
@ -435,7 +465,7 @@ class get_excel(APIView):
qs = (qs | qs2).distinct() qs = (qs | qs2).distinct()
data = pd.DataFrame(list(qs.values())) data = pd.DataFrame(list(qs.values()))
data['start_date'] = data['start_date'].dt.tz_localize(None) data['start_date'] = data['start_date'].dt.tz_localize(None)
data['sample_trn']=data['sample_trn'].astype(int) data['sample_trn'] = data['sample_trn'].astype(int)
with BytesIO() as b: with BytesIO() as b:
with pd.ExcelWriter(b) as writer: with pd.ExcelWriter(b) as writer:
data.to_excel(writer, sheet_name="Placement Points", index=False) data.to_excel(writer, sheet_name="Placement Points", index=False)
@ -447,3 +477,42 @@ class get_excel(APIView):
) )
res['Content-Disposition'] = f'attachment; filename={filename}' res['Content-Disposition'] = f'attachment; filename={filename}'
return res return res
class get_top_10k(APIView):
def get(self, request):
if models.PlacementPoint.objects.count()>10000:
qs = models.PlacementPoint.objects.order_by('-prediction_current').all()[10000]
return Response({'prediction_current':qs.prediction_current},200)
return Response({'prediction_current': models.PlacementPoint.objects.order_by('prediction_current').first().prediction_current}, 200)
class search_address(APIView):
pagination_class=MyPagination
queryset = models.PlacementPoint.objects.all()
serializer_class = PlacementPointSerializer
@property
def paginator(self):
if not hasattr(self, '_paginator'):
if self.pagination_class is None:
self._paginator = None
else:
self._paginator = self.pagination_class()
return self._paginator
def paginate_queryset(self, queryset):
if self.paginator is None:
return None
return self.paginator.paginate_queryset(queryset, self.request, view=self)
def get_paginated_response(self, data):
assert self.paginator is not None
return self.paginator.get_paginated_response(data)
def get(self, request):
address = self.request.GET.get('address')
qs = self.queryset.filter(address__icontains=address)
page = self.paginate_queryset(qs)
if page is not None:
serializer = self.serializer_class(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.serializer_class(qs, many=True)
return Response(serializer.data)
Loading…
Cancel
Save