now have a class to maintain the status, use that to set alert/message for owned, and redirect after create/update/delete owned -> owneds

This commit is contained in:
2020-12-05 11:00:53 +11:00
parent d61e830525
commit 5057e856be
3 changed files with 26 additions and 25 deletions

25
main.py
View File

@@ -4,7 +4,9 @@ from flask_marshmallow import Marshmallow
from flask_bootstrap import Bootstrap
from wtforms import SubmitField, StringField, HiddenField, SelectField, IntegerField, TextAreaField, validators
from flask_wtf import FlaskForm
from status import st, Status
####################################### Flask App globals #######################################
app = Flask(__name__)
### what is this value? I gather I should chagne it?
DB_URL = 'postgresql+psycopg2://ddp:NWNlfa01@127.0.0.1:5432/library'
@@ -15,6 +17,7 @@ db = SQLAlchemy(app)
ma = Marshmallow(app)
Bootstrap(app)
################################# Now, import non-book classes ###################################
from author import Author, AuthorForm, AuthorSchema, GetAuthors
from publisher import Publisher, PublisherForm, PublisherSchema, GetPublisherById
from genre import Genre, GenreForm, GenreSchema, GetGenres
@@ -173,16 +176,6 @@ class BookForm(FlaskForm):
delete = SubmitField('Delete' )
# allow jinja2 to call this python function
app.jinja_env.globals['GetCovertypeById'] = GetCovertypeById
app.jinja_env.globals['GetOwnedById'] = GetOwnedById
app.jinja_env.globals['GetConditionById'] = GetConditionById
app.jinja_env.globals['GetPublisherById'] = GetPublisherById
app.jinja_env.globals['SeriesBookNum'] = SeriesBookNum
### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?)
book_schema = BookSchema()
################################# helper functions ###################################
def GetBookIdFromSeriesByBookNum( series_id, book_num ):
tmp_book = Book_Series_Link.query.filter(Book_Series_Link.series_id==series_id,Book_Series_Link.book_num==book_num).all()
@@ -199,6 +192,18 @@ def GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book_id, sub_book_num ):
tmp_bsbl = Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==book_id, Book_Sub_Book_Link.sub_book_num==sub_book_num ).all()
return tmp_bsbl[0].sub_book_id
####################################### GLOBALS #######################################
# allow jinja2 to call this python function
app.jinja_env.globals['GetCovertypeById'] = GetCovertypeById
app.jinja_env.globals['GetOwnedById'] = GetOwnedById
app.jinja_env.globals['GetConditionById'] = GetConditionById
app.jinja_env.globals['GetPublisherById'] = GetPublisherById
app.jinja_env.globals['SeriesBookNum'] = SeriesBookNum
app.jinja_env.globals['ClearStatus'] = st.ClearMessage
### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?)
book_schema = BookSchema()
####################################### ROUTES #######################################
@app.route("/books", methods=["GET"])
def books():

View File

@@ -1,9 +1,9 @@
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators
from flask import request, render_template
from flask import request, render_template, redirect
from flask_wtf import FlaskForm
from __main__ import db, app, ma
from sqlalchemy import func, Sequence
from status import st, Status
################################################################################
# Class describing Owned in the database, and via sqlalchemy, connected to the DB as well
@@ -38,26 +38,24 @@ class OwnedForm(FlaskForm):
@app.route("/owneds", methods=["GET"])
def owneds():
owneds = Owned.query.order_by('id').all()
return render_template("show_id_name.html", objects=owneds, page_title='Show All Ownership types', url_base='owned')
return render_template("show_id_name.html", objects=owneds, page_title='Show All Ownership types', url_base='owned', alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /owned -> GET/POST -> creates a new owned type and when created, takes you back to /owneds
################################################################################
@app.route("/owned", methods=["GET", "POST"])
def new_owned():
alert="success"
owned_form = OwnedForm(request.form)
if 'name' not in request.form:
owned=None
message=""
return render_template("edit_id_name.html", owned=owned, alert=alert, message=message, form=owned_form, page_title='Create new Owned Type' )
return render_template("edit_id_name.html", owned=owned, form=owned_form, page_title='Create new Owned Type' )
else:
owned = Owned( name=request.form["name"] )
db.session.add(owned)
db.session.commit()
message="Created new Owned Type (id={})".format(owned.id)
st.SetMessage( "Created new Owned Type (id={})".format(owned.id) )
owneds = Owned.query.order_by('id').all()
return render_template("show_id_name.html", objects=owneds, page_title='Show All Ownership types', url_base='owned', alert=alert, message=message)
return redirect( '/owneds' )
################################################################################
# /owned/<id> -> GET/POST(save or delete) -> shows/edits/delets a single
@@ -65,25 +63,22 @@ def new_owned():
################################################################################
@app.route("/owned/<id>", methods=["GET", "POST"])
def owned(id):
alert="success"
### DDP: should this be request.form or request.values?
owned_form = OwnedForm(request.form)
if request.method == 'POST' and owned_form.validate():
owned = Owned.query.get(id)
if 'delete' in request.form:
message="Successfully deleted (id={}, name={})".format( owned.id, owned.name )
st.SetMessage("Successfully deleted (id={}, name={})".format( owned.id, owned.name ) )
owned = Owned.query.filter(Owned.id==id).delete()
if 'submit' in request.form:
owned.name = request.form['name']
message="Successfully Updated Owned (id={})".format(id)
st.SetMessage("Successfully Updated Owned (id={})".format(id) )
db.session.commit()
owneds = Owned.query.order_by('id').all()
return render_template("show_id_name.html", objects=owneds, page_title='Show All Ownership types', url_base='owned', alert=alert, message=message)
return redirect( '/owneds' )
else:
owned = Owned.query.get(id)
owned_form = OwnedForm(request.values, obj=owned)
message=""
return render_template("edit_id_name.html", owned=owned, alert=alert, message=message, form=owned_form, page_title='Edit Owned Type' )
return render_template("edit_id_name.html", owned=owned, form=owned_form, page_title='Edit Owned Type' )
def GetOwnedById(id):
return Owned.query.get(id).name

View File

@@ -5,6 +5,7 @@
{% if message|length %}
<div class="row alert alert-{{alert}}">
{{message}}
{{ ClearStatus() }}
</div>
{% endif %}
<table id="book_table" class="table table-striped table-sm" data-toolbar="#toolbar" data-search="true">