okay, condition* now works, menu to call them works, just testing it out though, so also a BookForm started but unused at present. Finally, tweaked pagination size to 20 now that navbar takes up so much space, also made pagination choices match
This commit is contained in:
68
condition.py
Normal file
68
condition.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators, Form
|
||||
from flask import request, render_template
|
||||
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(Form):
|
||||
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()
|
||||
print ( Condition.query.order_by('id') )
|
||||
print ( conditions )
|
||||
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("condition.html", condition=condition, alert=alert, message=message, condition_form=condition_form)
|
||||
Reference in New Issue
Block a user