4
4
5
5
from .validators import check_items_for_import , check_valid_uuid , check_date_iso
6
6
from .utils import create_new_item , update_existing_item , remove_item
7
- from .utils import update_parent_prices_after_deleting
7
+ from .utils import update_parent_prices_after_item_deleting
8
8
from .utils import satisfies_date_interval
9
9
from .utils import add_parents_changes_to_history
10
10
from .utils import get_node_statistics_filtered_by_date
11
- from .custom_types import Error , SHOP_UNIT_TYPES
11
+ from .custom_types import ERROR_400 , ERROR_404
12
+ from .constants import SHOP_UNIT_TYPES
12
13
from .models import ShopUnit , HistoryTable
13
14
from .serializers import shop_unit_to_dict
14
15
15
16
16
17
class ImportsAPIView (views .APIView ):
17
18
def post (self , request : Request ) -> Response :
18
- received_post_data = request .data
19
+ if 'updateDate' not in request .data :
20
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
19
21
20
- received_update_date = received_post_data [ 'updateDate' ]
21
- received_items = received_post_data [ 'items' ]
22
+ if 'items' not in request . data :
23
+ return Response ( ERROR_400 . to_dict (), status = status . HTTP_400_BAD_REQUEST )
22
24
23
- check_result : bool = check_items_for_import (received_items , received_update_date )
25
+ received_update_date = request .data ['updateDate' ]
26
+ received_items = request .data ['items' ]
24
27
25
- if not check_result :
26
- return Response (Error ( 400 , 'Validation Failed' ) .to_dict (), status = status .HTTP_400_BAD_REQUEST )
28
+ if not check_items_for_import ( received_items , received_update_date ) :
29
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
27
30
28
31
for item in received_items :
29
32
existing_units = ShopUnit .objects
@@ -40,12 +43,12 @@ def post(self, request: Request) -> Response:
40
43
class DeleteAPIView (views .APIView ):
41
44
def delete (self , request : Request , to_delete : str = None ) -> Response :
42
45
if not check_valid_uuid (to_delete ):
43
- return Response (Error ( 400 , 'Validation Failed' ) .to_dict (), status = status .HTTP_400_BAD_REQUEST )
46
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
44
47
45
48
found_item = ShopUnit .objects .filter (id = to_delete )
46
49
47
50
if len (found_item ) == 0 :
48
- return Response (Error ( 404 , 'Item not found' ) .to_dict (), status = status .HTTP_404_NOT_FOUND )
51
+ return Response (ERROR_404 .to_dict (), status = status .HTTP_404_NOT_FOUND )
49
52
50
53
deleted_goods_count : int = found_item [0 ].totally_inner_goods_count
51
54
deleted_total_sum : int = found_item [0 ].total_inner_sum
@@ -54,7 +57,7 @@ def delete(self, request: Request, to_delete: str = None) -> Response:
54
57
remove_item (found_item [0 ])
55
58
56
59
if len (parent ) != 0 :
57
- update_parent_prices_after_deleting (parent , deleted_total_sum , deleted_goods_count )
60
+ update_parent_prices_after_item_deleting (parent , deleted_total_sum , deleted_goods_count )
58
61
add_parents_changes_to_history (parent )
59
62
60
63
return Response (status = status .HTTP_200_OK )
@@ -63,12 +66,12 @@ def delete(self, request: Request, to_delete: str = None) -> Response:
63
66
class NodesAPIView (views .APIView ):
64
67
def get (self , request : Request , to_get : str ) -> Response :
65
68
if not check_valid_uuid (to_get ):
66
- return Response (Error ( 400 , 'Validation Failed' ) .to_dict (), status = status .HTTP_400_BAD_REQUEST )
69
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
67
70
68
71
found_items = ShopUnit .objects .filter (id = to_get )
69
72
70
73
if len (found_items ) == 0 :
71
- return Response (Error ( 404 , 'Item not found' ) .to_dict (), status = status .HTTP_404_NOT_FOUND )
74
+ return Response (ERROR_404 .to_dict (), status = status .HTTP_404_NOT_FOUND )
72
75
73
76
found_item = found_items [0 ]
74
77
@@ -78,14 +81,15 @@ def get(self, request: Request, to_get: str) -> Response:
78
81
class SalesAPIView (views .APIView ):
79
82
def get (self , request : Request ) -> Response :
80
83
if 'date' not in request .query_params :
81
- return Response (Error ( 400 , 'Validation Failed' ) .to_dict (), status = status .HTTP_400_BAD_REQUEST )
84
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
82
85
83
86
date_to_get : str = request .query_params ['date' ]
84
87
85
88
if not check_date_iso (date_to_get ):
86
- return Response (Error ( 400 , 'Validation Failed' ) .to_dict (), status = status .HTTP_400_BAD_REQUEST )
89
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
87
90
88
91
all_offers = ShopUnit .objects .filter (type = SHOP_UNIT_TYPES [1 ][1 ])
92
+
89
93
response : list = list ()
90
94
91
95
for offer in all_offers :
@@ -98,25 +102,23 @@ def get(self, request: Request) -> Response:
98
102
class NodeStatisticsAPIView (views .APIView ):
99
103
def get (self , request : Request , to_get : str ) -> Response :
100
104
if not check_valid_uuid (to_get ):
101
- return Response (Error (400 , 'Validation Failed' ).to_dict (), status = status .HTTP_400_BAD_REQUEST )
102
-
103
- query_parameters = request .query_params
105
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
104
106
105
- if 'dateStart' in query_parameters :
106
- if not check_date_iso (query_parameters ['dateStart' ]):
107
- return Response (Error ( 400 , 'Validation Failed' ) .to_dict (), status = status .HTTP_400_BAD_REQUEST )
107
+ if 'dateStart' in request . query_params :
108
+ if not check_date_iso (request . query_params ['dateStart' ]):
109
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
108
110
109
- if 'dateEnd' in query_parameters :
110
- if not check_date_iso (query_parameters ['dateEnd' ]):
111
- return Response (Error ( 400 , 'Validation Failed' ) .to_dict (), status = status .HTTP_400_BAD_REQUEST )
111
+ if 'dateEnd' in request . query_params :
112
+ if not check_date_iso (request . query_params ['dateEnd' ]):
113
+ return Response (ERROR_400 .to_dict (), status = status .HTTP_400_BAD_REQUEST )
112
114
113
115
found_item = HistoryTable .objects .filter (id = to_get )
114
116
115
117
if len (found_item ) == 0 :
116
- return Response (Error ( 404 , 'Item not found' ) .to_dict (), status = status .HTTP_404_NOT_FOUND )
118
+ return Response (ERROR_404 .to_dict (), status = status .HTTP_404_NOT_FOUND )
117
119
118
- filtered = get_node_statistics_filtered_by_date (found_item [0 ], query_parameters . get ('dateStart' ), query_parameters .get ('dateEnd' ))
120
+ filtered = get_node_statistics_filtered_by_date (found_item [0 ], request . query_params . get ('dateStart' ), request . query_params .get ('dateEnd' ))
119
121
120
122
response : list [dict ] = list (map (lambda item : shop_unit_to_dict (item , False ), filtered ))
121
123
122
- return Response (response , status = status .HTTP_200_OK )
124
+ return Response (response , status = status .HTTP_200_OK )
0 commit comments