Move query logic to new module

This commit is contained in:
coolneng 2020-01-09 21:10:27 +01:00
parent c6e63cf087
commit 65d3af6817
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
3 changed files with 32 additions and 18 deletions

View File

@ -45,14 +45,11 @@ CLOSED: [2019-11-01 Fri 00:34]
- [X] Black box
- [X] Entity-Relationship
** Implementation
*** TODO Backend [2/4] [50%]
*** TODO Backend [3/4] [75%]
**** TODO Flask Application [2/3] [66%]
- [ ] Plots with pandas
- [X] Login for admin
- [X] Tables with pandas
**** TODO Possible additions [1/2] [50%]
- [ ] Text search for glaciers
- [X] Form seach for a year
**** DONE Database [3/3] [100%]
CLOSED: [2020-01-03 Fri 00:44]
- [X] Connection
@ -63,6 +60,10 @@ CLOSED: [2020-01-08 Wed 03:18]
- [X] Select useful fiels
- [X] Convert PU to Country (ISO 3166)
- [X] Insert into database
**** DONE Possible additions [2/2] [100%]
CLOSED: [2020-01-09 Thu 20:57]
- [X] Text search for glaciers
- [X] Form seach for a year
*** TODO Documentation [0/2] [0%]
- [ ] Code
- [ ] Explanations (uid as Varchar, ORM)

View File

@ -5,6 +5,7 @@ from flask import flash, redirect, render_template, url_for, request
from flask_login import current_user, login_user, logout_user, login_required
from werkzeug.urls import url_parse
from processing.tabulate import create_table
from database.queries import query_annual_data, query_user
@app.route("/")
@ -19,7 +20,7 @@ def login():
return redirect(url_for("admin"))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
user = query_user(form)
if user is None or not user.check_password(form.password.data):
flash("Invalid username or password")
return redirect(url_for("login"))
@ -58,19 +59,10 @@ def data():
def table_selection():
form = YearForm()
if form.validate_on_submit():
annual_data = db.session.query(Annual_Data).filter_by(year=form.year.data)
query = annual_data
if form.name.data:
query = (
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)
)
if query.scalar() is None:
flash("Sorry, no results found")
return redirect(url_for("table_selection"))
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)

21
code/database/queries.py Normal file
View File

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