@ -22,11 +22,12 @@ from service import utils
from service . enums import PointStatus
from service . enums import PointStatus
from service . permissions import UserPermission
from service . permissions import UserPermission
from service . service import PointService
from service . service import PointService
from service . tasks import raschet
from service . tasks import raschet , load_post_and_pvz , load_other_objects , load_data
from service . utils import load_data
from rest_framework . permissions import AllowAny
from rest_framework . permissions import AllowAny
from django . shortcuts import redirect
from django . shortcuts import redirect
from django . contrib import messages
from django . contrib import messages
from django_filters . rest_framework import DjangoFilterBackend
from rest_framework import filters
class AOViewSet ( ReadOnlyModelViewSet ) :
class AOViewSet ( ReadOnlyModelViewSet ) :
@ -35,11 +36,79 @@ class AOViewSet(ReadOnlyModelViewSet):
permission_classes = [ AllowAny ]
permission_classes = [ AllowAny ]
class PostAndPVZCategoryViewSet ( ReadOnlyModelViewSet ) :
serializer_class = serializers . PostAndPVZCategorySerializer
queryset = models . Post_and_pvzCategory . objects
class OtherObjectsCategoryViewSet ( ReadOnlyModelViewSet ) :
serializer_class = serializers . OtherObjectsCategorySerializer
queryset = models . OtherObjectsCategory . objects
class PlacementPointViewSet ( ReadOnlyModelViewSet ) :
class PlacementPointViewSet ( ReadOnlyModelViewSet ) :
serializer_class = serializers . PlacementPointSerializer
serializer_class = serializers . PlacementPointSerializer
queryset = models . PlacementPoint . objects
queryset = models . PlacementPoint . objects
pagination_class = pagination . MyPagination
pagination_class = pagination . MyPagination
permission_classes = [ UserPermission ]
permission_classes = [ UserPermission ]
filter_backends = [ DjangoFilterBackend , filters . OrderingFilter , filters . SearchFilter ]
@property
def filterset_fields ( self ) :
model_cls = self . queryset . model
fieldset = {
field . name : [
" exact " ,
" gt " ,
" gte " ,
" lt " ,
" lte " ,
" in " ,
" iexact " ,
" startswith " ,
" istartswith " ,
" endswith " ,
" iendswith " ,
" regex " ,
" iregex " ,
" isnull " ,
" contains " ,
" icontains " ,
]
for field in [
field
for field in model_cls . _meta . get_fields ( )
if field . get_internal_type ( )
not in [
" JSONField " ,
" ForeignKey " ,
" ManyToManyField " ,
" OneToOneField " ,
" PointField "
]
]
}
# filters for model relations
for field in [
field
for field in model_cls . _meta . get_fields ( )
if field . get_internal_type ( )
in [
" ForeignKey " ,
" ManyToManyField " ,
" OneToOneField " ,
]
] :
fieldset [ field . name ] = [
" exact " ,
" gt " ,
" gte " ,
" lt " ,
" lte " ,
]
return fieldset
def get_queryset ( self ) :
def get_queryset ( self ) :
qs = self . queryset . all ( ) . order_by ( ' id ' )
qs = self . queryset . all ( ) . order_by ( ' id ' )
@ -59,6 +128,7 @@ class PlacementPointViewSet(ReadOnlyModelViewSet):
delta_current = self . request . GET . get ( ' delta_current[] ' )
delta_current = self . request . GET . get ( ' delta_current[] ' )
rayons = self . request . GET . get ( ' area[] ' )
rayons = self . request . GET . get ( ' area[] ' )
aos = self . request . GET . get ( ' district[] ' )
aos = self . request . GET . get ( ' district[] ' )
group_dists = self . request . GET . getlist ( ' dist_to_group ' )
if location_ids :
if location_ids :
location_ids = list ( location_ids . split ( ' , ' ) )
location_ids = list ( location_ids . split ( ' , ' ) )
qs = qs . filter ( pk__in = location_ids )
qs = qs . filter ( pk__in = location_ids )
@ -108,8 +178,20 @@ class PlacementPointViewSet(ReadOnlyModelViewSet):
inclded = list ( included . split ( ' , ' ) )
inclded = list ( included . split ( ' , ' ) )
qs2 = models . PlacementPoint . objects . filter ( pk__in = inclded ) . all ( )
qs2 = models . PlacementPoint . objects . filter ( pk__in = inclded ) . all ( )
qs = ( qs | qs2 ) . distinct ( )
qs = ( qs | qs2 ) . distinct ( )
if group_dists :
g_d = [ list ( g . split ( ' , ' ) ) for g in group_dists ]
for group in g_d :
filtered_points = list (
models . PlacementPointPVZDistance . objects . filter ( pvz_postamates_group__id = int ( group [ 0 ] ) ,
dist__lt = int ( group [ 1 ] ) ) . values_list (
' placement_point__id ' , flat = True ) )
qs = qs . filter ( id__in = filtered_points )
return qs
return qs
@action ( methods = [ ' get ' ] , detail = False )
def get_filterset_fields ( self , request , * args , * * kwargs ) :
return Response ( self . filterset_fields , status = HTTPStatus . OK )
@action ( detail = False , methods = [ ' get ' ] )
@action ( detail = False , methods = [ ' get ' ] )
def filters ( self , request ) :
def filters ( self , request ) :
qs = self . get_queryset ( )
qs = self . get_queryset ( )
@ -231,8 +313,11 @@ class refresh_placement_points(APIView):
@staticmethod
@staticmethod
def post ( request ) :
def post ( request ) :
warnings . filterwarnings ( ' ignore ' )
warnings . filterwarnings ( ' ignore ' )
file = request . FILES [ ' file ' ]
file = request . FILES [ ' file ' ] . file
load_data ( file )
file_bytes = file . read ( )
csv_base64 = base64 . b64encode ( file_bytes ) . decode ( )
obj = models . TempFiles . objects . create ( data = csv_base64 )
load_data . delay ( obj . id )
messages . success ( request , ' Файл точек успешно загружен ' )
messages . success ( request , ' Файл точек успешно загружен ' )
return redirect ( ' /admin ' )
return redirect ( ' /admin ' )
@ -248,21 +333,30 @@ class load_ao_and_rayons(APIView):
return redirect ( ' /admin ' )
return redirect ( ' /admin ' )
import base64
@api_view ( [ ' POST ' ] )
@api_view ( [ ' POST ' ] )
def upload_post_and_pvz ( request ) :
def upload_post_and_pvz ( request ) :
warnings . filterwarnings ( ' ignore ' )
warnings . filterwarnings ( ' ignore ' )
file_rivals = request . FILES [ ' file_post_and_pvz ' ]
file_rivals = request . FILES [ ' file_post_and_pvz ' ] . file
utils . load_rivals ( file_rivals )
file_bytes = file_rivals . read ( )
messages . success ( request , ' Файл ПВЗ и Постаматов успешно загружен ' )
excel_base64 = base64 . b64encode ( file_bytes ) . decode ( )
obj = models . TempFiles . objects . create ( data = excel_base64 )
load_post_and_pvz . delay ( obj . id )
messages . success ( request , ' Загрузка ПВЗ и Постаматов началась. Отслеживайте выполнение в Статусе фоновых задач ' )
return redirect ( ' /admin ' )
return redirect ( ' /admin ' )
@api_view ( [ ' POST ' ] )
@api_view ( [ ' POST ' ] )
def upload_other_objects ( request ) :
def upload_other_objects ( request ) :
warnings . filterwarnings ( ' ignore ' )
warnings . filterwarnings ( ' ignore ' )
file_rivals = request . FILES [ ' file_other_objects ' ]
file = request . FILES [ ' file_other_objects ' ]
utils . load_other_objects ( file_rivals )
file_bytes = file . read ( )
messages . success ( request , ' Файл прочих объектов успешно загружен ' )
excel_base64 = base64 . b64encode ( file_bytes ) . decode ( )
obj = models . TempFiles . objects . create ( data = excel_base64 )
load_other_objects . delay ( obj . id )
messages . success ( request , ' Загрузка Прочих объектов началась. Отслеживайте выполнение в Статусе фоновых задач ' )
return redirect ( ' /admin ' )
return redirect ( ' /admin ' )