From 9dee77b0578a8c4ac5cafd447c3338ad94b6a36c Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sat, 21 Nov 2020 14:32:38 +1100 Subject: [PATCH] removed clever code around publiseher/author, etc. it was just too cryptic, and it does not need to be so generic --- main.py | 18 +++++--- templates/book.html | 101 ++++++++++++++++++-------------------------- 2 files changed, 52 insertions(+), 67 deletions(-) diff --git a/main.py b/main.py index 954167a..41a8ded 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ from flask import Flask, render_template, request from flask_sqlalchemy import SQLAlchemy from flask_marshmallow import Marshmallow from flask_bootstrap import Bootstrap -from wtforms import SubmitField, StringField, HiddenField, SelectField, validators +from wtforms import SubmitField, StringField, HiddenField, SelectField, IntegerField, TextAreaField, validators from flask_wtf import FlaskForm app = Flask(__name__) @@ -96,14 +96,18 @@ class BookSchema(ma.SQLAlchemyAutoSchema): # To be completed # class BookForm(FlaskForm): - # I think I'll have to skip setting default on create, and using jquery to - # change it when I create the from? (or maybe I could use a default=set_me - # in the line below, then when I set create set_me = book.condition before - # bf=BookForm() - condition = SelectField( 'condition', choices=[(c.id, c.name) for c in Condition.query.order_by('id')] ) - covertype = SelectField( 'covertype', choices=[(c.id, c.name) for c in Covertype.query.order_by('id')] ) + id = HiddenField() + title = StringField('Title:', [validators.DataRequired()]) + # author built by hand + # publiser built by hand + # genre built by hand owned = SelectField( 'owned', choices=[(c.id, c.name) for c in Owned.query.order_by('id')] ) + covertype = SelectField( 'covertype', choices=[(c.id, c.name) for c in Covertype.query.order_by('id')] ) + condition = SelectField( 'condition', choices=[(c.id, c.name) for c in Condition.query.order_by('id')] ) + year_published = IntegerField('Year Published:', [validators.NumberRange(min=1900, max=2100)] ) rating = SelectField( 'rating', choices=[(c.id, c.name) for c in Rating.query.order_by('id')] ) + notes = TextAreaField('Notes:') + blurb = TextAreaField('Blurb:') ### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?) diff --git a/templates/book.html b/templates/book.html index dfa89c7..95793bb 100644 --- a/templates/book.html +++ b/templates/book.html @@ -1,77 +1,58 @@ {% extends "base.html" %} {% block main_content %} +

View/Edit Book

{% set keys = [ 'title', 'author', 'publisher', 'genre', 'owned', 'covertype', 'condition', 'year_published', 'rating', 'notes', 'blurb' ] %} -
-
-
- {% for key in keys %} +
+ {% for key in keys %} +
+ {% if key == "genre" %} -
-
{% for genre in genre_list %} {% set gname=genre.name %}
- - -
+ + +
{% endfor %}
-
-{% elif key == "condition" or key == "covertype" or key == "owned" or key == "rating" %} -
- -
- {{book_form[key](class="form-control")}} -
-
-{% else %} -
- -
- {% if books[key] is iterable and books[key] is not string %} + {% elif key == "publisher" %} +
- {% set cnt = namespace(idx=0, val=0) %} - {% for objects in books[key] %} - {% set cnt.val = 0 %} - {% set str = namespace(val="") %} - {% for attr in objects %} - {% if attr != "id" %} - {% if cnt.val > 0 %} - {% set str.val=str.val+", "+objects[attr] %} - {% else %} - {% set str.val=str.val+objects[attr] %} - {% endif %} - {% set cnt.val = cnt.val + 1 %} - {% endif %} - {% endfor %} - - {{str.val}} + + {{books[key][0].name}} - {% set cnt.idx = cnt.idx+1 %} - {% endfor %}
- {% else %} - {% if key == "notes" or key == "blurb" %} - - {% else %} - - {% endif %} - {% endif %} -
-
-{% endif %} - {% endfor %} - -
-
+
+ {% elif key == "author" %} +
+
+ {% set cnt = namespace(idx=0) %} + {% for objects in books[key] %} + + + {{books.author[cnt.idx].surname + ", " + books.author[cnt.idx].firstnames}} + + + {% set cnt.idx = cnt.idx+1 %} + {% endfor %} +
+
+ {% else %} +
+ {{book_form[key](class="form-control", value=books[key], rows="5" )}} +
+ {% endif %} +
+ {% endfor %} +
{% if books.sub_book|length %}

sub_book is defined: {{books.sub_book}}

{% endif %}