86 lines
4.3 KiB
Python
86 lines
4.3 KiB
Python
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators
|
|
from flask import request, render_template, redirect
|
|
from flask_wtf import FlaskForm
|
|
from __main__ import db, app, ma
|
|
from sqlalchemy import func, Sequence
|
|
from status import st, Status
|
|
|
|
################################################################################
|
|
# Class describing Condition in the database, and via sqlalchemy, connected to the DB as well
|
|
################################################################################
|
|
class Condition(db.Model):
|
|
id = db.Column(db.Integer, db.Sequence('condition_id_seq'), primary_key=True )
|
|
name = db.Column(db.String(50), unique=True, nullable=False)
|
|
|
|
def __repr__(self):
|
|
return "<id: {}, name: {}>".format(self.id,self.name)
|
|
|
|
################################################################################
|
|
# Helper class that inherits a .dump() method to turn class Condition into json / useful in jinja2
|
|
################################################################################
|
|
class ConditionSchema(ma.SQLAlchemyAutoSchema):
|
|
class Meta: model = Condition
|
|
|
|
################################################################################
|
|
# Helper class that defines a form for condition, used to make html <form>, with field validation (via wtforms)
|
|
################################################################################
|
|
class ConditionForm(FlaskForm):
|
|
id = HiddenField()
|
|
name = StringField('Name:', [validators.DataRequired()])
|
|
submit = SubmitField('Save' )
|
|
delete = SubmitField('Delete' )
|
|
|
|
################################################################################
|
|
# Routes for condition data
|
|
#
|
|
# /conditions -> GET only -> prints out list of all conditions
|
|
################################################################################
|
|
@app.route("/conditions", methods=["GET"])
|
|
def conditions():
|
|
objects = Condition.query.order_by('id').all()
|
|
return render_template("show_id_name.html", objects=objects, page_title='Show All Conditions', url_base='condition', alert=st.GetAlert(), message=st.GetMessage() )
|
|
|
|
################################################################################
|
|
# /condition -> GET/POST -> creates a new condition type and when created, takes you back to /conditions
|
|
################################################################################
|
|
@app.route("/condition", methods=["GET", "POST"])
|
|
def new_condition():
|
|
form = ConditionForm(request.form)
|
|
if 'name' not in request.form:
|
|
return render_template("edit_id_name.html", form=form, page_title='Create new Condition' )
|
|
else:
|
|
condition = Condition( name=request.form["name"] )
|
|
db.session.add(condition)
|
|
db.session.commit()
|
|
st.SetMessage( "Created new Condition (id={})".format(condition.id) )
|
|
conditions = Condition.query.order_by('id').all()
|
|
return redirect( '/conditions' )
|
|
|
|
################################################################################
|
|
# /condition/<id> -> GET/POST(save or delete) -> shows/edits/delets a single condition
|
|
################################################################################
|
|
@app.route("/condition/<id>", methods=["GET", "POST"])
|
|
def condition(id):
|
|
### DDP: should this be request.form or request.values?
|
|
form = ConditionForm(request.form)
|
|
if request.method == 'POST' and form.validate():
|
|
condition = Condition.query.get(id)
|
|
if 'delete' in request.form:
|
|
st.SetMessage("Successfully deleted (id={}, name={})".format( condition.id, condition.name ) )
|
|
condition = Condition.query.filter(Condition.id==id).delete()
|
|
if 'submit' in request.form:
|
|
st.SetMessage("Successfully Updated Condition (id={})".format(id) )
|
|
condition.name = request.form['name']
|
|
db.session.commit()
|
|
return redirect( '/conditions' )
|
|
else:
|
|
condition = Condition.query.get(id)
|
|
form = ConditionForm(request.values, obj=condition)
|
|
return render_template("edit_id_name.html", form=form, page_title='Edit Condition' )
|
|
|
|
################################################################################
|
|
# Gets the Condition matching id from DB, helper func in jinja2 code to show books
|
|
################################################################################
|
|
def GetConditionById(id):
|
|
return Condition.query.get(id).name
|