using font awesome, datatables, bootstrap and fixed so class attributes are serialized via marshmallow and it all works with jinja2 now
This commit is contained in:
11
README
11
README
@@ -1,12 +1,15 @@
|
||||
## 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
|
||||
# flask-sqlachemy combines/wraps this to provide a db.* set of objects based on the 'app' that flask creates
|
||||
# marshmallow-sqlachemy provides a way to create a 'schema' of your class, then serialize an object to it
|
||||
|
||||
# --user sticks python libs in ~/.local/[bin|lib|share]
|
||||
##LEARN: supposedly could use virtualenv instead?
|
||||
# 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
|
||||
pip3 install --user flask sqlalchemy flask-sqlalchemy
|
||||
|
||||
##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
|
||||
|
||||
46
main.py
46
main.py
@@ -2,13 +2,14 @@ from flask import Flask
|
||||
from flask import render_template
|
||||
from flask import request
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
import logging
|
||||
from flask_marshmallow import Marshmallow
|
||||
|
||||
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)
|
||||
ma = Marshmallow(app)
|
||||
|
||||
####################################### CLASSES / DB model #######################################
|
||||
book_author_link = db.Table('book_author_link', db.Model.metadata,
|
||||
@@ -19,22 +20,51 @@ book_author_link = db.Table('book_author_link', db.Model.metadata,
|
||||
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)
|
||||
year_published = db.Column(db.Integer)
|
||||
rating = db.Column(db.String(20))
|
||||
condition = db.Column(db.String(20))
|
||||
owned = db.Column(db.String(20))
|
||||
covertype = db.Column(db.String(20))
|
||||
notes = db.Column(db.Text)
|
||||
blurb = db.Column(db.Text)
|
||||
created = db.Column(db.Date)
|
||||
modified = db.Column(db.Date)
|
||||
|
||||
author = db.relationship('Author', secondary=book_author_link)
|
||||
|
||||
def __repr__(self):
|
||||
return "<title: {}, id: {} author: {} author.firstnames {}>".format(self.title, self.id, self.author, self.id )
|
||||
# return "<title: {}, id: {} author: {} author.firstnames {}>".format(self.title, self.id, self.author, self.author.firstnames )
|
||||
return "<title: {}, id: {} author: {}>".format(self.title, self.id, self.author )
|
||||
|
||||
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 )
|
||||
book = db.relationship('Book', secondary=book_author_link )
|
||||
|
||||
def __repr__(self):
|
||||
return "<firstnames: {}, surname: {}>".format(self.firstnames, self.surname )
|
||||
# return "<firstnames: {}, surname: {}, book: {}>".format(self.firstnames, self.surname, self.book)
|
||||
return "<firstnames: {}, surname: {}, book: {}>".format(self.firstnames, self.surname, self.book)
|
||||
|
||||
|
||||
|
||||
### setup serializer schemas, to make returning books/authors easier
|
||||
class AuthorSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Author
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||
author = ma.Nested(AuthorSchema, many=True)
|
||||
class Meta:
|
||||
model = Book
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
book_schema = BookSchema()
|
||||
author_schema = AuthorSchema()
|
||||
|
||||
print( book_schema )
|
||||
|
||||
####################################### ROUTES #######################################
|
||||
@app.route("/books", methods=["GET"])
|
||||
@@ -47,8 +77,8 @@ def books():
|
||||
@app.route("/book/<id>", methods=["GET"])
|
||||
def book(id):
|
||||
book = Book.query.get(id)
|
||||
print( book )
|
||||
return render_template("books.html", books=book )
|
||||
book_s = book_schema.dump(book)
|
||||
return render_template("books.html", books=book_s )
|
||||
|
||||
@app.route("/authors", methods=["GET"])
|
||||
def author():
|
||||
|
||||
@@ -8,37 +8,50 @@
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.18.0/dist/bootstrap-table.min.css">
|
||||
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.22/css/dataTables.bootstrap4.min.css">
|
||||
<script src="https://kit.fontawesome.com/9b4c7cf470.js" crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body>
|
||||
<form method="POST" action="/">
|
||||
<input type="text" name="title">
|
||||
<input type="submit" value="Add">
|
||||
</form>
|
||||
|
||||
{% if books is not mapping %}
|
||||
<h3>All Books</h1>
|
||||
{% if books is iterable %}
|
||||
<table id="book_table" class="table table-striped table-sm" data-toolbar="#toolbar" data-search="true">
|
||||
<thead>
|
||||
<tr class="thead-light"><th>Title</th><th>Author</th></tr>
|
||||
<tr class="thead-light"><th>Title</th><th>Author</th><th>Publisher</th><th>Condition</th><th>Owned</th><th>Covertype</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
|
||||
{% for book in books %}
|
||||
<tr><td data-sort="{{book.id}}">{{book.title}}</td><td>{{ book.author[0]['surname'] }}, {{ book.author[0]['firstnames'] }}</td></tr>
|
||||
<tr>
|
||||
<td data-sort="{{book.id}}"><a href="/book/{{book.id}}">{{book.title}}</a></td>
|
||||
<td>{{ book.author[0]['surname'] }}, {{book.author[0]['firstnames']}}</td>
|
||||
<td>{{ book.publisher}}</td>
|
||||
<td align="center">
|
||||
{% if book.condition == "Good" %}
|
||||
<i class="fas fa-book" style="color:black"></i>
|
||||
{% elif book.condition == "Average" %}
|
||||
<i class="fas fa-book" style="color:orange"></i>
|
||||
{% else %}
|
||||
<i class="fas fa-book" style="color:red"></i>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{{ book.owned}}</td>
|
||||
<td>{{ book.covertype}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% else %}
|
||||
<h3>Book</h1>
|
||||
<p>{{books.title}}, {{ books.author[0]['surname'] }}, {{books.author[0]['firstnames']}} </p>
|
||||
{% endif %}
|
||||
|
||||
<!-- code to get bootstrap & bootstrap datatable to work -->
|
||||
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
|
||||
<script src="https://cdn.datatables.net/1.10.22/js/dataTables.bootstrap4.min.js"></script>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
$('#book_table').DataTable();
|
||||
$('#book_table').DataTable( { 'pageLength': 25 } );
|
||||
} );
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user