diff --git a/condition.py b/condition.py new file mode 100644 index 0000000..ad1d882 --- /dev/null +++ b/condition.py @@ -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 "".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
, 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/ -> GET/POST(save or delete) -> shows/edits/delets a single +# condition +################################################################################ +@app.route("/condition/", 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) diff --git a/main.py b/main.py index cf5b373..66fc9df 100644 --- a/main.py +++ b/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) diff --git a/templates/condition.html b/templates/condition.html new file mode 100644 index 0000000..9fdc56a --- /dev/null +++ b/templates/condition.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} {% block main_content %} +

Condition

+
+ + {% if message|length %} +
+ {{message}} +
+ {% endif %} +
+ + {{ wtf.form_field( condition_form.id, form_type='inline' ) }} +
+ {{ wtf.form_field( condition_form.name, form_type='horizontal', horizontal_columns=('xl', 2, 10), style="width:100%" ) }} +
+
+

+
+ {{ 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" )}} + +
+
+{% endblock main_content %} diff --git a/templates/conditions.html b/templates/conditions.html new file mode 100644 index 0000000..d295e8b --- /dev/null +++ b/templates/conditions.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block main_content %} +

Conditions

+ + + + + + {% for condition in conditions %} + + {% endfor %} + +
Name
{{condition.name}}
+{% endblock main_content %}