removed extraneous bits of schema definitions, cleaned up bug in __repr__ of book_sub_book, and still tinkering with schemas vs raw sql for book sub book linkages that then get passed to the html
This commit is contained in:
66
main.py
66
main.py
@@ -3,13 +3,19 @@ from flask import render_template
|
||||
from flask import request
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_marshmallow import Marshmallow
|
||||
from author import AuthorForm
|
||||
from flask_bootstrap import Bootstrap
|
||||
|
||||
|
||||
DB_URL = 'postgresql+psycopg2://ddp:NWNlfa01@127.0.0.1:5432/library'
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
### what is this value? I gather I should chagne it?
|
||||
app.config.from_mapping( SECRET_KEY=b'\xd6\x04\xbdj\xfe\xed$c\x1e@\xad\x0f\x13,@G')
|
||||
db = SQLAlchemy(app)
|
||||
ma = Marshmallow(app)
|
||||
Bootstrap(app)
|
||||
|
||||
####################################### CLASSES / DB model #######################################
|
||||
book_author_link = db.Table('book_author_link', db.Model.metadata,
|
||||
@@ -34,7 +40,7 @@ class Book_Sub_Book_Link(db.Model):
|
||||
sub_book_num = db.Column(db.Integer)
|
||||
|
||||
def __repr__(self):
|
||||
return "<book_id: {}, sub_book_id: {}, sub_book_num: {}".format(self.book_id, self.sub_book_id, self.sub_book_num)
|
||||
return "<book_id: {}, sub_book_id: {}, sub_book_num: {}>".format(self.book_id, self.sub_book_id, self.sub_book_num)
|
||||
|
||||
class Book(db.Model):
|
||||
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
||||
@@ -83,28 +89,16 @@ class Genre_Lst(db.Model):
|
||||
|
||||
### setup serializer schemas, to make returning books/authors easier
|
||||
class AuthorSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Author
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
class Meta: model = Author
|
||||
|
||||
class PublisherSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Publisher
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
class Meta: model = Publisher
|
||||
|
||||
class Genre_LstSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Genre_Lst
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
class Meta: model = Genre_Lst
|
||||
|
||||
class Book_Sub_Book_LinkSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Book_Sub_Book_Link
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
class Meta: model = Book_Sub_Book_Link
|
||||
|
||||
class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||
author = ma.Nested(AuthorSchema, many=True)
|
||||
@@ -112,39 +106,37 @@ class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||
genre = ma.Nested(Genre_LstSchema, many=True)
|
||||
parent_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
|
||||
child_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
|
||||
class Meta:
|
||||
model = Book
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
class Meta: model = Book
|
||||
|
||||
### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?)
|
||||
book_schema = BookSchema()
|
||||
books_schema = BookSchema(many=True)
|
||||
|
||||
####################################### ROUTES #######################################
|
||||
@app.route("/books", methods=["GET"])
|
||||
def books():
|
||||
if request.form:
|
||||
print(request.form)
|
||||
|
||||
### DDP: this fails... also, maybe we use ORM to build a parent_book (and I am child #4) and child_books???
|
||||
# books = Book.query.all()
|
||||
books = Book.query.outerjoin(Book_Sub_Book_Link, Book.id==Book_Sub_Book_Link.book_id).order_by(Book.id, Book_Sub_Book_Link.sub_book_num).all()
|
||||
|
||||
# want to get sub book info and patch it into the books object to at least reference sub_book_num and parent_book,
|
||||
# then per book in jinja2, slide it into the right aprt of the table with the right markup to show its a sub book
|
||||
subs = db.engine.execute ( "select * from book_sub_book_link" )
|
||||
for row in subs:
|
||||
index = next((i for i, item in enumerate(books) if item.id == row.sub_book_id), -1)
|
||||
books[index].parent_id = row.book_id
|
||||
books[index].sub_book_num = row.sub_book_num
|
||||
|
||||
return render_template("books.html", books=books)
|
||||
books_s = books_schema.dump(books)
|
||||
return render_template("books.html", books=books, tst=books_s)
|
||||
# return render_template("books.html", books=books_s)
|
||||
|
||||
@app.route("/book/<id>", methods=["GET"])
|
||||
def book(id):
|
||||
book = Book.query.get(id)
|
||||
book_s = book_schema.dump(book)
|
||||
|
||||
######
|
||||
###
|
||||
### okay, this manual hacking of sub_book is currently going to be needed, because in the jinja2 I want to list the book, and more than just the id numhber of the sub_book, I want the details... (sub_book_schema needs a book relationship BUT, dependencies mean I can't define a book schema inside sub_book schema, and I am definitely not sure how to join it anyway... for another time.
|
||||
###
|
||||
#####
|
||||
# force sub books for jinja2 to be able to use
|
||||
subs = db.engine.execute ( "select bsb.book_id, bsb.sub_book_id, bsb.sub_book_num, book.title, book.rating, book.year_published, book.notes, bal.author_id as author_id, author.surname||', '||author.firstnames as author from book_sub_book_link bsb, book, book_author_link bal, author where bsb.book_id = {} and book.id = bsb.sub_book_id and book.id = bal.book_id and bal.author_id = author.id".format( id ) )
|
||||
sub_book=[]
|
||||
@@ -173,19 +165,23 @@ def authors():
|
||||
|
||||
@app.route("/author/<id>", methods=["GET", "POST"])
|
||||
def author(id):
|
||||
if request.method == 'POST':
|
||||
id = request.form['author.id']
|
||||
### DDP: should this be request.form or request.values?
|
||||
author_form = AuthorForm(request.form)
|
||||
print("we are here")
|
||||
if request.method == 'POST' and author_form.validate():
|
||||
print("we are posting")
|
||||
id = request.form['id']
|
||||
author = Author.query.get(id)
|
||||
author.firstnames = request.form['author.firstnames']
|
||||
author.surname = request.form['author.surname']
|
||||
author.firstnames = request.form['firstnames']
|
||||
author.surname = request.form['surname']
|
||||
db.session.commit()
|
||||
message="Successfully Updated Author (id={})".format(id)
|
||||
else:
|
||||
author = Author.query.get(id)
|
||||
author_form = AuthorForm(request.values, obj=author)
|
||||
message=""
|
||||
return render_template("author.html", author=author, message=message)
|
||||
return render_template("author.html", author=author, message=message, author_form=author_form)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", debug=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user