fixed bug with route to / now needing to be to base.html, not main.html, then just added publisher (list all/edit 1/deleted button)
This commit is contained in:
64
publisher.py
Normal file
64
publisher.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from wtforms import SubmitField, StringField, HiddenField, validators, Form
|
||||
from flask import request, render_template
|
||||
from __main__ import db, app, ma
|
||||
|
||||
################################################################################
|
||||
# Class describing Publisher in the database, and via sqlalchemy, connected to the DB as well
|
||||
################################################################################
|
||||
class Publisher(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 Publisher into json / useful in jinja2
|
||||
################################################################################
|
||||
class PublisherSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta: model = Publisher
|
||||
|
||||
################################################################################
|
||||
# Helper class that defines a form for publisher, used to make html <form>, with field validation (via wtforms)
|
||||
################################################################################
|
||||
class PublisherForm(Form):
|
||||
id = HiddenField()
|
||||
name = StringField('Name:', [validators.DataRequired()])
|
||||
submit = SubmitField('Save' )
|
||||
delete = SubmitField('Delete' )
|
||||
|
||||
################################################################################
|
||||
# Routes for publisher data
|
||||
#
|
||||
# /publishers -> GET only -> prints out list of all publishers
|
||||
################################################################################
|
||||
@app.route("/publishers", methods=["GET"])
|
||||
def publishers():
|
||||
publishers = Publisher.query.all()
|
||||
return render_template("publishers.html", publishers=publishers)
|
||||
|
||||
################################################################################
|
||||
# /publisher/<id> -> GET/POST(save or delete) -> shows/edits/delets a single publisher
|
||||
################################################################################
|
||||
@app.route("/publisher/<id>", methods=["GET", "POST"])
|
||||
def publisher(id):
|
||||
### DDP: should this be request.form or request.values?
|
||||
alert="Success"
|
||||
publisher_form = PublisherForm(request.form)
|
||||
if request.method == 'POST' and publisher_form.validate():
|
||||
id = request.form['id']
|
||||
publisher = Publisher.query.get(id)
|
||||
try:
|
||||
request.form['submit']
|
||||
except:
|
||||
message="Sorry, Deleting unsupported at present"
|
||||
alert="Danger"
|
||||
else:
|
||||
publisher.name = request.form['name']
|
||||
db.session.commit()
|
||||
message="Successfully Updated Publisher (id={})".format(id)
|
||||
else:
|
||||
publisher = Publisher.query.get(id)
|
||||
publisher_form = PublisherForm(request.values, obj=publisher)
|
||||
message=""
|
||||
return render_template("publisher.html", publisher=publisher, alert=alert, message=message, publisher_form=publisher_form)
|
||||
Reference in New Issue
Block a user