okay removed sub_book as a formal db.relationship, its too confusing, using raw sql for now. Still need to ensure ordering of sub_books based on book_id, sub_book_num AND need to pass enough sub_book fields that can vary so that jinja2 can show all subs of a book when editing any of them
This commit is contained in:
45
main.py
45
main.py
@@ -22,15 +22,6 @@ book_publisher_link = db.Table('book_publisher_link', db.Model.metadata,
|
|||||||
db.Column('publisher_id', db.Integer, db.ForeignKey('publisher.id'))
|
db.Column('publisher_id', db.Integer, db.ForeignKey('publisher.id'))
|
||||||
)
|
)
|
||||||
|
|
||||||
class Book_Sub_Book_Link(db.Model):
|
|
||||||
__tablename__ = 'book_sub_book_link'
|
|
||||||
book_id = db.Column( db.Integer, db.ForeignKey('book.id'), primary_key=True)
|
|
||||||
sub_book_id = db.Column( db.Integer, db.ForeignKey('book.id'), primary_key=True)
|
|
||||||
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 )
|
|
||||||
|
|
||||||
class Book(db.Model):
|
class Book(db.Model):
|
||||||
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
||||||
title = db.Column(db.String(100), unique=True, nullable=False)
|
title = db.Column(db.String(100), unique=True, nullable=False)
|
||||||
@@ -46,11 +37,6 @@ 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)
|
||||||
# sub_book = db.relationship('Book', secondary="book_sub_book_link",
|
|
||||||
# primaryjoin="Book.id == Book_Sub_Book_Link.book_id",
|
|
||||||
# secondaryjoin="Book.id == Book_Sub_Book_Link.sub_book_id",
|
|
||||||
# backref="parent_book",
|
|
||||||
# )
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<id: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}>".format(self.title, self.id, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher )
|
return "<id: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}>".format(self.title, self.id, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher )
|
||||||
@@ -85,42 +71,47 @@ class PublisherSchema(ma.SQLAlchemyAutoSchema):
|
|||||||
include_relationships = True
|
include_relationships = True
|
||||||
load_instance = True
|
load_instance = True
|
||||||
|
|
||||||
class Book_Sub_Book_Link(ma.SQLAlchemyAutoSchema):
|
class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||||
|
author = ma.Nested(AuthorSchema, many=True)
|
||||||
|
publisher = ma.Nested(PublisherSchema, many=True)
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Book_Sub_Book_Link
|
model = Book
|
||||||
include_relationships = True
|
include_relationships = True
|
||||||
load_instance = True
|
load_instance = True
|
||||||
|
|
||||||
#class BookSchema(ma.SQLAlchemyAutoSchema):
|
|
||||||
# class Meta:
|
|
||||||
# model = Book
|
|
||||||
# include_relationships = True
|
|
||||||
# load_instance = True
|
|
||||||
|
|
||||||
author_schema = AuthorSchema()
|
author_schema = AuthorSchema()
|
||||||
publisher_schema = PublisherSchema()
|
publisher_schema = PublisherSchema()
|
||||||
book_sub_book_link_schema = Book_Sub_Book_Link()
|
book_schema = BookSchema()
|
||||||
#book_schema = BookSchema()
|
|
||||||
|
|
||||||
####################################### ROUTES #######################################
|
####################################### ROUTES #######################################
|
||||||
@app.route("/books", methods=["GET"])
|
@app.route("/books", methods=["GET"])
|
||||||
def books():
|
def books():
|
||||||
if request.form:
|
if request.form:
|
||||||
print(request.form)
|
print(request.form)
|
||||||
# books = Book.query.all()
|
|
||||||
#### quick hack - lets just return all the books not in the first 6, but should be those that are not in list of book_id's from book_sub_book_link
|
books = Book.query.all()
|
||||||
books = Book.query.filter( ~ Book.id.in_([0, 1, 2, 3, 4, 5, 6]) ).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)
|
return render_template("books.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)
|
||||||
book_s = book_schema.dump(book)
|
book_s = book_schema.dump(book)
|
||||||
|
|
||||||
|
# force sub books for jinja2 to be able to use
|
||||||
subs = db.engine.execute ( "select book_id, sub_book_id, sub_book_num from book_sub_book_link where book_id = " + id )
|
subs = db.engine.execute ( "select book_id, sub_book_id, sub_book_num from book_sub_book_link where book_id = " + id )
|
||||||
sub_book=[]
|
sub_book=[]
|
||||||
for row in subs:
|
for row in subs:
|
||||||
sub_book.append( { 'sub_book_id': row.sub_book_id, 'sub_book_num': row.sub_book_num } )
|
sub_book.append( { 'sub_book_id': row.sub_book_id, 'sub_book_num': row.sub_book_num } )
|
||||||
book_s['sub_book'] = sub_book
|
book_s['sub_book'] = sub_book
|
||||||
|
print( book_s )
|
||||||
|
|
||||||
return render_template("books.html", books=book_s )
|
return render_template("books.html", books=book_s )
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,11 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
{% for book in books %}
|
{% for book in books %}
|
||||||
<tr>
|
<tr>
|
||||||
<td data-sort="{{book.id}}"><a href="/book/{{book.id}}">{{book.title}}</a></td>
|
{% if book.sub_book_num is defined %}
|
||||||
|
<td data-sort="{{book.id}}"><a href="/book/{{book.id}}"> {{book.title}}</a></td>
|
||||||
|
{% else %}
|
||||||
|
<td data-sort="{{book.id}}"><a href="/book/{{book.id}}">{{book.title}}</a></td>
|
||||||
|
{% endif %}
|
||||||
<td>{{ book.author[0]['surname'] }}, {{book.author[0]['firstnames']}}</td>
|
<td>{{ book.author[0]['surname'] }}, {{book.author[0]['firstnames']}}</td>
|
||||||
<td>{{ book.publisher[0]['name']}}</td>
|
<td>{{ book.publisher[0]['name']}}</td>
|
||||||
<td align="center">
|
<td align="center">
|
||||||
@@ -42,6 +46,9 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
<h3>Book</h1>
|
<h3>Book</h1>
|
||||||
<p>{{books.title}}, {{ books.author[0]['surname'] }}, {{books.author[0]['firstnames']}} </p>
|
<p>{{books.title}}, {{ books.author[0]['surname'] }}, {{books.author[0]['firstnames']}} </p>
|
||||||
|
{% if books.sub_book is defined %}
|
||||||
|
<p>sub_book is defined: {{books.sub_book}}</p>
|
||||||
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<!-- code to get bootstrap & bootstrap datatable to work -->
|
<!-- code to get bootstrap & bootstrap datatable to work -->
|
||||||
|
|||||||
Reference in New Issue
Block a user