diff --git a/README b/README index 36f7953..5b35f83 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -## TODO: get all this inside a docker container and use compose to do the whole set (pg, flask, ?) +### TODO: get all this inside a docker container and use compose to do the whole set (pg, flask, ?) # flask -> python web server # sqlalchemy -> provides db-agnostic python objects of db content (and more) # flask-sqlachemy combines/wraps this to provide a db.* set of objects based on the 'app' that flask creates @@ -7,9 +7,15 @@ # install needed binaries (maybe I could have done this instead of pip below too -- when I docker this shit, sort it out?) sudo apt install python3-psycopg2 libpq-dev -##LEARN: supposedly could use virtualenv instead of pip3 install --user? +### LEARN: supposedly could use virtualenv instead of pip3 install --user? # --user sticks python libs in ~/.local/[bin|lib|share] pip3 install --user flask sqlalchemy flask-sqlalchemy flask-marshmallow SQLAlchemy-serializer # run the web server by: python3 main.py + + +### TODO: +- book subbook link will be next real challenge +- then next challenge will be to make single book page an edit / save +- then its just finish this off :) diff --git a/main.py b/main.py index 665185c..5205f85 100644 --- a/main.py +++ b/main.py @@ -22,6 +22,15 @@ book_publisher_link = db.Table('book_publisher_link', db.Model.metadata, 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 "".format(self.book_id, self.sub_book_id, self.sub_book_num ) + class Book(db.Model): id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) title = db.Column(db.String(100), unique=True, nullable=False) @@ -37,9 +46,14 @@ class Book(db.Model): author = db.relationship('Author', secondary=book_author_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): - return "".format(self.title, self.id ) + return "".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 ) class Author(db.Model): id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) @@ -71,28 +85,43 @@ class PublisherSchema(ma.SQLAlchemyAutoSchema): include_relationships = True load_instance = True -class BookSchema(ma.SQLAlchemyAutoSchema): +class Book_Sub_Book_Link(ma.SQLAlchemyAutoSchema): class Meta: - model = Book + model = Book_Sub_Book_Link include_relationships = True load_instance = True +#class BookSchema(ma.SQLAlchemyAutoSchema): +# class Meta: +# model = Book +# include_relationships = True +# load_instance = True + author_schema = AuthorSchema() publisher_schema = PublisherSchema() -book_schema = BookSchema() +book_sub_book_link_schema = Book_Sub_Book_Link() +#book_schema = BookSchema() ####################################### ROUTES ####################################### @app.route("/books", methods=["GET"]) def books(): if request.form: print(request.form) - books = Book.query.all() +# 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.filter( ~ Book.id.in_([0, 1, 2, 3, 4, 5, 6]) ).all() return render_template("books.html", books=books) @app.route("/book/", methods=["GET"]) def book(id): book = Book.query.get(id) book_s = book_schema.dump(book) + subs = db.engine.execute ( "select book_id, sub_book_id, sub_book_num from book_sub_book_link where book_id = " + id ) + sub_book=[] + for row in subs: + sub_book.append( { 'sub_book_id': row.sub_book_id, 'sub_book_num': row.sub_book_num } ) + book_s['sub_book'] = sub_book + return render_template("books.html", books=book_s ) @app.route("/authors", methods=["GET"])