From 9466e61db9c092bda97b78b80001bf538e7ebd7e Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sun, 22 Nov 2020 00:23:18 +1100 Subject: [PATCH] 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) --- condition.py | 4 ++++ covertype.py | 8 ++++++++ main.py | 15 ++++++++++----- owned.py | 3 +++ templates/books.html | 11 ++++++----- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/condition.py b/condition.py index 5db57e3..7f40310 100644 --- a/condition.py +++ b/condition.py @@ -65,3 +65,7 @@ def condition(id): condition_form = ConditionForm(request.values, obj=condition) message="" 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 + diff --git a/covertype.py b/covertype.py index 2829028..0a893b6 100644 --- a/covertype.py +++ b/covertype.py @@ -39,6 +39,11 @@ def covertypes(): covertypes = Covertype.query.order_by('id').all() 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/ -> GET/POST(save or delete) -> shows/edits/delets a single # covertype @@ -65,3 +70,6 @@ def covertype(id): covertype_form = CovertypeForm(request.values, obj=covertype) message="" 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 diff --git a/main.py b/main.py index d23eb1e..0c13e41 100644 --- a/main.py +++ b/main.py @@ -18,12 +18,17 @@ Bootstrap(app) from author import Author, AuthorForm, AuthorSchema from publisher import Publisher, PublisherForm, PublisherSchema, GetPublishers from genre import Genre, GenreForm, GenreSchema, GetGenres -from condition import Condition, ConditionForm, ConditionSchema -from covertype import Covertype, CovertypeForm, CovertypeSchema -from owned import Owned, OwnedForm, OwnedSchema +from condition import Condition, ConditionForm, ConditionSchema, GetConditionById +from covertype import Covertype, CovertypeForm, CovertypeSchema, GetCovertypeById +from owned import Owned, OwnedForm, OwnedSchema, GetOwnedById from rating import Rating, RatingForm, RatingSchema 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 ####################################### book_author_link = db.Table('book_author_link', db.Model.metadata, 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].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/", methods=["GET"]) def books_for_loan(id): @@ -170,7 +175,7 @@ def book(id): book_form.process() genre_list = GetGenres() 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"]) def main_page(): diff --git a/owned.py b/owned.py index bace3c3..2bea93c 100644 --- a/owned.py +++ b/owned.py @@ -65,3 +65,6 @@ def owned(id): owned_form = OwnedForm(request.values, obj=owned) message="" 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 diff --git a/templates/books.html b/templates/books.html index ec6b829..43b081e 100644 --- a/templates/books.html +++ b/templates/books.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% block main_content %} -

All Books

+

All Books

@@ -16,16 +16,17 @@ - - + + {% endfor %}
TitleAuthorPublisherConditionOwnedCovertype
{{ book.author[0]['surname'] }}, {{book.author[0]['firstnames']}} {{ book.publisher[0]['name']}} - {% if book.condition == "Good" %} + {% set cond = GetConditionById(book.condition) %} + {% if cond == "Good" %} - {% elif book.condition == "Average" %} + {% elif cond == "Average" %} {% else %} {% endif %} {{ book.owned}}{{ book.covertype}}{{ GetOwnedById(book.owned)}}{{ GetCovertypeById(book.covertype) }}