diff --git a/main.py b/main.py index 005010d..a999fba 100644 --- a/main.py +++ b/main.py @@ -27,6 +27,15 @@ book_genre_link = db.Table('book_genre_link', db.Model.metadata, db.Column('genre_id', db.Integer, db.ForeignKey('genre_lst.id')) ) +class Book_Sub_Book_Link(db.Model): + __tablename__ = "book_sub_book_link" + book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True) + sub_book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True) + sub_book_num = db.Column(db.Integer) + + def __repr__(self): + return "".format(self.id, self.author, self.title, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher ) @@ -89,10 +99,17 @@ class Genre_LstSchema(ma.SQLAlchemyAutoSchema): include_relationships = True load_instance = True +class Book_Sub_Book_LinkSchema(ma.SQLAlchemyAutoSchema): + class Meta: + model = Book_Sub_Book_Link + include_relationships = True + load_instance = True + class BookSchema(ma.SQLAlchemyAutoSchema): author = ma.Nested(AuthorSchema, many=True) publisher = ma.Nested(PublisherSchema, many=True) genre = ma.Nested(Genre_LstSchema, many=True) + subs = ma.Nested(Book_Sub_Book_LinkSchema, many=True) class Meta: model = Book include_relationships = True @@ -107,7 +124,10 @@ def books(): if request.form: print(request.form) - books = Book.query.all() +### 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" ) @@ -137,6 +157,9 @@ def book(id): book_s['sub_book'] = sub_book + print( book.genre ) + print( book.subs ) + return render_template("books.html", books=book_s, subs=sub_book ) @app.route("/authors", methods=["GET"]) diff --git a/templates/books.html b/templates/books.html index 9af902f..e8b27c3 100644 --- a/templates/books.html +++ b/templates/books.html @@ -22,7 +22,7 @@ {% for book in books %} {% if book.sub_book_num is defined %} -     {{book.title}} +     {{book.title}} {% else %} {{book.title}} {% endif %} @@ -83,6 +83,7 @@ {% if books.sub_book is defined %}

sub_book is defined: {{books.sub_book}}

{% endif %} + {{ books.subs }} {% endif %}