okay, condition* now works, menu to call them works, just testing it out though, so also a BookForm started but unused at present. Finally, tweaked pagination size to 20 now that navbar takes up so much space, also made pagination choices match
This commit is contained in:
68
condition.py
Normal file
68
condition.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators, Form
|
||||
from flask import request, render_template
|
||||
from __main__ import db, app, ma
|
||||
|
||||
################################################################################
|
||||
# Class describing Condition in the database, and via sqlalchemy, connected to the DB as well
|
||||
################################################################################
|
||||
class Condition(db.Model):
|
||||
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
||||
name = db.Column(db.String(50), unique=False, nullable=False)
|
||||
|
||||
def __repr__(self):
|
||||
return "<id: {}, name: {}>".format(self.id,self.name)
|
||||
|
||||
################################################################################
|
||||
# Helper class that inherits a .dump() method to turn class Condition into json / useful in jinja2
|
||||
################################################################################
|
||||
class ConditionSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta: model = Condition
|
||||
|
||||
|
||||
################################################################################
|
||||
# Helper class that defines a form for condition, used to make html <form>, with field validation (via wtforms)
|
||||
################################################################################
|
||||
class ConditionForm(Form):
|
||||
id = HiddenField()
|
||||
name = StringField('Name:', [validators.DataRequired()])
|
||||
submit = SubmitField('Save' )
|
||||
delete = SubmitField('Delete' )
|
||||
|
||||
################################################################################
|
||||
# Routes for condition data
|
||||
#
|
||||
# /conditions -> GET only -> prints out list of all conditions
|
||||
################################################################################
|
||||
@app.route("/conditions", methods=["GET"])
|
||||
def conditions():
|
||||
conditions = Condition.query.order_by('id').all()
|
||||
print ( Condition.query.order_by('id') )
|
||||
print ( conditions )
|
||||
return render_template("conditions.html", conditions=conditions)
|
||||
|
||||
################################################################################
|
||||
# /condition/<id> -> GET/POST(save or delete) -> shows/edits/delets a single
|
||||
# condition
|
||||
################################################################################
|
||||
@app.route("/condition/<id>", methods=["GET", "POST"])
|
||||
def condition(id):
|
||||
### DDP: should this be request.form or request.values?
|
||||
alert="Success"
|
||||
condition_form = ConditionForm(request.form)
|
||||
if request.method == 'POST' and condition_form.validate():
|
||||
id = request.form['id']
|
||||
condition = Condition.query.get(id)
|
||||
try:
|
||||
request.form['submit']
|
||||
except:
|
||||
message="Sorry, Deleting unsupported at present"
|
||||
alert="Danger"
|
||||
else:
|
||||
condition.name = request.form['name']
|
||||
db.session.commit()
|
||||
message="Successfully Updated Condition (id={})".format(id)
|
||||
else:
|
||||
condition = Condition.query.get(id)
|
||||
condition_form = ConditionForm(request.values, obj=condition)
|
||||
message=""
|
||||
return render_template("condition.html", condition=condition, alert=alert, message=message, condition_form=condition_form)
|
||||
26
main.py
26
main.py
@@ -4,6 +4,7 @@ from flask import request
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_marshmallow import Marshmallow
|
||||
from flask_bootstrap import Bootstrap
|
||||
from wtforms import SubmitField, StringField, HiddenField, SelectField, validators, Form
|
||||
|
||||
app = Flask(__name__)
|
||||
### what is this value? I gather I should chagne it?
|
||||
@@ -18,6 +19,7 @@ Bootstrap(app)
|
||||
from author import Author, AuthorForm, AuthorSchema
|
||||
from publisher import Publisher, PublisherForm, PublisherSchema
|
||||
from genre import Genre, GenreForm, GenreSchema
|
||||
from condition import Condition, ConditionForm, ConditionSchema
|
||||
|
||||
####################################### CLASSES / DB model #######################################
|
||||
book_author_link = db.Table('book_author_link', db.Model.metadata,
|
||||
@@ -47,19 +49,19 @@ class Book_Sub_Book_Link(db.Model):
|
||||
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)
|
||||
publisher = db.relationship('Publisher', secondary=book_publisher_link)
|
||||
genre = db.relationship('Genre', secondary=book_genre_link )
|
||||
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))
|
||||
condition = db.Column(db.String(20))
|
||||
rating = 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)
|
||||
publisher = db.relationship('Publisher', secondary=book_publisher_link)
|
||||
genre = db.relationship('Genre', secondary=book_genre_link )
|
||||
parent_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id" )
|
||||
child_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id" )
|
||||
|
||||
@@ -69,6 +71,9 @@ class Book(db.Model):
|
||||
class Book_Sub_Book_LinkSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta: model = Book_Sub_Book_Link
|
||||
|
||||
# Note not ordering this in the code below as, I can't work out how to use
|
||||
# jinja2 to iterate over orderedDict - seems it doesnt support it?
|
||||
# so I just hacked a list of keys in book.html
|
||||
class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||
author = ma.Nested(AuthorSchema, many=True)
|
||||
publisher = ma.Nested(PublisherSchema, many=True)
|
||||
@@ -77,6 +82,17 @@ class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||
child_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
|
||||
class Meta: model = Book
|
||||
|
||||
#
|
||||
# To be completed
|
||||
#
|
||||
class BookForm(Form):
|
||||
# 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')] )
|
||||
|
||||
|
||||
### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?)
|
||||
book_schema = BookSchema()
|
||||
books_schema = BookSchema(many=True)
|
||||
|
||||
24
templates/condition.html
Normal file
24
templates/condition.html
Normal file
@@ -0,0 +1,24 @@
|
||||
{% extends "base.html" %} {% block main_content %}
|
||||
<h3><center>Condition</center></h3>
|
||||
<div class="container">
|
||||
<right>
|
||||
{% if message|length %}
|
||||
<div class="row alert alert-{{alert}}">
|
||||
{{message}}
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="row">
|
||||
<form class="form form-inline col-xl-12" action="" method="POST">
|
||||
{{ wtf.form_field( condition_form.id, form_type='inline' ) }}
|
||||
<div class="col-xl-12">
|
||||
{{ wtf.form_field( condition_form.name, form_type='horizontal', horizontal_columns=('xl', 2, 10), style="width:100%" ) }}
|
||||
</div>
|
||||
<div class="col-xl-12">
|
||||
<br></br>
|
||||
</div>
|
||||
{{ wtf.form_field( condition_form.submit, horizontal_columns=('xl', 2, 2), class="btn btn-primary offset-xl-1 col-xl-2" )}}
|
||||
{{ wtf.form_field( condition_form.delete, horizontal_columns=('xl', 2, 2), class="btn btn-danger offset-xl-1 col-xl-2" )}}
|
||||
</form>
|
||||
</div class="row">
|
||||
</div class="container">
|
||||
{% endblock main_content %}
|
||||
15
templates/conditions.html
Normal file
15
templates/conditions.html
Normal file
@@ -0,0 +1,15 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block main_content %}
|
||||
<h3>Conditions</h3>
|
||||
<table id="book_table" class="table table-striped table-sm" data-toolbar="#toolbar" data-search="true">
|
||||
<thead>
|
||||
<tr class="thead-light"><th>Name</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for condition in conditions %}
|
||||
<tr><td data-sort="{{condition.id}}"><a href="{{url_for('condition', id=condition.id )}}">{{condition.name}}</a></td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endblock main_content %}
|
||||
Reference in New Issue
Block a user