From 1e52bcae0dbeffd2b83fbcf47ee64faa7d27e503 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 9 Jan 2020 22:30:00 +0100 Subject: [PATCH] Show all matching annual data for glacier's name --- code/app/forms.py | 4 ++-- code/app/routes.py | 3 --- code/app/templates/login.html | 12 ++++++------ code/app/templates/table.html | 1 + code/database/queries.py | 20 ++++++++++++-------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/code/app/forms.py b/code/app/forms.py index 1c73f14..5946cc3 100644 --- a/code/app/forms.py +++ b/code/app/forms.py @@ -21,8 +21,8 @@ class YearForm(FlaskForm): ("2017", 2017), ("2018", 2018), ] - name = StringField("Glacier Name") - year = SelectField("Year", validators=[DataRequired()], choices=year_list) + name = StringField("Search by Glacier Name") + year = SelectField("Search by Year", validators=[DataRequired()], choices=year_list) submit = SubmitField("Search") diff --git a/code/app/routes.py b/code/app/routes.py index b719547..0b38a37 100644 --- a/code/app/routes.py +++ b/code/app/routes.py @@ -60,9 +60,6 @@ def table_selection(): form = YearForm() if form.validate_on_submit(): query = query_annual_data(form) - if query.scalar() is None: - flash("Sorry, no results found") - return redirect(url_for("table_selection")) table = create_table(query.statement) return render_template("table.html", table=table, title="Table") return render_template("table_selection.html", title="Data", form=form) diff --git a/code/app/templates/login.html b/code/app/templates/login.html index 92455e3..13c191a 100644 --- a/code/app/templates/login.html +++ b/code/app/templates/login.html @@ -2,11 +2,11 @@ {% import 'bootstrap/wtf.html' as wtf %} {% block app_content %} -

Sign In

-
-
- {{ wtf.quick_form(form) }} -
+

Sign In

+
+
+ {{ wtf.quick_form(form) }}
-
+
+
{% endblock %} diff --git a/code/app/templates/table.html b/code/app/templates/table.html index dbc7da9..e19c613 100644 --- a/code/app/templates/table.html +++ b/code/app/templates/table.html @@ -4,4 +4,5 @@ {% block app_content %}

Table

{{ table|safe }} +

Back

{% endblock %} diff --git a/code/database/queries.py b/code/database/queries.py index 08ba745..80b3159 100644 --- a/code/database/queries.py +++ b/code/database/queries.py @@ -1,21 +1,25 @@ -from app.models import Glacier, Annual_Data, User from app import db +from app.models import Annual_Data, Glacier, User +from flask_sqlalchemy import BaseQuery +from flask import flash, redirect, url_for -def query_annual_data(form): +def query_annual_data(form) -> BaseQuery: annual_data = db.session.query(Annual_Data).filter_by(year=form.year.data) - query = annual_data if form.name.data: - query = ( + glacier_name = ( db.session.query(Annual_Data) - .filter_by(year=form.year.data) .join(Glacier, Glacier.id == Annual_Data.id) .filter_by(name=form.name.data) - .group_by(Glacier.id) + .group_by(Annual_Data.year) ) - return query + if glacier_name.first() is None: + flash("Sorry, no results found") + return redirect(url_for("table_selection")) + return glacier_name + return annual_data -def query_user(form): +def query_user(form) -> BaseQuery: user = User.query.filter_by(username=form.username.data).first() return user