change_points_in_rad

dev
AlexP077 3 years ago committed by Dmitry Titov
parent 56f9b1ac2b
commit d4bb63bc50

@ -169,3 +169,4 @@ CELERY_BROKER_URL = 'amqp://loyalty-rabbit'
CELERY_NAMESPACE = 'CELERY' CELERY_NAMESPACE = 'CELERY'
PROJECT_NAME = 'postamates' PROJECT_NAME = 'postamates'
CACHE_TIMEOUT = 0 CACHE_TIMEOUT = 0
POINT_RADIUS = 500

@ -1,12 +1,14 @@
from io import BytesIO from io import BytesIO
import pandas as pd import pandas as pd
from django.contrib.gis.measure import Distance
from django.db.models import F
from postamates.settings import POINT_RADIUS
from service import models from service import models
class PointService: class PointService:
def update_fact(self, postamat_id: str, fact: int): def update_fact(self, postamat_id: str, fact: int):
qs = self.get_point_by_postamat_id(postamat_id) qs = self.get_point_by_postamat_id(postamat_id)
qs.update(**{'fact': fact}) qs.update(**{'fact': fact})
@ -15,6 +17,22 @@ class PointService:
qs = self.get_point_by_id(point_id) qs = self.get_point_by_id(point_id)
qs.update(**{'postamat_id': postamat_id}) qs.update(**{'postamat_id': postamat_id})
@staticmethod
def update_points_in_radius(qs: models.PlacementPoint, new_status: str):
for point in qs:
if new_status == 'Installation':
if point.status == 'Pending':
pnts = models.PlacementPoint.objects.filter(
geometry__distance_lt=(point.geometry, Distance(m=POINT_RADIUS)),
)
pnts.update(target_post_cnt=F('target_post_cnt') + 1)
elif new_status == 'Cancelled' or new_status == 'Pending':
if point.status == 'Installation':
pnts = models.PlacementPoint.objects.filter(
geometry__distance_lt=(point.geometry, Distance(m=POINT_RADIUS)),
)
pnts.update(target_post_cnt=F('target_post_cnt') - 1 if F('target_post_cnt') != 0 else 0)
@staticmethod @staticmethod
def update_status(qs: models.PlacementPoint, new_status: str) -> models.PlacementPoint: def update_status(qs: models.PlacementPoint, new_status: str) -> models.PlacementPoint:
qs.update(**{'status': new_status}) qs.update(**{'status': new_status})

@ -138,8 +138,10 @@ class PlacementPointViewSet(ReadOnlyModelViewSet):
def update_status(self, request): def update_status(self, request):
qs = self.get_queryset() qs = self.get_queryset()
new_status = self.request.GET.get('status') new_status = self.request.GET.get('status')
if not new_status: choices = [choice[0] for choice in models.PlacementPoint.STATUS_CHOICES]
if not new_status or new_status not in choices:
return Response({'message': 'No status'}, HTTPStatus.BAD_REQUEST) return Response({'message': 'No status'}, HTTPStatus.BAD_REQUEST)
PointService.update_points_in_radius(qs, new_status)
PointService.update_status(qs, new_status) PointService.update_status(qs, new_status)
return Response( return Response(
{'message': 'status updated'}, {'message': 'status updated'},

Loading…
Cancel
Save