From 520f104c616713fd10852a534e9f164e91550eda Mon Sep 17 00:00:00 2001 From: Argiris Deligiannidis Date: Sun, 7 Apr 2024 13:43:07 +0300 Subject: [PATCH] Code Formmating, set DB_REBUILD env variable --- db_connector.py | 5 +---- dot_env | 3 ++- main.py | 13 ++++++------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/db_connector.py b/db_connector.py index 874db2a..3bb3fff 100644 --- a/db_connector.py +++ b/db_connector.py @@ -12,19 +12,16 @@ DATABASE = os.getenv('DATABASE') DB_HOST = os.getenv('DB_HOST') DB_USER = os.getenv('DB_USER') DB_PASS = os.getenv('DB_PASS') +DB_REBUILD = os.getenv('DB_REBUILD') DB_URI="postgresql://{}:{}@{}/{}".format(DB_USER, DB_PASS, DB_HOST, DATABASE) #, echo=True) - engine=create_engine(DB_URI) - database=declarative_base() - db_session=scoped_session(sessionmaker(bind=engine)) if not database_exists(engine.url): create_database(engine.url) - try: connection = engine.connect() print("\n\t\tConnected successfully to database: {}@{}\n".format(DATABASE, DB_HOST)) diff --git a/dot_env b/dot_env index 55fdb08..2fdbce7 100644 --- a/dot_env +++ b/dot_env @@ -1,4 +1,5 @@ DATABASE='weatherapp' DB_HOST='10.1.1.1' DB_USER='weatherapp' -DB_PASS='YEj5zxBsM4pZlDG6NrX2' \ No newline at end of file +DB_PASS='YEj5zxBsM4pZlDG6NrX2' +DB_REBUILD='True' \ No newline at end of file diff --git a/main.py b/main.py index 1f2bcbe..88b6e46 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,10 @@ from fastapi import FastAPI, status, HTTPException from models import Location -from db_connector import database,engine +from db_connector import database,engine,DB_REBUILD import utils - -db_rebuild = True - -database.metadata.drop_all(engine) -if db_rebuild: +if DB_REBUILD == 'True': + database.metadata.drop_all(engine) utils.initialize_database() app = FastAPI() @@ -31,6 +28,7 @@ def error_400_handler(return_data): async def index_response(): return {"Status": "OK"} + @app.get("/locations") async def get_location_weather(places: str): """ @@ -63,7 +61,6 @@ async def get_weather_by_id(id: int): Returns: - The weather data for the specified location """ - result = utils.retrieve_weather_data([id]) error_400_handler(result) @@ -84,6 +81,7 @@ async def add_location( - longitude (float): The longitude coordinate of the location. - latitude (float): The latitude coordinate of the location. """ + if len(name.encode('utf-8')) > 200: raise HTTPException( status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, @@ -92,6 +90,7 @@ async def add_location( else: utils.add_location(Location(name=name,longitude=longitude,latitude=latitude)) + @app.delete("/locations/{id}") async def delete_location(id: int): """