igdb/code/database/queries.py

39 lines
1.2 KiB
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
2020-01-10 23:29:23 +01:00
def query_plot_data(form) -> BaseQuery:
query = (
db.session.query(Annual_Data)
.join(Glacier, Glacier.id == Annual_Data.id)
.filter_by(name=form.name.data)
.group_by(Annual_Data.year)
)
if query.first() is None:
flash("Sorry, no results found")
return redirect(url_for("plot_selection"))
return query