Remove Community model

This commit is contained in:
Matthew Dillon 2015-10-09 10:48:41 -07:00
parent c3344b00e7
commit eb17b450e7
4 changed files with 41 additions and 32 deletions

View file

@ -1,10 +1,10 @@
from flask_wtf import Form from flask_wtf import Form
from wtforms import IntegerField from wtforms import IntegerField, SelectField
from wtforms.validators import NumberRange, Required from wtforms.validators import NumberRange, Required
from wtforms.ext.sqlalchemy.fields import QuerySelectField from wtforms.ext.sqlalchemy.fields import QuerySelectField
from sqlalchemy import func from sqlalchemy import func
from .models import Community, Dataset, Temperature from .models import Dataset, Temperature, DB
class AKIYearField(IntegerField): class AKIYearField(IntegerField):
@ -17,10 +17,6 @@ class AKIYearField(IntegerField):
self.validators = [NumberRange(min=ymin, max=ymax), Required()] self.validators = [NumberRange(min=ymin, max=ymax), Required()]
def communities():
return Community.query.order_by('name')
def datasets(): def datasets():
return Dataset.query.order_by('datatype', 'model', 'scenario') return Dataset.query.order_by('datatype', 'model', 'scenario')
@ -30,10 +26,7 @@ def dataset_names(ds):
class AKIForm(Form): class AKIForm(Form):
community = QuerySelectField(query_factory=communities, community = SelectField(coerce=int,
get_label='name',
allow_blank=True,
blank_text='---Select a community---',
validators=[Required(message='Please select a community')]) validators=[Required(message='Please select a community')])
minyear = AKIYearField('minyear') minyear = AKIYearField('minyear')

View file

@ -1,17 +1,7 @@
from app import db from app import db
from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.sql import text
from flask import abort
class Community(db.Model):
__tablename__ = 'communities'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False, unique=True)
northing = db.Column(db.Float, nullable=False)
easting = db.Column(db.Float, nullable=False)
latitude = db.Column(db.Float, nullable=False)
longitude = db.Column(db.Float, nullable=False)
temperatures = db.relationship('Temperature', backref='communities')
class Dataset(db.Model): class Dataset(db.Model):
@ -50,3 +40,25 @@ class Temperature(db.Model):
november = db.Column(db.Float, nullable=False) november = db.Column(db.Float, nullable=False)
december = db.Column(db.Float, nullable=False) december = db.Column(db.Float, nullable=False)
updated = db.Column(db.DateTime, nullable=True) updated = db.Column(db.DateTime, nullable=True)
class DB:
@classmethod
def getCommunity(cls, id):
cmd = """
SELECT id, name, latitude, longitude, northing, easting
FROM new_communities
WHERE id=:id;
"""
result = db.engine.execute(text(cmd), id=id).fetchone()
return result or abort(500)
@classmethod
def getCommunities(cls):
cmd = """
SELECT id, name
FROM new_communities
ORDER BY name ASC;
"""
result = db.engine.execute(text(cmd), id=id).fetchall()
return result or abort(500)

View file

@ -1,6 +1,6 @@
import numpy import numpy
from .models import Temperature, Dataset from .models import Temperature, Dataset, DB
def getTemps(datasets, community_id, minyear, maxyear): def getTemps(datasets, community_id, minyear, maxyear):
@ -69,3 +69,6 @@ def des_air_indices(indices):
def c_to_f(temp): def c_to_f(temp):
return (temp * 9. / 5.) + 32. return (temp * 9. / 5.) + 32.
def communitiesSelect():
return [(c.id, c.name) for c in DB.getCommunities()]

View file

@ -5,13 +5,13 @@ from flask import session, render_template, request, redirect, url_for
from . import main from . import main
from .forms import AKIForm from .forms import AKIForm
from .utils import getTemps, avg_air_temp, ann_air_indices, \ from .utils import getTemps, avg_air_temp, ann_air_indices, \
avg_air_indices, des_air_indices avg_air_indices, des_air_indices, communitiesSelect
from .models import Community, Dataset from .models import Dataset, DB
@main.route('/', methods=['GET']) @main.route('/', methods=['GET'])
def index(): def index():
form = AKIForm() form = AKIForm()
form.community.choices = communitiesSelect()
session['community_data'] = None session['community_data'] = None
session['avg_temp'] = None session['avg_temp'] = None
session['avg_indices'] = None session['avg_indices'] = None
@ -20,13 +20,13 @@ def index():
if 'community' in session: if 'community' in session:
community_id = session['community'] community_id = session['community']
if all(key in session for key in ('minyear', 'maxyear', 'datasets')): if all(key in session for key in ('minyear', 'maxyear', 'datasets')):
community = Community.query.get_or_404(community_id) community = DB.getCommunity(community_id)
session['community_data'] = { session['community_data'] = {
'id': community_id, 'id': community_id,
'name': community.name, 'name': community['name'],
'latitude': round(community.latitude, 5), 'latitude': round(community['latitude'], 5),
'longitude': round(community.longitude, 5), 'longitude': round(community['longitude'], 5),
} }
session['ds_name'] = Dataset.query. \ session['ds_name'] = Dataset.query. \
@ -48,6 +48,7 @@ def index():
@main.route('/', methods=['POST']) @main.route('/', methods=['POST'])
def index_submit(): def index_submit():
form = AKIForm() form = AKIForm()
form.community.choices = communitiesSelect()
if form.validate(): if form.validate():
session['community'] = request.form['community'] session['community'] = request.form['community']
session['minyear'] = request.form['minyear'] session['minyear'] = request.form['minyear']
@ -58,7 +59,7 @@ def index_submit():
session['datasets'] = request.form['model'] session['datasets'] = request.form['model']
return redirect(url_for('main.index')) return redirect(url_for('main.index'))
else: else:
return render_template("main/index.html", form=form) return render_template('main/index.html', form=form)
@main.route('/datatypes') @main.route('/datatypes')