aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Chudnick <sam@chudnick.com>2023-06-11 09:57:43 -0400
committerSam Chudnick <sam@chudnick.com>2023-06-11 09:57:43 -0400
commit386da379df831c5f3a467706c252c56781e473f5 (patch)
tree538b61db5eb8581e1b6e931ac1ef65b9544313e7
parentb3c58c63f0cba1a67c8669f776b2e7c841e1997a (diff)
Add error handling for when no location results are found
-rw-r--r--src/app.py14
-rw-r--r--src/library.py4
-rw-r--r--src/static/style.css11
-rw-r--r--src/templates/index.html9
-rw-r--r--src/templates/location.html2
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__)
12app.config['SECRET_KEY'] = "JAnmklasd39u2mnwim" 12app.config['SECRET_KEY'] = "JAnmklasd39u2mnwim"
13 13
14 14
15def check_submission(location): 15def check_submission(location, form):
16 latitude, longitude = library.get_lat_long(location) 16 latitude, longitude = library.get_lat_long(location)
17 if (latitude, longitude) == (0, 0): 17 if (latitude, longitude) == (0, 0):
18 return flask.redirect(flask.url_for('location', location=location)) 18 return flask.redirect(flask.url_for('location', location=location))
19 elif (latitude, longitude) == (-1, -1):
20 return flask.render_template("index.html", form=form, msg="No results found")
19 else: 21 else:
20 return flask.redirect(flask.url_for('weather', location=location, latitude=latitude, longitude=longitude)) 22 return flask.redirect(flask.url_for('weather', location=location, latitude=latitude, longitude=longitude))
21 23
22 24
23@app.route('/', methods=('GET', 'POST')) 25@ app.route('/', methods=('GET', 'POST'))
24def index(): 26def index():
25 form = forms.WeatherForm() 27 form = forms.WeatherForm()
26 if form.validate_on_submit(): 28 if form.validate_on_submit():
27 location = form.location.data 29 location = form.location.data
28 return check_submission(location) 30 return check_submission(location, form)
29 else: 31 else:
30 return flask.render_template("index.html", form=form) 32 return flask.render_template("index.html", form=form)
31 33
32 34
33@app.route('/weather', methods=('GET', 'POST')) 35@ app.route('/weather', methods=('GET', 'POST'))
34def weather(): 36def weather():
35 location = flask.request.args.get('location') 37 location = flask.request.args.get('location')
36 latitude = flask.request.args.get('latitude', type=str) 38 latitude = flask.request.args.get('latitude', type=str)
@@ -49,14 +51,14 @@ def weather():
49 51
50 if form.validate_on_submit(): 52 if form.validate_on_submit():
51 location = form.location.data 53 location = form.location.data
52 return check_submission(location) 54 return check_submission(location, form=form)
53 else: 55 else:
54 return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime, 56 return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime,
55 weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon, 57 weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon,
56 location_data=location_data) 58 location_data=location_data)
57 59
58 60
59@app.route('/location', methods=('GET', 'POST')) 61@ app.route('/location', methods=('GET', 'POST'))
60def location(): 62def location():
61 location = flask.request.args.get('location', type=str) 63 location = flask.request.args.get('location', type=str)
62 url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" 64 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):
41 url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" 41 url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json"
42 headers = {"User-Agent": "pywttr 0.1"} 42 headers = {"User-Agent": "pywttr 0.1"}
43 data = requests.get(url, headers=headers).json() 43 data = requests.get(url, headers=headers).json()
44 if len(data["results"]) > 1: 44 if "results" not in data:
45 return -1, -1
46 elif len(data["results"]) > 1:
45 return 0, 0 47 return 0, 0
46 else: 48 else:
47 latitude = data["results"][0]["latitude"] 49 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 {
28 font-size: 12pt; 28 font-size: 12pt;
29} 29}
30 30
31.index-title { 31div.index-header {
32 display: flex; 32 display: flex;
33 align-self: center; 33 align-self: center;
34 justify-content: center; 34 justify-content: center;
35 width: 100%; 35 width: 100%;
36}
37
38h1.index-header {
39 margin-top: 15px;
40 margin-bottom: 5px;
41}
36 42
43h2.index-header {
44 margin-top: 5px;
45 margin-bottom: 10px;
37} 46}
38 47
39.content-noborder { 48.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 @@
1{% extends 'base.html' %} 1{% extends 'base.html' %}
2{% block content %} 2{% block content %}
3<div class=content-noborder> 3<div class=content-noborder>
4 <div class=index-title> 4 <div class=index-header>
5 <h1>Enter a Location</h1> 5 <h1 class=index-header>Enter a Location</h1>
6 </div> 6 </div>
7 {% if msg is defined %}
8 <div class=index-header>
9 <h2 class=index-header>{{ msg }}</h2>
10 </div>
11 {% endif %}
7 <div class=index-form> 12 <div class=index-form>
8 <form method="post"> 13 <form method="post">
9 {{ form.csrf_token }} 14 {{ 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 @@
1{% extends 'base.html' %} 1{% extends 'base.html' %}
2{% block content %} 2{% block content %}
3<div class=content-noborder> 3<div class=content-noborder>
4 <div class=index-title> 4 <div class=index-header>
5 <h1>Select a Location</h1> 5 <h1>Select a Location</h1>
6 </div> 6 </div>
7 <div class=index-form> 7 <div class=index-form>