Show all matching annual data for glacier's name

This commit is contained in:
coolneng 2020-01-09 22:30:00 +01:00
parent 65d3af6817
commit 1e52bcae0d
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
5 changed files with 21 additions and 19 deletions

View File

@ -21,8 +21,8 @@ class YearForm(FlaskForm):
("2017", 2017), ("2017", 2017),
("2018", 2018), ("2018", 2018),
] ]
name = StringField("Glacier Name") name = StringField("Search by Glacier Name")
year = SelectField("Year", validators=[DataRequired()], choices=year_list) year = SelectField("Search by Year", validators=[DataRequired()], choices=year_list)
submit = SubmitField("Search") submit = SubmitField("Search")

View File

@ -60,9 +60,6 @@ def table_selection():
form = YearForm() form = YearForm()
if form.validate_on_submit(): if form.validate_on_submit():
query = query_annual_data(form) 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) table = create_table(query.statement)
return render_template("table.html", table=table, title="Table") return render_template("table.html", table=table, title="Table")
return render_template("table_selection.html", title="Data", form=form) return render_template("table_selection.html", title="Data", form=form)

View File

@ -4,4 +4,5 @@
{% block app_content %} {% block app_content %}
<h1>Table</h1> <h1>Table</h1>
{{ table|safe }} {{ table|safe }}
<p><a href="{{ url_for('table_selection') }}">Back</a></p>
{% endblock %} {% endblock %}

View File

@ -1,21 +1,25 @@
from app.models import Glacier, Annual_Data, User
from app import db 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) annual_data = db.session.query(Annual_Data).filter_by(year=form.year.data)
query = annual_data
if form.name.data: if form.name.data:
query = ( glacier_name = (
db.session.query(Annual_Data) db.session.query(Annual_Data)
.filter_by(year=form.year.data)
.join(Glacier, Glacier.id == Annual_Data.id) .join(Glacier, Glacier.id == Annual_Data.id)
.filter_by(name=form.name.data) .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() user = User.query.filter_by(username=form.username.data).first()
return user return user