Show all matching annual data for glacier's name
This commit is contained in:
parent
65d3af6817
commit
1e52bcae0d
|
@ -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")
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -4,4 +4,5 @@
|
|||
{% block app_content %}
|
||||
<h1>Table</h1>
|
||||
{{ table|safe }}
|
||||
<p><a href="{{ url_for('table_selection') }}">Back</a></p>
|
||||
{% endblock %}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue