Keeping flask-marshmallow as its too hard to do my own objects, due to SqlAlchemy using its own list that is not iterable, etc. Added Genre, ALSO, beware table names with an underscore are messed with, and should be explictly defined - found out this the hard way with genre_lst
This commit is contained in:
37
main.py
37
main.py
@@ -22,6 +22,11 @@ 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'))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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'))
|
||||||
|
)
|
||||||
|
|
||||||
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)
|
||||||
@@ -37,26 +42,33 @@ 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_Lst', secondary=book_genre_link )
|
||||||
|
|
||||||
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: {}, 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 Author(db.Model):
|
class Author(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)
|
||||||
firstnames = db.Column(db.String(50), unique=False, nullable=False)
|
firstnames = db.Column(db.String(50), unique=False, nullable=False)
|
||||||
surname = db.Column(db.String(30), unique=False, nullable=False)
|
surname = db.Column(db.String(30), unique=False, nullable=False)
|
||||||
|
|
||||||
book = db.relationship('Book', secondary=book_author_link )
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<firstnames: {}, surname: {}, id: {}>".format(self.firstnames, self.surname, self.id)
|
return "<id: {}, firstnames: {}, surname: {}>".format(self.id,self.firstnames, self.surname)
|
||||||
|
|
||||||
class Publisher(db.Model):
|
class Publisher(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)
|
||||||
name = db.Column(db.String(50), unique=False, nullable=False)
|
name = db.Column(db.String(50), unique=False, nullable=False)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<name: {}, id: {}>".format(self.name, self.id)
|
return "<id: {}, name: {}>".format(self.id, self.name)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
### setup serializer schemas, to make returning books/authors easier
|
### setup serializer schemas, to make returning books/authors easier
|
||||||
class AuthorSchema(ma.SQLAlchemyAutoSchema):
|
class AuthorSchema(ma.SQLAlchemyAutoSchema):
|
||||||
@@ -71,16 +83,24 @@ class PublisherSchema(ma.SQLAlchemyAutoSchema):
|
|||||||
include_relationships = True
|
include_relationships = True
|
||||||
load_instance = True
|
load_instance = True
|
||||||
|
|
||||||
|
class Genre_LstSchema(ma.SQLAlchemyAutoSchema):
|
||||||
|
class Meta:
|
||||||
|
model = Genre_Lst
|
||||||
|
include_relationships = True
|
||||||
|
load_instance = True
|
||||||
|
|
||||||
class BookSchema(ma.SQLAlchemyAutoSchema):
|
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)
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Book
|
model = Book
|
||||||
include_relationships = True
|
include_relationships = True
|
||||||
load_instance = True
|
load_instance = True
|
||||||
|
|
||||||
author_schema = AuthorSchema()
|
### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?)
|
||||||
|
author_schema = AuthorSchema(many=True)
|
||||||
publisher_schema = PublisherSchema()
|
publisher_schema = PublisherSchema()
|
||||||
|
genre_schema = Genre_LstSchema(many=True)
|
||||||
book_schema = BookSchema()
|
book_schema = BookSchema()
|
||||||
|
|
||||||
####################################### ROUTES #######################################
|
####################################### ROUTES #######################################
|
||||||
@@ -118,9 +138,8 @@ def book(id):
|
|||||||
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 } )
|
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 } )
|
||||||
|
|
||||||
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, subs=sub_book )
|
||||||
|
|
||||||
@app.route("/authors", methods=["GET"])
|
@app.route("/authors", methods=["GET"])
|
||||||
def author():
|
def author():
|
||||||
|
|||||||
@@ -73,6 +73,7 @@
|
|||||||
</form>
|
</form>
|
||||||
</div class="row">
|
</div class="row">
|
||||||
</div class="container">
|
</div class="container">
|
||||||
|
{{books.genre}}
|
||||||
{% if books.sub_book is defined %}
|
{% if books.sub_book is defined %}
|
||||||
<p>sub_book is defined: {{books.sub_book}}</p>
|
<p>sub_book is defined: {{books.sub_book}}</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
Reference in New Issue
Block a user