moved to using a Get*ById() in condition, covertype, owned files and exposing them in jinja and using them in books.html to fix-up the fact those columns were now raw forein keys (ids)

This commit is contained in:
2020-11-22 00:23:18 +11:00
parent 1edf5082c7
commit 9466e61db9
5 changed files with 31 additions and 10 deletions

View File

@@ -65,3 +65,7 @@ def condition(id):
condition_form = ConditionForm(request.values, obj=condition) condition_form = ConditionForm(request.values, obj=condition)
message="" message=""
return render_template("edit_id_name.html", condition=condition, alert=alert, message=message, form=condition_form, page_title='Edit Condition') return render_template("edit_id_name.html", condition=condition, alert=alert, message=message, form=condition_form, page_title='Edit Condition')
def GetConditionById(id):
return Condition.query.get(id).name

View File

@@ -39,6 +39,11 @@ def covertypes():
covertypes = Covertype.query.order_by('id').all() covertypes = Covertype.query.order_by('id').all()
return render_template( "show_id_name.html", objects=covertypes, page_title='Show All Covertypes', url_base='covertype') return render_template( "show_id_name.html", objects=covertypes, page_title='Show All Covertypes', url_base='covertype')
def GetCovertypes():
covertypes = Covertype.query.order_by('name').all()
cs = CovertypeSchema(many=True)
return cs.dump( covertypes )
################################################################################ ################################################################################
# /covertype/<id> -> GET/POST(save or delete) -> shows/edits/delets a single # /covertype/<id> -> GET/POST(save or delete) -> shows/edits/delets a single
# covertype # covertype
@@ -65,3 +70,6 @@ def covertype(id):
covertype_form = CovertypeForm(request.values, obj=covertype) covertype_form = CovertypeForm(request.values, obj=covertype)
message="" message=""
return render_template("edit_id_name.html", covertype=covertype, alert=alert, message=message, form=covertype_form, page_title='Edit Covertype') return render_template("edit_id_name.html", covertype=covertype, alert=alert, message=message, form=covertype_form, page_title='Edit Covertype')
def GetCovertypeById(id):
return Covertype.query.get(id).name

15
main.py
View File

@@ -18,12 +18,17 @@ Bootstrap(app)
from author import Author, AuthorForm, AuthorSchema from author import Author, AuthorForm, AuthorSchema
from publisher import Publisher, PublisherForm, PublisherSchema, GetPublishers from publisher import Publisher, PublisherForm, PublisherSchema, GetPublishers
from genre import Genre, GenreForm, GenreSchema, GetGenres from genre import Genre, GenreForm, GenreSchema, GetGenres
from condition import Condition, ConditionForm, ConditionSchema from condition import Condition, ConditionForm, ConditionSchema, GetConditionById
from covertype import Covertype, CovertypeForm, CovertypeSchema from covertype import Covertype, CovertypeForm, CovertypeSchema, GetCovertypeById
from owned import Owned, OwnedForm, OwnedSchema from owned import Owned, OwnedForm, OwnedSchema, GetOwnedById
from rating import Rating, RatingForm, RatingSchema from rating import Rating, RatingForm, RatingSchema
from loan import Loan, LoanForm, LoanSchema from loan import Loan, LoanForm, LoanSchema
# allow jinja2 to call this python function
app.jinja_env.globals['GetCovertypeById'] = GetCovertypeById
app.jinja_env.globals['GetOwnedById'] = GetOwnedById
app.jinja_env.globals['GetConditionById'] = GetConditionById
####################################### CLASSES / DB model ####################################### ####################################### CLASSES / DB model #######################################
book_author_link = db.Table('book_author_link', db.Model.metadata, book_author_link = db.Table('book_author_link', db.Model.metadata,
db.Column('book_id', db.Integer, db.ForeignKey('book.id')), db.Column('book_id', db.Integer, db.ForeignKey('book.id')),
@@ -127,7 +132,7 @@ def books():
books[index].parent_id = row.book_id books[index].parent_id = row.book_id
books[index].sub_book_num = row.sub_book_num books[index].sub_book_num = row.sub_book_num
return render_template("books.html", books=books) return render_template("books.html", books=books )
@app.route("/books_for_loan/<id>", methods=["GET"]) @app.route("/books_for_loan/<id>", methods=["GET"])
def books_for_loan(id): def books_for_loan(id):
@@ -170,7 +175,7 @@ def book(id):
book_form.process() book_form.process()
genre_list = GetGenres() genre_list = GetGenres()
publisher_list = GetPublishers() publisher_list = GetPublishers()
return render_template("book.html", books=book_s, subs=sub_book, book_form=book_form, genre_list=genre_list, publisher_list=publisher_list ) return render_template("book.html", books=book_s, subs=sub_book, book_form=book_form, genre_list=genre_list, publisher_list=publisher_list, covertype_list=covertypes )
@app.route("/", methods=["GET"]) @app.route("/", methods=["GET"])
def main_page(): def main_page():

View File

@@ -65,3 +65,6 @@ def owned(id):
owned_form = OwnedForm(request.values, obj=owned) owned_form = OwnedForm(request.values, obj=owned)
message="" message=""
return render_template("edit_id_name.html", owned=owned, alert=alert, message=message, form=owned_form, page_title='Edit Owned Type' ) return render_template("edit_id_name.html", owned=owned, alert=alert, message=message, form=owned_form, page_title='Edit Owned Type' )
def GetOwnedById(id):
return Owned.query.get(id).name

View File

@@ -1,6 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block main_content %} {% block main_content %}
<h3>All Books</h1> <h3>All Books</h1>
<table id="book_table" class="table table-striped table-sm" data-toolbar="#toolbar" data-search="true"> <table id="book_table" class="table table-striped table-sm" data-toolbar="#toolbar" data-search="true">
<thead> <thead>
<tr class="thead-light"><th>Title</th><th>Author</th><th>Publisher</th><th>Condition</th><th>Owned</th><th>Covertype</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>
@@ -16,16 +16,17 @@
<td>{{ book.author[0]['surname'] }}, {{book.author[0]['firstnames']}}</td> <td>{{ book.author[0]['surname'] }}, {{book.author[0]['firstnames']}}</td>
<td>{{ book.publisher[0]['name']}}</td> <td>{{ book.publisher[0]['name']}}</td>
<td align="center"> <td align="center">
{% if book.condition == "Good" %} {% set cond = GetConditionById(book.condition) %}
{% if cond == "Good" %}
<i class="fas fa-book" style="color:black"></i> <i class="fas fa-book" style="color:black"></i>
{% elif book.condition == "Average" %} {% elif cond == "Average" %}
<i class="fas fa-book" style="color:orange"></i> <i class="fas fa-book" style="color:orange"></i>
{% else %} {% else %}
<i class="fas fa-book" style="color:red"></i> <i class="fas fa-book" style="color:red"></i>
{% endif %} {% endif %}
</td> </td>
<td>{{ book.owned}}</td> <td>{{ GetOwnedById(book.owned)}}</td>
<td>{{ book.covertype}}</td> <td>{{ GetCovertypeById(book.covertype) }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>