Coverage for api/utils/check_carriers_resources.py: 14%
85 statements
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-10 03:02 +0300
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-10 03:02 +0300
1from typing import Any
3from exceptions import BadParameterHTTPError
4from logging_config import logger
5from sotrans_models.models.misc.document import RequestDocumentType
6from sotrans_models.models.misc.verification import VerificationStatus
7from sotrans_models.models.orders.order import OrderDBModel
8from sotrans_models.models.organizations import OrganizationDBModel
9from sotrans_models.models.resources.drivers import DriverDBModel
10from sotrans_models.models.resources.trailers import TrailerDBModel
11from sotrans_models.models.resources.trucks import (
12 BodySettingsModel,
13 LoadingType,
14 TruckDBModel,
15 TruckType,
16)
17from starlette import status
18from starlette.exceptions import HTTPException
21def is_resource_verified(data: Any) -> bool:
22 return (
23 isinstance(data, dict)
24 and "verification" in data
25 and data["verification"]
26 and data["verification"]["status"] == "accepted"
27 )
30def is_resource_model_verified(
31 resource: DriverDBModel
32 | TruckDBModel
33 | TrailerDBModel
34 | OrganizationDBModel,
35) -> bool:
36 if (
37 resource.verification
38 and resource.verification.status == VerificationStatus.accepted
39 ):
40 return True
41 return False
44def verified_mark(
45 resource: TruckDBModel
46 | TrailerDBModel
47 | DriverDBModel
48 | OrganizationDBModel,
49) -> str:
50 if is_resource_model_verified(resource):
51 return " ✅"
52 return ""
55def check_verification(data: dict[str, Any], entity_name: str):
56 if is_resource_verified(data):
57 return
58 raise HTTPException(
59 status.HTTP_406_NOT_ACCEPTABLE, f"{entity_name} не верифицирован."
60 )
63def is_body_appropriate(
64 carriers_settings: BodySettingsModel | None,
65 required: BodySettingsModel | None,
66) -> bool:
67 if not required:
68 return True
69 if not carriers_settings:
70 return False
71 if required.weight and (
72 not carriers_settings.weight
73 or carriers_settings.weight < required.weight
74 ):
75 return False
76 logger.warning("weight pass")
77 if required.volume and (
78 not carriers_settings.volume
79 or carriers_settings.volume < required.volume
80 ):
81 return False
82 logger.warning("volume pass")
83 if required.body_type and not set(required.body_type) & set(
84 carriers_settings.body_type
85 ):
86 return False
87 logger.warning("body type pass")
88 if required.loading_type:
89 if not carriers_settings.loading_type:
90 return False
91 if (
92 LoadingType.any in carriers_settings.loading_type
93 or LoadingType.any in required.loading_type
94 ):
95 return True
96 logger.warning("loading any failed")
97 if not carriers_settings.loading_type:
98 return False
99 if not set(required.loading_type) & set(
100 carriers_settings.loading_type
101 ):
102 return False
103 return True
106def check_order_approved(order: OrderDBModel) -> bool:
107 if not order.documents:
108 return False
109 for doc in order.documents:
110 if doc.type == RequestDocumentType.order_request:
111 return True
112 return False
115def check_resources_are_same(order_data, update_data) -> bool:
116 """
117 Used to prevent update execution.
119 Doesn't encounter cases of similarity
120 which are thrown away by resource
121 validation in different places
122 (generate_draft_document and top of caller)
123 """
124 order = OrderDBModel(**order_data)
125 update = OrderDBModel(**update_data)
126 same_truck = same_driver = False
127 if order.truck and update.truck:
128 if order.truck.id == update.truck.id:
129 same_truck = True
130 if order.driver and update.driver:
131 if order.driver.id == update.driver.id:
132 same_driver = True
133 truck = update.truck or order.truck
134 if truck is None:
135 raise BadParameterHTTPError("нет тягача")
136 if truck.truck_type == TruckType.van_truck:
137 if same_truck and same_driver:
138 return True
139 if order.trailer and update.trailer:
140 if order.trailer.id == update.trailer.id:
141 return same_truck and same_driver
142 return False
145def check_resources_for_generation(order: OrderDBModel):
146 if not (order.driver and order.truck):
147 raise BadParameterHTTPError("нет ресурсов")
148 if order.truck.truck_type == TruckType.van_truck:
149 return
150 if not order.trailer:
151 raise BadParameterHTTPError("нет прицепа")