From 386da379df831c5f3a467706c252c56781e473f5 Mon Sep 17 00:00:00 2001 From: Sam Chudnick Date: Sun, 11 Jun 2023 09:57:43 -0400 Subject: Add error handling for when no location results are found --- src/app.py | 14 ++++++++------ src/library.py | 4 +++- src/static/style.css | 11 ++++++++++- src/templates/index.html | 9 +++++++-- src/templates/location.html | 2 +- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/app.py b/src/app.py index 449f37a..0560ea9 100644 --- a/src/app.py +++ b/src/app.py @@ -12,25 +12,27 @@ app = flask.Flask(__name__) app.config['SECRET_KEY'] = "JAnmklasd39u2mnwim" -def check_submission(location): +def check_submission(location, form): latitude, longitude = library.get_lat_long(location) if (latitude, longitude) == (0, 0): return flask.redirect(flask.url_for('location', location=location)) + elif (latitude, longitude) == (-1, -1): + return flask.render_template("index.html", form=form, msg="No results found") else: return flask.redirect(flask.url_for('weather', location=location, latitude=latitude, longitude=longitude)) -@app.route('/', methods=('GET', 'POST')) +@ app.route('/', methods=('GET', 'POST')) def index(): form = forms.WeatherForm() if form.validate_on_submit(): location = form.location.data - return check_submission(location) + return check_submission(location, form) else: return flask.render_template("index.html", form=form) -@app.route('/weather', methods=('GET', 'POST')) +@ app.route('/weather', methods=('GET', 'POST')) def weather(): location = flask.request.args.get('location') latitude = flask.request.args.get('latitude', type=str) @@ -49,14 +51,14 @@ def weather(): if form.validate_on_submit(): location = form.location.data - return check_submission(location) + return check_submission(location, form=form) else: return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime, weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon, location_data=location_data) -@app.route('/location', methods=('GET', 'POST')) +@ app.route('/location', methods=('GET', 'POST')) def location(): location = flask.request.args.get('location', type=str) url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" diff --git a/src/library.py b/src/library.py index 37ba3eb..07112da 100644 --- a/src/library.py +++ b/src/library.py @@ -41,7 +41,9 @@ def get_lat_long(location): url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" headers = {"User-Agent": "pywttr 0.1"} data = requests.get(url, headers=headers).json() - if len(data["results"]) > 1: + if "results" not in data: + return -1, -1 + elif len(data["results"]) > 1: return 0, 0 else: latitude = data["results"][0]["latitude"] diff --git a/src/static/style.css b/src/static/style.css index 768b816..3e39dea 100644 --- a/src/static/style.css +++ b/src/static/style.css @@ -28,12 +28,21 @@ html, body, .container { font-size: 12pt; } -.index-title { +div.index-header { display: flex; align-self: center; justify-content: center; width: 100%; +} + +h1.index-header { + margin-top: 15px; + margin-bottom: 5px; +} +h2.index-header { + margin-top: 5px; + margin-bottom: 10px; } .content-noborder { diff --git a/src/templates/index.html b/src/templates/index.html index da2a19a..dae4c7e 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -1,9 +1,14 @@ {% extends 'base.html' %} {% block content %}
-
-

Enter a Location

+
+

Enter a Location

+ {% if msg is defined %} +
+

{{ msg }}

+
+ {% endif %}
{{ form.csrf_token }} diff --git a/src/templates/location.html b/src/templates/location.html index fa691ea..432f14a 100644 --- a/src/templates/location.html +++ b/src/templates/location.html @@ -1,7 +1,7 @@ {% extends 'base.html' %} {% block content %}
-
+

Select a Location

-- cgit v1.2.3