68 lines
3.1 KiB
Python
68 lines
3.1 KiB
Python
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators
|
|
from flask import request, render_template
|
|
from flask_wtf import FlaskForm
|
|
from __main__ import db, app, ma
|
|
|
|
################################################################################
|
|
# Class describing Condition in the database, and via sqlalchemy, connected to the DB as well
|
|
################################################################################
|
|
class Condition(db.Model):
|
|
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
|
name = db.Column(db.String(50), unique=False, 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():
|
|
conditions = Condition.query.order_by('id').all()
|
|
return render_template("conditions.html", conditions=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?
|
|
alert="Success"
|
|
condition_form = ConditionForm(request.form)
|
|
if request.method == 'POST' and condition_form.validate():
|
|
id = request.form['id']
|
|
condition = Condition.query.get(id)
|
|
try:
|
|
request.form['submit']
|
|
except:
|
|
message="Sorry, Deleting unsupported at present"
|
|
alert="Danger"
|
|
else:
|
|
condition.name = request.form['name']
|
|
db.session.commit()
|
|
message="Successfully Updated Condition (id={})".format(id)
|
|
else:
|
|
condition = Condition.query.get(id)
|
|
condition_form = ConditionForm(request.values, obj=condition)
|
|
message=""
|
|
return render_template("id_name_form.html", condition=condition, alert=alert, message=message, form=condition_form, page_title='Edit Condition')
|