diff --git a/fixtures/post_and_pvz.json b/fixtures/post_and_pvz.json index dcf2876..d5b5309 100644 --- a/fixtures/post_and_pvz.json +++ b/fixtures/post_and_pvz.json @@ -13,7 +13,7 @@ "model": "service.post_and_pvzcategory", "pk": 2, "fields": { - "name": "Постамат", + "name": "Постаматы прочих сетей", "image": "", "visible": true, "include_in_ml": true diff --git a/service/urls.py b/service/urls.py index cd63db6..deadce0 100644 --- a/service/urls.py +++ b/service/urls.py @@ -19,6 +19,7 @@ urlpatterns += [ url(r'me/', views.get_current_user, name='me'), url('download_pvz_template/', views.download_pvz_template, name='download_pvz_template'), url('download_other_template/', views.download_other_template, name='download_other_template'), + url('avg_bi_values/', views.AvgBiValuesViewSet.as_view(), name='avg_bi_values'), ] USE_X_FORWARDED_HOST = True SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') diff --git a/service/utils.py b/service/utils.py index d835549..a48008d 100644 --- a/service/utils.py +++ b/service/utils.py @@ -13,6 +13,8 @@ from django.core.cache import cache from django.conf import settings from rest_framework.response import Response from rest_framework.viewsets import ReadOnlyModelViewSet +from django.db.models import Avg + import psycopg2 from postamates.settings import DB_URL @@ -93,6 +95,49 @@ def cached_func(key, func, timeout=settings.CACHE_TIMEOUT, *args, **kwargs): return d +def get_middle_bi_values(): + fields_to_aggregate = [ + 'target_dist_shap', + 'target_post_cnt_shap', + 'target_cnt_ao_mean_shap', + 'rival_pvz_cnt_shap', + 'rival_post_cnt_shap', + 'metro_dist_shap', + 'property_price_bargains_shap', + 'property_price_offers_shap', + 'property_mean_floor_shap', + 'property_era_shap', + 'flats_cnt_shap', + 'popul_home_shap', + 'popul_job_shap', + 'yndxfood_sum_shap', + 'yndxfood_cnt_shap', + 'school_cnt_shap', + 'kindergar_cnt_shap', + 'public_stop_cnt_shap', + 'sport_center_cnt_shap', + 'pharmacy_cnt_shap', + 'supermarket_cnt_shap', + 'supermarket_premium_cnt_shap', + 'clinic_cnt_shap', + 'bank_cnt_shap', + 'reca_cnt_shap', + 'lab_cnt_shap', + 'culture_cnt_shap', + 'attraction_cnt_shap', + 'mfc_cnt_shap', + 'bc_cnt_shap', + 'tc_cnt_shap', + 'business_activity_shap' + ] + + aggregations = {} + for field_name in fields_to_aggregate: + aggregations[f'avg_{field_name}'] = Avg(field_name) + result = models.PlacementPoint.objects.aggregate(**aggregations) + return result + + class CustomReadOnlyModelViewSet(ReadOnlyModelViewSet): def list(self, request, *args, **kwargs): def f(): diff --git a/service/views.py b/service/views.py index b6cffa8..60b97ef 100644 --- a/service/views.py +++ b/service/views.py @@ -32,6 +32,7 @@ from service.utils import CustomReadOnlyModelViewSet from django.db.models import Min, Max import os from django.forms.models import model_to_dict +import base64 class AOViewSet(CustomReadOnlyModelViewSet): @@ -450,7 +451,12 @@ class load_ao_and_rayons(APIView): return redirect('/admin') -import base64 +class AvgBiValuesViewSet(APIView): + @staticmethod + def get(request): + data = utils.get_middle_bi_values() + return Response(data, status=HTTPStatus.OK) + @api_view(['POST'])