Code Formmating, set DB_REBUILD env variable
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Argiris Deligiannidis 2024-04-07 13:43:07 +03:00
parent 933a650bdb
commit 520f104c61
3 changed files with 9 additions and 12 deletions

View File

@ -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))

View File

@ -2,3 +2,4 @@ DATABASE='weatherapp'
DB_HOST='10.1.1.1'
DB_USER='weatherapp'
DB_PASS='YEj5zxBsM4pZlDG6NrX2'
DB_REBUILD='True'

13
main.py
View File

@ -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):
"""
@ -64,7 +62,6 @@ async def get_weather_by_id(id: int):
- The weather data for the specified location
"""
result = utils.retrieve_weather_data([id])
error_400_handler(result)
return 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):
"""