commit a51db7c9e4e997b1bbdc5f5af06d29e5df18f1c7 Author: Damien De Paoli Date: Tue Nov 3 18:29:41 2020 +1100 initial commit of book library in python with sqlalchemy (as an ORM), flask (for the web server) and jinja2 (as the web template front-end bit) diff --git a/README b/README new file mode 100644 index 0000000..61689f7 --- /dev/null +++ b/README @@ -0,0 +1,12 @@ +## TODO: get all this inside a docker container and use compose to do the whole set (pg, flask, ?) +# flash -> python web server +# sqlalchemy -> provides db-agnostic python objects of db content (and more) +## LEARN: not totally sure what flask-sqlachemy provides + +# --user sticks python libs in ~/.local/[bin|lib|share] +##LEARN: supposedly could use virtualenv instead? +sudo apt install python3-psycopg2 libpq-dev +pip3 install --user flask sqlalchemy flask-sqlalchemy + +# run the web server by: +python3 book.py diff --git a/__pycache__/author.cpython-38.pyc b/__pycache__/author.cpython-38.pyc new file mode 100644 index 0000000..ae55ffd Binary files /dev/null and b/__pycache__/author.cpython-38.pyc differ diff --git a/__pycache__/authorroutes.cpython-38.pyc b/__pycache__/authorroutes.cpython-38.pyc new file mode 100644 index 0000000..0523e9d Binary files /dev/null and b/__pycache__/authorroutes.cpython-38.pyc differ diff --git a/__pycache__/book.cpython-38.pyc b/__pycache__/book.cpython-38.pyc new file mode 100644 index 0000000..6bdc13a Binary files /dev/null and b/__pycache__/book.cpython-38.pyc differ diff --git a/__pycache__/db.cpython-38.pyc b/__pycache__/db.cpython-38.pyc new file mode 100644 index 0000000..b0cc632 Binary files /dev/null and b/__pycache__/db.cpython-38.pyc differ diff --git a/__pycache__/db_model.cpython-38.pyc b/__pycache__/db_model.cpython-38.pyc new file mode 100644 index 0000000..6d3fe00 Binary files /dev/null and b/__pycache__/db_model.cpython-38.pyc differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..3ec39aa --- /dev/null +++ b/main.py @@ -0,0 +1,60 @@ +from flask import Flask +from flask import render_template +from flask import request +from flask_sqlalchemy import SQLAlchemy +import logging + +DB_URL = 'postgresql+psycopg2://ddp:NWNlfa01@127.0.0.1:5432/library' +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +db = SQLAlchemy(app) + +####################################### CLASSES / DB model ####################################### +book_author_link = db.Table('book_author_link', db.Model.metadata, + db.Column('book_id', db.Integer, db.ForeignKey('book.id')), + db.Column('author_id', db.Integer, db.ForeignKey('author.id')) +) + +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) + author = db.relationship('Author', secondary=book_author_link) + + def __repr__(self): + return "".format(self.title, self.id, self.author, self.id ) +# return "".format(self.title, self.id, self.author, self.author.firstnames ) + +class Author(db.Model): + id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) + firstnames = db.Column(db.String(50), 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): + return "".format(self.firstnames, self.surname ) +# return "".format(self.firstnames, self.surname, self.book) + +####################################### ROUTES ####################################### +@app.route("/books", methods=["GET"]) +def books(): + if request.form: + print(request.form) + books = Book.query.all() + return render_template("books.html", books=books) + +@app.route("/book/", methods=["GET"]) +def book(id): + book = Book.query.get(id) + print( book ) + return render_template("books.html", books=book ) + +@app.route("/authors", methods=["GET"]) +def author(): + authors = Author.query.all() + return render_template("author.html", authors=authors) + +if __name__ == "__main__": + app.run(host="0.0.0.0", debug=True) + diff --git a/templates/author.html b/templates/author.html new file mode 100644 index 0000000..0dbdf16 --- /dev/null +++ b/templates/author.html @@ -0,0 +1,9 @@ + + + +

authors

+ {% for author in authors %} +

{{author.surname}}, {{author.firstnames}}

+ {% endfor %} + + diff --git a/templates/books.html b/templates/books.html new file mode 100644 index 0000000..6f73b31 --- /dev/null +++ b/templates/books.html @@ -0,0 +1,45 @@ + + + + + + + + + + + + +
+ + +
+ +

All Books

+ {% if books is iterable %} + + + + + + + {% for book in books %} + + {% endfor %} + +
TitleAuthor
{{book.title}}{{ book.author[0]['surname'] }}, {{ book.author[0]['firstnames'] }}
+ {% else %} +

{{books.title}}, {{ books.author[0]['surname'] }}, {{books.author[0]['firstnames']}}

+ {% endif %} + + + + + + + +