From 93e6485ddfd25a5d1494390e6983a73942cfd5c3 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Sun, 11 Jan 2026 18:29:00 -0700 Subject: [PATCH] moving API route to separate file --- weather/__init__.py | 3 +++ weather/api.py | 22 ++++++++++++++++++++++ weather/weather.py | 14 +------------- 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 weather/api.py diff --git a/weather/__init__.py b/weather/__init__.py index d7f5a2b..cf91bbc 100644 --- a/weather/__init__.py +++ b/weather/__init__.py @@ -25,6 +25,9 @@ def create_app(test_config=None): from . import weather app.register_blueprint(weather.bp) app.add_url_rule('/', endpoint='index') + + from . import api + app.register_blueprint(api.bp) app.add_url_rule('/api', endpoint='api') return app diff --git a/weather/api.py b/weather/api.py new file mode 100644 index 0000000..621000c --- /dev/null +++ b/weather/api.py @@ -0,0 +1,22 @@ +from flask import ( + Blueprint +) +from werkzeug.exceptions import abort + +from weather.db import get_db + +bp = Blueprint('api', __name__) + +@bp.route('/api') +def api(): + db = get_db() + latest_period = dict(db.execute( + 'SELECT `id` FROM `reports` WHERE `type` = "hourly" ORDER BY `reported_at` DESC' + ).fetchone()) + + current_conditions = dict(db.execute( + f"SELECT * FROM `periods` WHERE `report_id` = {latest_period['id']} LIMIT 1" + ).fetchone()) + + return current_conditions + diff --git a/weather/weather.py b/weather/weather.py index ad863f4..c6d2081 100644 --- a/weather/weather.py +++ b/weather/weather.py @@ -1,5 +1,5 @@ from flask import ( - Blueprint, flash, g, render_template, request, url_for + Blueprint, render_template ) from werkzeug.exceptions import abort @@ -35,15 +35,3 @@ def index(): periods=periods ) -@bp.route('/api') -def api(): - db = get_db() - latest_period = dict(db.execute( - 'SELECT `id` FROM `reports` WHERE `type` = "hourly" ORDER BY `reported_at` DESC' - ).fetchone()) - - current_conditions = dict(db.execute( - f"SELECT * FROM `periods` WHERE `report_id` = {latest_period['id']} LIMIT 1" - ).fetchone()) - - return current_conditions