removed all refs to genre_lst, made genre table confirm to id, name, added classes/routes for genre*, tweaked book format to accommodate multiple authors, etc. still 1-line, but with 12 / num of <authors, etc.> col wide form-controls

This commit is contained in:
2020-11-15 14:16:58 +11:00
parent 7334e4b622
commit e39a4da6a2
6 changed files with 119 additions and 20 deletions

22
main.py
View File

@@ -17,6 +17,7 @@ Bootstrap(app)
from author import Author, AuthorForm, AuthorSchema
from publisher import Publisher, PublisherForm, PublisherSchema
from genre import Genre, GenreForm, GenreSchema
####################################### CLASSES / DB model #######################################
book_author_link = db.Table('book_author_link', db.Model.metadata,
@@ -31,7 +32,7 @@ book_publisher_link = db.Table('book_publisher_link', db.Model.metadata,
book_genre_link = db.Table('book_genre_link', db.Model.metadata,
db.Column('book_id', db.Integer, db.ForeignKey('book.id')),
db.Column('genre_id', db.Integer, db.ForeignKey('genre_lst.id'))
db.Column('genre_id', db.Integer, db.ForeignKey('genre.id'))
)
class Book_Sub_Book_Link(db.Model):
@@ -58,31 +59,20 @@ class Book(db.Model):
author = db.relationship('Author', secondary=book_author_link)
publisher = db.relationship('Publisher', secondary=book_publisher_link)
genre = db.relationship('Genre_Lst', secondary=book_genre_link )
genre = db.relationship('Genre', secondary=book_genre_link )
parent_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id" )
child_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id" )
def __repr__(self):
return "<id: {}, author: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}>".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 )
class Genre_Lst(db.Model):
__tablename__ = "genre_lst"
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
genre = db.Column(db.String(20), unique=False, nullable=False)
def __repr__(self):
return "<id: {}, genre: {}>".format(self.id, self.genre)
class Genre_LstSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Genre_Lst
class Book_Sub_Book_LinkSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Book_Sub_Book_Link
class BookSchema(ma.SQLAlchemyAutoSchema):
author = ma.Nested(AuthorSchema, many=True)
publisher = ma.Nested(PublisherSchema, many=True)
genre = ma.Nested(Genre_LstSchema, many=True)
genre = ma.Nested(GenreSchema, 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
@@ -122,9 +112,9 @@ def book(id):
for row in subs:
# get genres for sub book and add by hand first
tmp_g = []
genres = db.engine.execute ( "select genre_lst.id, genre_lst.genre from genre_lst, book_genre_link bgl where genre_lst.id = bgl.genre_id and bgl.book_id = {}".format( row.sub_book_id ) )
genres = db.engine.execute ( "select genre.id, genre.name from genre, book_genre_link bgl where genre.id = bgl.genre_id and bgl.book_id = {}".format( row.sub_book_id ) )
for genre in genres:
tmp_g.append( { 'id': genre.id, 'genre': genre.genre } )
tmp_g.append( { 'id': genre.id, 'name': genre.name } )
sub_book.append( { 'sub_book_id': row.sub_book_id, 'sub_book_num': row.sub_book_num, 'title' : row.title, 'rating': row.rating, 'year_published' : row.year_published, 'notes' : row.notes, 'author_id' : row.author_id, 'author' : row.author, 'genres' : tmp_g } )