igdb/code/database/queries.py

26 lines
830 B
Python
Raw Normal View History

2020-01-09 21:10:27 +01:00
from app import db
from app.models import Annual_Data, Glacier, User
from flask_sqlalchemy import BaseQuery
from flask import flash, redirect, url_for
2020-01-09 21:10:27 +01:00
def query_annual_data(form) -> BaseQuery:
2020-01-09 21:10:27 +01:00
annual_data = db.session.query(Annual_Data).filter_by(year=form.year.data)
if form.name.data:
glacier_name = (
2020-01-09 21:10:27 +01:00
db.session.query(Annual_Data)
.join(Glacier, Glacier.id == Annual_Data.id)
.filter_by(name=form.name.data)
.group_by(Annual_Data.year)
2020-01-09 21:10:27 +01:00
)
if glacier_name.first() is None:
flash("Sorry, no results found")
return redirect(url_for("table_selection"))
return glacier_name
return annual_data
2020-01-09 21:10:27 +01:00
def query_user(form) -> BaseQuery:
2020-01-09 21:10:27 +01:00
user = User.query.filter_by(username=form.username.data).first()
return user