use .env in app.py
This commit is contained in:
32
app.py
32
app.py
@@ -8,6 +8,9 @@ from ingress import IngressAPI
|
|||||||
from models import EventType, Plext
|
from models import EventType, Plext
|
||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
from pymongo.errors import PyMongoError
|
from pymongo.errors import PyMongoError
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
# Timezone configuration
|
# Timezone configuration
|
||||||
TIMEZONE = ZoneInfo("Europe/Rome")
|
TIMEZONE = ZoneInfo("Europe/Rome")
|
||||||
@@ -49,6 +52,7 @@ def basic_auth_required(f):
|
|||||||
Returns:
|
Returns:
|
||||||
401 Unauthorized if authentication fails or is missing
|
401 Unauthorized if authentication fails or is missing
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
auth = request.authorization
|
auth = request.authorization
|
||||||
@@ -58,7 +62,7 @@ def basic_auth_required(f):
|
|||||||
"Could not verify your access level for that URL.\n"
|
"Could not verify your access level for that URL.\n"
|
||||||
"You have to login with proper credentials",
|
"You have to login with proper credentials",
|
||||||
401,
|
401,
|
||||||
{"WWW-Authenticate": 'Basic realm="Login Required"'}
|
{"WWW-Authenticate": 'Basic realm="Login Required"'},
|
||||||
)
|
)
|
||||||
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
@@ -150,6 +154,7 @@ def plext_to_dict(plext: Plext) -> dict:
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.route("/plexts/from-db", methods=["GET", "OPTIONS"])
|
@app.route("/plexts/from-db", methods=["GET", "OPTIONS"])
|
||||||
@basic_auth_required
|
@basic_auth_required
|
||||||
def get_plexts_from_db():
|
def get_plexts_from_db():
|
||||||
@@ -186,13 +191,21 @@ def get_plexts_from_db():
|
|||||||
try:
|
try:
|
||||||
timestamp_from = int(timestamp_from)
|
timestamp_from = int(timestamp_from)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return jsonify({"error": "timestamp_from must be an integer"}), 400, cors_headers
|
return (
|
||||||
|
jsonify({"error": "timestamp_from must be an integer"}),
|
||||||
|
400,
|
||||||
|
cors_headers,
|
||||||
|
)
|
||||||
|
|
||||||
if timestamp_to is not None:
|
if timestamp_to is not None:
|
||||||
try:
|
try:
|
||||||
timestamp_to = int(timestamp_to)
|
timestamp_to = int(timestamp_to)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return jsonify({"error": "timestamp_to must be an integer"}), 400, cors_headers
|
return (
|
||||||
|
jsonify({"error": "timestamp_to must be an integer"}),
|
||||||
|
400,
|
||||||
|
cors_headers,
|
||||||
|
)
|
||||||
|
|
||||||
# Build MongoDB filter query
|
# Build MongoDB filter query
|
||||||
filter_query = {}
|
filter_query = {}
|
||||||
@@ -222,18 +235,14 @@ def get_plexts_from_db():
|
|||||||
projection = {"_id": 0}
|
projection = {"_id": 0}
|
||||||
|
|
||||||
# Execute query with sorting (timestamp DESC - most recent first)
|
# Execute query with sorting (timestamp DESC - most recent first)
|
||||||
cursor = collection.find(
|
cursor = collection.find(filter=filter_query, projection=projection).sort(
|
||||||
filter=filter_query,
|
"timestamp", -1
|
||||||
projection=projection
|
)
|
||||||
).sort("timestamp", -1)
|
|
||||||
|
|
||||||
# Convert cursor to list
|
# Convert cursor to list
|
||||||
plexts = list(cursor)
|
plexts = list(cursor)
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({"count": len(plexts), "plexts": plexts}), 200, cors_headers
|
||||||
"count": len(plexts),
|
|
||||||
"plexts": plexts
|
|
||||||
}), 200, cors_headers
|
|
||||||
|
|
||||||
except PyMongoError as e:
|
except PyMongoError as e:
|
||||||
logger.error(f"MongoDB error: {e}")
|
logger.error(f"MongoDB error: {e}")
|
||||||
@@ -245,6 +254,7 @@ def get_plexts_from_db():
|
|||||||
logger.exception("Unexpected error in get_plexts_from_db")
|
logger.exception("Unexpected error in get_plexts_from_db")
|
||||||
return jsonify({"error": "An error occurred"}), 500, cors_headers
|
return jsonify({"error": "An error occurred"}), 500, cors_headers
|
||||||
|
|
||||||
|
|
||||||
@app.route("/plexts/from-api", methods=["GET"])
|
@app.route("/plexts/from-api", methods=["GET"])
|
||||||
@basic_auth_required
|
@basic_auth_required
|
||||||
def get_plexts_from_api():
|
def get_plexts_from_api():
|
||||||
|
|||||||
Reference in New Issue
Block a user