From 155f7e8fddae58eb905fa31aaab455b616c73a99 Mon Sep 17 00:00:00 2001 From: ArnavPrakash Date: Thu, 8 Feb 2024 14:40:27 +0530 Subject: [PATCH] fix: mypy issues --- server/campus_hub/controllers/services.py | 40 +++++++++++++++++++---- server/campus_hub/models/store.py | 2 ++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/server/campus_hub/controllers/services.py b/server/campus_hub/controllers/services.py index 6116f774..c3871943 100644 --- a/server/campus_hub/controllers/services.py +++ b/server/campus_hub/controllers/services.py @@ -202,7 +202,13 @@ def delete_service(service_id: str) -> APIResponse: ) -def get_stores_by_service_id(service_id: str, max_rating: float, min_rating: float, distance:float, category:str, ) -> APIResponse: +def get_stores_by_service_id( + service_id: str, + max_rating: float, + min_rating: float, + distance: float, + category: str, +) -> APIResponse: """ Get stores by service id from the MongoDB database. @@ -254,18 +260,40 @@ def get_stores_by_service_id(service_id: str, max_rating: float, min_rating: flo # If distance is specified, filter stores by distance if distance: try: - origin = (float(request.args.get("latitude")), float(request.args.get("longitude"))) + latitude_str = request.args.get("latitude") + longitude_str = request.args.get("longitude") + latitude: float = ( + float(latitude_str) if latitude_str is not None else 0.0 + ) + longitude: float = ( + float(longitude_str) if longitude_str is not None else 0.0 + ) + + origin = (latitude, longitude) + for store in stores: destination = store.coordinates distance_matrix = get_distance_matrix(origin, destination) - store["distance"] = distance_matrix["rows"][0]["elements"][0]["distance"]["text"] + distance_str: str = ( + distance_matrix.get("rows", [{}])[0] + .get("elements", [{}])[0] + .get("distance", {}) + .get("text", "") + ) + store.distance = distance_str except Exception as e: return response( Status.INTERNAL_SERVER_ERROR, **message(f"Error calculating distance: {str(e)}"), ) - stores = list(filter(lambda store: float(store["distance"].split(" ")[0]) <= distance, stores)) - + + stores = [ + store + for store in stores + if store.distance + if float(store.distance.split(" ")[0]) <= distance + ] + # If stores are found, return a JSON response return response(Status.SUCCESS, **store_list.model_dump()) @@ -274,7 +302,7 @@ def get_stores_by_service_id(service_id: str, max_rating: float, min_rating: flo Status.INTERNAL_SERVER_ERROR, **message(f"Error retrieving stores from MongoDB: {e}"), ) - + def get_products_by_service_id(service_id): # Placeholder logic to get details of a specific service by ID diff --git a/server/campus_hub/models/store.py b/server/campus_hub/models/store.py index 05efb811..e0afb2fb 100644 --- a/server/campus_hub/models/store.py +++ b/server/campus_hub/models/store.py @@ -40,6 +40,8 @@ class Store(BaseModel): timings: Optional[Tuple[float, float]] overall_rating: Optional[float] offer_available: Optional[bool] + distance: Optional[str] + class StoreList(BaseModel): """