105 lines
5.4 KiB
Python
105 lines
5.4 KiB
Python
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators
|
|
from flask import request, render_template, redirect
|
|
from flask_wtf import FlaskForm
|
|
from main import db, app, ma
|
|
from sqlalchemy import Sequence
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from status import st, Status
|
|
|
|
################################################################################
|
|
# Class describing Publisher in the database, and via sqlalchemy, connected to the DB as well
|
|
################################################################################
|
|
class Publisher(db.Model):
|
|
id = db.Column(db.Integer, db.Sequence('publisher_id_seq'), primary_key=True )
|
|
name = db.Column(db.String(50), unique=True, 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(FlaskForm):
|
|
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():
|
|
objects = Publisher.query.order_by('id').all()
|
|
return render_template("show_id_name.html", objects=objects, page_title='Show All Publishers', url_base='publisher', alert=st.GetAlert(), message=st.GetMessage() )
|
|
|
|
################################################################################
|
|
# /publisher -> GET/POST -> creates a new publisher type and when created, takes you back to /publishers
|
|
################################################################################
|
|
@app.route("/publisher", methods=["GET", "POST"])
|
|
def new_publisher():
|
|
form = PublisherForm(request.form)
|
|
page_title='Create new Publisher'
|
|
if 'name' not in request.form:
|
|
return render_template("edit_id_name.html", form=form, page_title=page_title )
|
|
else:
|
|
publisher = Publisher( name=request.form["name"] )
|
|
try:
|
|
db.session.add(publisher)
|
|
db.session.commit()
|
|
st.SetMessage( "Created new Publisher (id={})".format(publisher.id) )
|
|
return redirect( '/publishers' )
|
|
except SQLAlchemyError as e:
|
|
st.SetAlert( "danger" )
|
|
st.SetMessage( "<b>Failed to add Publisher:</b> {}".format( e.orig) )
|
|
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
|
|
|
################################################################################
|
|
# /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?
|
|
form = PublisherForm(request.form)
|
|
page_title='Edit Publisher'
|
|
if request.method == 'POST' and form.validate():
|
|
try:
|
|
publisher = Publisher.query.get(id)
|
|
if 'delete' in request.form:
|
|
st.SetMessage("Successfully deleted (id={}, name={})".format( publisher.id, publisher.name ) )
|
|
publisher = Publisher.query.filter(Publisher.id==id).delete()
|
|
if 'submit' in request.form:
|
|
st.SetMessage("Successfully Updated Publisher (id={})".format(id) )
|
|
publisher.name = request.form['name']
|
|
db.session.commit()
|
|
return redirect( '/publishers' )
|
|
except SQLAlchemyError as e:
|
|
st.SetAlert( "danger" )
|
|
st.SetMessage( "<b>Failed to modify Publisher:</b> {}".format(e.orig) )
|
|
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
|
else:
|
|
publisher = Publisher.query.get(id)
|
|
form = PublisherForm(request.values, obj=publisher)
|
|
return render_template("edit_id_name.html", form=form, page_title=page_title )
|
|
|
|
################################################################################
|
|
# Gets the Publisher matching id from DB, helper func in jinja2 code to show books
|
|
################################################################################
|
|
def GetPublisherById(id):
|
|
return Publisher.query.get(id).name
|
|
|
|
################################################################################
|
|
# Gets the list of Publishers, populates publisher_list var in jinja2 code in book.html
|
|
################################################################################
|
|
def GetPublishers():
|
|
publishers = Publisher.query.order_by('name').all()
|
|
return publishers
|