added in loans support, also particular route for book_for_loan without markup used as div content on loan.html page via ajax load

This commit is contained in:
2020-11-18 19:58:41 +11:00
parent 6a8d2731ac
commit d669ef71a4

17
main.py
View File

@@ -22,6 +22,7 @@ from condition import Condition, ConditionForm, ConditionSchema
from covertype import Covertype, CovertypeForm, CovertypeSchema from covertype import Covertype, CovertypeForm, CovertypeSchema
from owned import Owned, OwnedForm, OwnedSchema from owned import Owned, OwnedForm, OwnedSchema
from rating import Rating, RatingForm, RatingSchema from rating import Rating, RatingForm, RatingSchema
from loan import Loan, LoanForm, LoanSchema
####################################### CLASSES / DB model ####################################### ####################################### CLASSES / DB model #######################################
book_author_link = db.Table('book_author_link', db.Model.metadata, book_author_link = db.Table('book_author_link', db.Model.metadata,
@@ -39,6 +40,11 @@ book_genre_link = db.Table('book_genre_link', db.Model.metadata,
db.Column('genre_id', db.Integer, db.ForeignKey('genre.id')) db.Column('genre_id', db.Integer, db.ForeignKey('genre.id'))
) )
class Book_Loan_Link(db.Model):
__tablename__ = "book_loan_link"
book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True)
loan_id = db.Column(db.Integer, db.ForeignKey('loan.id'), unique=True, nullable=False, primary_key=True)
class Book_Sub_Book_Link(db.Model): class Book_Sub_Book_Link(db.Model):
__tablename__ = "book_sub_book_link" __tablename__ = "book_sub_book_link"
book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True) book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True)
@@ -54,6 +60,7 @@ class Book(db.Model):
author = db.relationship('Author', secondary=book_author_link) author = db.relationship('Author', secondary=book_author_link)
publisher = db.relationship('Publisher', secondary=book_publisher_link) publisher = db.relationship('Publisher', secondary=book_publisher_link)
genre = db.relationship('Genre', secondary=book_genre_link ) genre = db.relationship('Genre', secondary=book_genre_link )
loan = db.relationship('Loan', secondary=Book_Loan_Link.__table__);
year_published = db.Column(db.Integer) year_published = db.Column(db.Integer)
condition = db.Column(db.Integer, db.ForeignKey('condition.id')) condition = db.Column(db.Integer, db.ForeignKey('condition.id'))
covertype = db.Column(db.Integer, db.ForeignKey('covertype.id')) covertype = db.Column(db.Integer, db.ForeignKey('covertype.id'))
@@ -80,6 +87,7 @@ class BookSchema(ma.SQLAlchemyAutoSchema):
author = ma.Nested(AuthorSchema, many=True) author = ma.Nested(AuthorSchema, many=True)
publisher = ma.Nested(PublisherSchema, many=True) publisher = ma.Nested(PublisherSchema, many=True)
genre = ma.Nested(GenreSchema, many=True) genre = ma.Nested(GenreSchema, many=True)
loan = ma.Nested(LoanSchema, many=True)
parent_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True) parent_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
child_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True) child_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
class Meta: model = Book class Meta: model = Book
@@ -117,6 +125,14 @@ def books():
return render_template("books.html", books=books) return render_template("books.html", books=books)
@app.route("/books_for_loan/<id>", methods=["GET"])
def books_for_loan(id):
# books = db.engine.execute ( "select * from book_loan_link where loan_id = {}".format( id ) )
print( id )
books = Book.query.join(Book_Loan_Link).filter(Book_Loan_Link.loan_id==id).order_by(Book.id).all()
print( books )
return render_template("books_for_loan.html", books=books)
@app.route("/book/<id>", methods=["GET"]) @app.route("/book/<id>", methods=["GET"])
def book(id): def book(id):
book = Book.query.get(id) book = Book.query.get(id)
@@ -147,7 +163,6 @@ def book(id):
book_form.covertype.default = book.covertype book_form.covertype.default = book.covertype
book_form.owned.default = book.owned book_form.owned.default = book.owned
book_form.rating.default = book.rating book_form.rating.default = book.rating
print(book_form)
book_form.process() book_form.process()
return render_template("book.html", books=book_s, subs=sub_book, book_form=book_form ) return render_template("book.html", books=book_s, subs=sub_book, book_form=book_form )