Files
books/covertype.py

76 lines
3.3 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 Covertype in the database, and via sqlalchemy, connected to the DB as well
################################################################################
class Covertype(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 Covertype into json / useful in jinja2
################################################################################
class CovertypeSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Covertype
################################################################################
# Helper class that defines a form for covertype, used to make html <form>, with field validation (via wtforms)
################################################################################
class CovertypeForm(FlaskForm):
id = HiddenField()
name = StringField('Name:', [validators.DataRequired()])
submit = SubmitField('Save' )
delete = SubmitField('Delete' )
################################################################################
# Routes for covertype data
#
# /covertypes -> GET only -> prints out list of all covertypes
################################################################################
@app.route("/covertypes", methods=["GET"])
def covertypes():
covertypes = Covertype.query.order_by('id').all()
return render_template( "show_id_name.html", objects=covertypes, page_title='Show All Covertypes', url_base='covertype')
def GetCovertypes():
covertypes = Covertype.query.order_by('name').all()
cs = CovertypeSchema(many=True)
return cs.dump( covertypes )
################################################################################
# /covertype/<id> -> GET/POST(save or delete) -> shows/edits/delets a single
# covertype
################################################################################
@app.route("/covertype/<id>", methods=["GET", "POST"])
def covertype(id):
### DDP: should this be request.form or request.values?
alert="Success"
covertype_form = CovertypeForm(request.form)
if request.method == 'POST' and covertype_form.validate():
id = request.form['id']
covertype = Covertype.query.get(id)
try:
request.form['submit']
except:
message="Sorry, Deleting unsupported at present"
alert="Danger"
else:
covertype.name = request.form['name']
db.session.commit()
message="Successfully Updated Covertype (id={})".format(id)
else:
covertype = Covertype.query.get(id)
covertype_form = CovertypeForm(request.values, obj=covertype)
message=""
return render_template("edit_id_name.html", covertype=covertype, alert=alert, message=message, form=covertype_form, page_title='Edit Covertype')
def GetCovertypeById(id):
return Covertype.query.get(id).name