Cleanup
This commit is contained in:
parent
f70f9d61b4
commit
d692c6c744
5 changed files with 16 additions and 63 deletions
|
@ -3,20 +3,13 @@ from wtforms import IntegerField, SelectField
|
||||||
from wtforms.validators import NumberRange, Required
|
from wtforms.validators import NumberRange, Required
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
|
|
||||||
from .models import Temperature, DB
|
|
||||||
|
|
||||||
from flask import current_app
|
|
||||||
|
|
||||||
|
|
||||||
class AKIYearField(IntegerField):
|
class AKIYearField(IntegerField):
|
||||||
def pre_validate(self, form):
|
def pre_validate(self, form):
|
||||||
pass
|
if form.data['dataset'] == 'CRU,TS31':
|
||||||
# if form.dataset.data is not None:
|
self.validators = [NumberRange(min=1901, max=2009), Required()]
|
||||||
# ymin, ymax = Temperature.query \
|
else:
|
||||||
# .with_entities(func.min(Temperature.year),
|
self.validators = [NumberRange(min=2001, max=2099), Required()]
|
||||||
# func.max(Temperature.year)) \
|
|
||||||
# .filter(Temperature.dataset_id == form.dataset.data.id).all()[0]
|
|
||||||
# self.validators = [NumberRange(min=ymin, max=ymax), Required()]
|
|
||||||
|
|
||||||
|
|
||||||
class AKIForm(Form):
|
class AKIForm(Form):
|
||||||
|
|
|
@ -4,44 +4,6 @@ from sqlalchemy.sql import text
|
||||||
from flask import abort
|
from flask import abort
|
||||||
|
|
||||||
|
|
||||||
class Dataset(db.Model):
|
|
||||||
__tablename__ = 'datasets'
|
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
|
||||||
datatype = db.Column(db.String(15), nullable=False)
|
|
||||||
model = db.Column(db.String(15), nullable=False)
|
|
||||||
modelname = db.Column(db.String(50), nullable=False)
|
|
||||||
scenario = db.Column(db.String(15), nullable=False)
|
|
||||||
resolution = db.Column(db.String(15), nullable=False)
|
|
||||||
temperatures = db.relationship('Temperature', backref='datasets')
|
|
||||||
|
|
||||||
@hybrid_property
|
|
||||||
def type(self):
|
|
||||||
return self.datatype.lower().capitalize()
|
|
||||||
|
|
||||||
|
|
||||||
class Temperature(db.Model):
|
|
||||||
__tablename__ = 'temperatures'
|
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
|
||||||
dataset_id = db.Column(db.Integer, db.ForeignKey('datasets.id'))
|
|
||||||
community_id = db.Column(db.Integer, db.ForeignKey('communities.id'))
|
|
||||||
year = db.Column(db.Integer, nullable=False)
|
|
||||||
january = db.Column(db.Float, nullable=False)
|
|
||||||
february = db.Column(db.Float, nullable=False)
|
|
||||||
march = db.Column(db.Float, nullable=False)
|
|
||||||
april = db.Column(db.Float, nullable=False)
|
|
||||||
may = db.Column(db.Float, nullable=False)
|
|
||||||
june = db.Column(db.Float, nullable=False)
|
|
||||||
july = db.Column(db.Float, nullable=False)
|
|
||||||
august = db.Column(db.Float, nullable=False)
|
|
||||||
september = db.Column(db.Float, nullable=False)
|
|
||||||
october = db.Column(db.Float, nullable=False)
|
|
||||||
november = db.Column(db.Float, nullable=False)
|
|
||||||
december = db.Column(db.Float, nullable=False)
|
|
||||||
updated = db.Column(db.DateTime, nullable=True)
|
|
||||||
|
|
||||||
|
|
||||||
class DB:
|
class DB:
|
||||||
@classmethod
|
@classmethod
|
||||||
def getCommunity(cls, id):
|
def getCommunity(cls, id):
|
||||||
|
@ -67,13 +29,11 @@ class DB:
|
||||||
def getDatasets(cls):
|
def getDatasets(cls):
|
||||||
cmd = """
|
cmd = """
|
||||||
SELECT DISTINCT ON (
|
SELECT DISTINCT ON (
|
||||||
doc->'model',
|
|
||||||
doc->'datatype',
|
doc->'datatype',
|
||||||
doc->'resolution',
|
doc->'resolution',
|
||||||
doc->'modelname',
|
doc->'modelname',
|
||||||
doc->'scenario'
|
doc->'scenario'
|
||||||
)
|
)
|
||||||
doc->'model' AS model,
|
|
||||||
doc->'datatype' AS datatype,
|
doc->'datatype' AS datatype,
|
||||||
doc->'resolution' AS resolution,
|
doc->'resolution' AS resolution,
|
||||||
doc->'modelname' AS modelname,
|
doc->'modelname' AS modelname,
|
||||||
|
@ -87,22 +47,22 @@ class DB:
|
||||||
return result or abort(500)
|
return result or abort(500)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def getTemps(cls, start, end, community_id, model, scenario):
|
def getTemps(cls, start, end, community_id, modelname, scenario):
|
||||||
years = [str(x) for x in range(int(start), int(end)+1)]
|
years = [str(x) for x in range(int(start), int(end)+1)]
|
||||||
cmd = """
|
cmd = """
|
||||||
WITH x AS (
|
WITH x AS (
|
||||||
SELECT name, jsonb_array_elements(data) AS data
|
SELECT name, jsonb_array_elements(data) AS data
|
||||||
FROM new_communities
|
FROM new_communities
|
||||||
WHERE id=:community_id)
|
WHERE id=:community_id)
|
||||||
SELECT d.key::INTEGER AS year, d.value AS temperatures
|
SELECT d.key AS year, d.value AS temperatures
|
||||||
FROM x, jsonb_each(data) d
|
FROM x, jsonb_each(data) d
|
||||||
WHERE data->>'model'=:model
|
WHERE data->>'modelname'=:modelname
|
||||||
AND data->>'scenario'=:scenario
|
AND data->>'scenario'=:scenario
|
||||||
AND d.key IN :years;
|
AND d.key IN :years;
|
||||||
"""
|
"""
|
||||||
result = db.engine.execute(text(cmd),
|
result = db.engine.execute(text(cmd),
|
||||||
community_id=community_id,
|
community_id=community_id,
|
||||||
model=model,
|
modelname=modelname,
|
||||||
scenario=scenario,
|
scenario=scenario,
|
||||||
years=tuple(years)).fetchall()
|
years=tuple(years)).fetchall()
|
||||||
return result or abort(500)
|
return result or abort(500)
|
||||||
|
|
|
@ -137,7 +137,7 @@
|
||||||
<td>{{ value['avg_indices'][1] }}</td>
|
<td>{{ value['avg_indices'][1] }}</td>
|
||||||
<td>{{ value['des_indices'][0] }}</td>
|
<td>{{ value['des_indices'][0] }}</td>
|
||||||
<td>{{ value['des_indices'][1] }}</td>
|
<td>{{ value['des_indices'][1] }}</td>
|
||||||
<td>{{ value['ds_name'][0][0] }} ({{ value['ds_name'][0][1] }})</td>
|
<td>{{ value['ds_name'][0] }} ({{ value['ds_name'][1] }})</td>
|
||||||
<td><button type="button" class="btn btn-danger btn-sm"
|
<td><button type="button" class="btn btn-danger btn-sm"
|
||||||
onclick="window.location.href='{{ url_for('main.delete', record=key) }}'"
|
onclick="window.location.href='{{ url_for('main.delete', record=key) }}'"
|
||||||
title="Click to delete this search">
|
title="Click to delete this search">
|
||||||
|
|
|
@ -2,11 +2,11 @@ from .models import DB
|
||||||
|
|
||||||
|
|
||||||
def getTemps(session):
|
def getTemps(session):
|
||||||
model, scenario = session['datasets'].split(',')
|
modelname, scenario = session['datasets'].split(',')
|
||||||
data = DB.getTemps(session['minyear'],
|
data = DB.getTemps(session['minyear'],
|
||||||
session['maxyear'],
|
session['maxyear'],
|
||||||
session['community_data']['id'],
|
session['community_data']['id'],
|
||||||
model,
|
modelname,
|
||||||
scenario)
|
scenario)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
|
@ -70,11 +70,11 @@ def reset():
|
||||||
|
|
||||||
@main.route('/details')
|
@main.route('/details')
|
||||||
def details():
|
def details():
|
||||||
datasets = request.args.get('datasets', '')
|
temps = getTemps({'datasets': request.args.get('datasets', ''),
|
||||||
community_id = request.args.get('community_id', '')
|
'minyear': request.args.get('minyear', ''),
|
||||||
minyear = request.args.get('minyear', '')
|
'maxyear': request.args.get('maxyear', ''),
|
||||||
maxyear = request.args.get('maxyear', '')
|
'community_data': {'id': request.args.get('community_id', '')}
|
||||||
temps = getTemps(session)
|
})
|
||||||
return render_template('main/details.html',
|
return render_template('main/details.html',
|
||||||
lat=request.args.get('lat', ''),
|
lat=request.args.get('lat', ''),
|
||||||
lon=request.args.get('lon', ''),
|
lon=request.args.get('lon', ''),
|
||||||
|
|
Loading…
Add table
Reference in a new issue