removed all old *_lst tables, added corresponding new classes, etc. fro covertype, owned, rating, and dropped tables from DB, etc. Updated base.html to use new tables as drop-downs that are set correctly. So far slight hack on BookForm, will finish that after syncing this all back to mara. If I do the sync, and export/import this version of DB, then the fixes.sql and fix_db() code included in main.py can be removed. Finally, lamely added a favicon and a static/ to support it
This commit is contained in:
66
covertype.py
Normal file
66
covertype.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators, Form
|
||||
from flask import request, render_template
|
||||
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(Form):
|
||||
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("covertypes.html", covertypes=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("covertype.html", covertype=covertype, alert=alert, message=message, covertype_form=covertype_form)
|
||||
Reference in New Issue
Block a user