fixed bug with route to / now needing to be to base.html, not main.html, then just added publisher (list all/edit 1/deleted button)

This commit is contained in:
2020-11-14 15:33:43 +11:00
parent 3831c0a03b
commit 29f8189c97
5 changed files with 113 additions and 9 deletions

17
main.py
View File

@@ -16,6 +16,7 @@ ma = Marshmallow(app)
Bootstrap(app)
from author import Author, AuthorForm, AuthorSchema
from publisher import Publisher, PublisherForm, PublisherSchema
####################################### CLASSES / DB model #######################################
book_author_link = db.Table('book_author_link', db.Model.metadata,
@@ -64,12 +65,12 @@ class Book(db.Model):
def __repr__(self):
return "<id: {}, author: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}>".format(self.id, self.author, self.title, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher )
class Publisher(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)
#$class Publisher(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)
class Genre_Lst(db.Model):
__tablename__ = "genre_lst"
@@ -79,8 +80,8 @@ class Genre_Lst(db.Model):
def __repr__(self):
return "<id: {}, genre: {}>".format(self.id, self.genre)
class PublisherSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Publisher
#class PublisherSchema(ma.SQLAlchemyAutoSchema):
# class Meta: model = Publisher
class Genre_LstSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Genre_Lst

64
publisher.py Normal file
View File

@@ -0,0 +1,64 @@
from wtforms import SubmitField, StringField, HiddenField, validators, Form
from flask import request, render_template
from __main__ import db, app, ma
################################################################################
# Class describing Publisher in the database, and via sqlalchemy, connected to the DB as well
################################################################################
class Publisher(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 Publisher into json / useful in jinja2
################################################################################
class PublisherSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Publisher
################################################################################
# Helper class that defines a form for publisher, used to make html <form>, with field validation (via wtforms)
################################################################################
class PublisherForm(Form):
id = HiddenField()
name = StringField('Name:', [validators.DataRequired()])
submit = SubmitField('Save' )
delete = SubmitField('Delete' )
################################################################################
# Routes for publisher data
#
# /publishers -> GET only -> prints out list of all publishers
################################################################################
@app.route("/publishers", methods=["GET"])
def publishers():
publishers = Publisher.query.all()
return render_template("publishers.html", publishers=publishers)
################################################################################
# /publisher/<id> -> GET/POST(save or delete) -> shows/edits/delets a single publisher
################################################################################
@app.route("/publisher/<id>", methods=["GET", "POST"])
def publisher(id):
### DDP: should this be request.form or request.values?
alert="Success"
publisher_form = PublisherForm(request.form)
if request.method == 'POST' and publisher_form.validate():
id = request.form['id']
publisher = Publisher.query.get(id)
try:
request.form['submit']
except:
message="Sorry, Deleting unsupported at present"
alert="Danger"
else:
publisher.name = request.form['name']
db.session.commit()
message="Successfully Updated Publisher (id={})".format(id)
else:
publisher = Publisher.query.get(id)
publisher_form = PublisherForm(request.values, obj=publisher)
message=""
return render_template("publisher.html", publisher=publisher, alert=alert, message=message, publisher_form=publisher_form)

View File

@@ -36,7 +36,7 @@
<div class="nav-item dropdown">
<a class="nav-item dropdown nav-link dropdown-toggle" href="#" id="PublisherMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Publishers</a>
<div class="dropdown-menu" aria-labelledby="PublisherMenu">
<a class="dropdown-item" href="#">Show All</a>
<a class="dropdown-item" href="{{url_for('publishers')}}">Show All</a>
</div>
</div>
</div>

24
templates/publisher.html Normal file
View File

@@ -0,0 +1,24 @@
{% extends "base.html" %} {% block main_content %}
<h3><center>Publisher</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( publisher_form.id, form_type='inline' ) }}
<div class="col-xl-12">
{{ wtf.form_field( publisher_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( publisher_form.submit, horizontal_columns=('xl', 2, 2), class="btn btn-primary offset-xl-1 col-xl-2" )}}
{{ wtf.form_field( publisher_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/publishers.html Normal file
View File

@@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block main_content %}
<h3>Publishers</h3>
<table id="book_table" class="table table-striped table-sm" data-toolbar="#toolbar" data-search="true">
<thead>
<tr class="thead-light"><th>Surname</th><th>Firstname(s)</th></tr>
</thead>
<tbody>
{% for publisher in publishers %}
<tr><td><a href="{{url_for('publisher', id=publisher.id )}}">{{publisher.name}}</a></td></tr>
{% endfor %}
</tbody>
</table>
{% endblock main_content %}