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:
Damien De Paoli
2020-11-17 21:22:15 +11:00
parent 8eddce043b
commit 9544790ffa
16 changed files with 466 additions and 47 deletions

42
main.py
View File

@@ -1,10 +1,9 @@
from flask import Flask
from flask import render_template
from flask import request
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_bootstrap import Bootstrap
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators, Form
from flask_wtf import FlaskForm
app = Flask(__name__)
### what is this value? I gather I should chagne it?
@@ -20,6 +19,9 @@ from author import Author, AuthorForm, AuthorSchema
from publisher import Publisher, PublisherForm, PublisherSchema
from genre import Genre, GenreForm, GenreSchema
from condition import Condition, ConditionForm, ConditionSchema
from covertype import Covertype, CovertypeForm, CovertypeSchema
from owned import Owned, OwnedForm, OwnedSchema
from rating import Rating, RatingForm, RatingSchema
####################################### CLASSES / DB model #######################################
book_author_link = db.Table('book_author_link', db.Model.metadata,
@@ -53,10 +55,10 @@ class Book(db.Model):
publisher = db.relationship('Publisher', secondary=book_publisher_link)
genre = db.relationship('Genre', secondary=book_genre_link )
year_published = db.Column(db.Integer)
owned = db.Column(db.String(20))
covertype = db.Column(db.String(20))
condition = db.Column(db.String(20))
rating = db.Column(db.String(20))
condition = db.Column(db.Integer, db.ForeignKey('condition.id'))
covertype = db.Column(db.Integer, db.ForeignKey('covertype.id'))
owned = db.Column(db.Integer, db.ForeignKey('owned.id'))
rating = db.Column(db.Integer, db.ForeignKey('rating.id'))
notes = db.Column(db.Text)
blurb = db.Column(db.Text)
created = db.Column(db.Date)
@@ -85,12 +87,15 @@ class BookSchema(ma.SQLAlchemyAutoSchema):
#
# To be completed
#
class BookForm(Form):
class BookForm(FlaskForm):
# I think I'll have to skip setting default on create, and using jquery to
# change it when I create the from? (or maybe I could use a default=set_me
# in the line below, then when I set create set_me = book.condition before
# bf=BookForm()
condition = SelectField( 'condition', choices=[(c.id, c.name) for c in Condition.query.order_by('id')] )
covertype = SelectField( 'covertype', choices=[(c.id, c.name) for c in Covertype.query.order_by('id')] )
owned = SelectField( 'owned', choices=[(c.id, c.name) for c in Owned.query.order_by('id')] )
rating = SelectField( 'rating', choices=[(c.id, c.name) for c in Rating.query.order_by('id')] )
### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?)
@@ -136,11 +141,30 @@ def book(id):
book_s['sub_book'] = sub_book
return render_template("book.html", books=book_s, subs=sub_book )
book_form=BookForm(request.form)
# set defaults for drop-down's based on this book
book_form.condition.default = book.condition
book_form.covertype.default = book.covertype
book_form.owned.default = book.owned
book_form.rating.default = book.rating
print(book_form)
book_form.process()
return render_template("book.html", books=book_s, subs=sub_book, book_form=book_form )
@app.route("/", methods=["GET"])
def main_page():
return render_template("base.html")
def fix_db():
books = Book.query.all()
for book in books:
rating=Rating.query.filter_by(name=book.rating).all()
book.temp_rating=rating[0].id
print(book.temp_rating)
db.session.commit()
print( "ran fix_db()")
if __name__ == "__main__":
# fix_db()
app.run(host="0.0.0.0", debug=True)