diff --git a/condition.py b/condition.py index 7f40310..9cb8cce 100644 --- a/condition.py +++ b/condition.py @@ -1,14 +1,16 @@ from wtforms import SubmitField, StringField, HiddenField, SelectField, validators -from flask import request, render_template +from flask import request, render_template, redirect from flask_wtf import FlaskForm from __main__ import db, app, ma +from sqlalchemy import func, Sequence +from status import st, Status ################################################################################ # 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) + id = db.Column(db.Integer, db.Sequence('condition_id_seq'), primary_key=True ) + name = db.Column(db.String(50), unique=True, nullable=False) def __repr__(self): return "".format(self.id,self.name) @@ -19,7 +21,6 @@ class Condition(db.Model): 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) ################################################################################ @@ -36,36 +37,49 @@ class ConditionForm(FlaskForm): ################################################################################ @app.route("/conditions", methods=["GET"]) def conditions(): - conditions = Condition.query.order_by('id').all() - return render_template("show_id_name.html", objects=conditions, page_title='Show All Conditions', url_base='condition') + objects = Condition.query.order_by('id').all() + return render_template("show_id_name.html", objects=objects, page_title='Show All Conditions', url_base='condition', alert=st.GetAlert(), message=st.GetMessage() ) ################################################################################ -# /condition/ -> GET/POST(save or delete) -> shows/edits/delets a single -# condition +# /condition -> GET/POST -> creates a new condition type and when created, takes you back to /conditions +################################################################################ +@app.route("/condition", methods=["GET", "POST"]) +def new_condition(): + form = ConditionForm(request.form) + if 'name' not in request.form: + return render_template("edit_id_name.html", form=form, page_title='Create new Condition' ) + else: + condition = Condition( name=request.form["name"] ) + db.session.add(condition) + db.session.commit() + st.SetMessage( "Created new Condition (id={})".format(condition.id) ) + conditions = Condition.query.order_by('id').all() + return redirect( '/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'] + form = ConditionForm(request.form) + if request.method == 'POST' and form.validate(): condition = Condition.query.get(id) - try: - request.form['submit'] - except: - message="Sorry, Deleting unsupported at present" - alert="Danger" - else: + if 'delete' in request.form: + st.SetMessage("Successfully deleted (id={}, name={})".format( condition.id, condition.name ) ) + condition = Condition.query.filter(Condition.id==id).delete() + if 'submit' in request.form: + st.SetMessage("Successfully Updated Condition (id={})".format(id) ) condition.name = request.form['name'] - db.session.commit() - message="Successfully Updated Condition (id={})".format(id) + db.session.commit() + return redirect( '/conditions' ) else: condition = Condition.query.get(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') + form = ConditionForm(request.values, obj=condition) + return render_template("edit_id_name.html", form=form, page_title='Edit Condition' ) +################################################################################ +# Gets the Condition matching id from DB, helper func in jinja2 code to show books +################################################################################ def GetConditionById(id): return Condition.query.get(id).name - diff --git a/covertype.py b/covertype.py index a58055b..26e48b5 100644 --- a/covertype.py +++ b/covertype.py @@ -1,14 +1,16 @@ from wtforms import SubmitField, StringField, HiddenField, SelectField, validators -from flask import request, render_template +from flask import request, render_template, redirect from flask_wtf import FlaskForm from __main__ import db, app, ma +from sqlalchemy import func, Sequence +from status import st, Status ################################################################################ # Class describing Covertype in the database, and via sqlalchemy, connected to the DB as well ################################################################################ class Covertype(db.Model): - id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) - name = db.Column(db.String(50), unique=False, nullable=False) + id = db.Column(db.Integer, db.Sequence('covertype_id_seq'), primary_key=True ) + name = db.Column(db.String(50), unique=True, nullable=False) def __repr__(self): return "".format(self.id,self.name) @@ -19,7 +21,6 @@ class Covertype(db.Model): class CovertypeSchema(ma.SQLAlchemyAutoSchema): class Meta: model = Covertype - ################################################################################ # Helper class that defines a form for covertype, used to make html , with field validation (via wtforms) ################################################################################ @@ -36,35 +37,49 @@ class CovertypeForm(FlaskForm): ################################################################################ @app.route("/covertypes", methods=["GET"]) 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') + objects = Covertype.query.order_by('id').all() + return render_template("show_id_name.html", objects=objects, page_title='Show All Covertypes', url_base='covertype', alert=st.GetAlert(), message=st.GetMessage() ) ################################################################################ -# /covertype/ -> GET/POST(save or delete) -> shows/edits/delets a single -# covertype +# /covertype -> GET/POST -> creates a new covertype type and when created, takes you back to /covertypes +################################################################################ +@app.route("/covertype", methods=["GET", "POST"]) +def new_covertype(): + form = CovertypeForm(request.form) + if 'name' not in request.form: + return render_template("edit_id_name.html", form=form, page_title='Create new Covertype' ) + else: + covertype = Covertype( name=request.form["name"] ) + db.session.add(covertype) + db.session.commit() + st.SetMessage( "Created new Covertype (id={})".format(covertype.id) ) + covertypes = Covertype.query.order_by('id').all() + return redirect( '/covertypes' ) + +################################################################################ +# /covertype/ -> GET/POST(save or delete) -> shows/edits/delets a single covertype ################################################################################ @app.route("/covertype/", methods=["GET", "POST"]) def covertype(id): ### DDP: should this be request.form or request.values? - alert="Success" - covertype_form = CovertypeForm(request.form) - if request.method == 'POST' and covertype_form.validate(): - id = request.form['id'] + form = CovertypeForm(request.form) + if request.method == 'POST' and form.validate(): covertype = Covertype.query.get(id) - try: - request.form['submit'] - except: - message="Sorry, Deleting unsupported at present" - alert="Danger" - else: + if 'delete' in request.form: + st.SetMessage("Successfully deleted (id={}, name={})".format( covertype.id, covertype.name ) ) + covertype = Covertype.query.filter(Covertype.id==id).delete() + if 'submit' in request.form: + st.SetMessage("Successfully Updated Covertype (id={})".format(id) ) covertype.name = request.form['name'] - db.session.commit() - message="Successfully Updated Covertype (id={})".format(id) + db.session.commit() + return redirect( '/covertypes' ) else: covertype = Covertype.query.get(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') + form = CovertypeForm(request.values, obj=covertype) + return render_template("edit_id_name.html", form=form, page_title='Edit Covertype' ) +################################################################################ +# Gets the Covertype matching id from DB, helper func in jinja2 code to show books +################################################################################ def GetCovertypeById(id): return Covertype.query.get(id).name diff --git a/genre.py b/genre.py index 166565c..4df00cf 100644 --- a/genre.py +++ b/genre.py @@ -1,14 +1,16 @@ -from wtforms import SubmitField, StringField, HiddenField, validators -from flask import request, render_template +from wtforms import SubmitField, StringField, HiddenField, SelectField, validators +from flask import request, render_template, redirect from flask_wtf import FlaskForm from __main__ import db, app, ma +from sqlalchemy import func, Sequence +from status import st, Status ################################################################################ # Class describing Genre in the database, and via sqlalchemy, connected to the DB as well ################################################################################ class Genre(db.Model): - id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) - name = db.Column(db.String(50), unique=False, nullable=False) + id = db.Column(db.Integer, db.Sequence('genre_id_seq'), primary_key=True ) + name = db.Column(db.String(50), unique=True, nullable=False) def __repr__(self): return "".format(self.id,self.name) @@ -35,13 +37,24 @@ class GenreForm(FlaskForm): ################################################################################ @app.route("/genres", methods=["GET"]) def genres(): - genres = Genre.query.all() - return render_template("show_id_name.html", objects=genres, page_title='Show All Genres', url_base='genre') + objects = Genre.query.order_by('id').all() + return render_template("show_id_name.html", objects=objects, page_title='Show All Genres', url_base='genre', alert=st.GetAlert(), message=st.GetMessage() ) - -def GetGenres(): - genres = Genre.query.order_by('name').all() - return genres +################################################################################ +# /genre -> GET/POST -> creates a new genre type and when created, takes you back to /genres +################################################################################ +@app.route("/genre", methods=["GET", "POST"]) +def new_genre(): + form = GenreForm(request.form) + if 'name' not in request.form: + return render_template("edit_id_name.html", form=form, page_title='Create new Genre' ) + else: + genre = Genre( name=request.form["name"] ) + db.session.add(genre) + db.session.commit() + st.SetMessage( "Created new Genre (id={})".format(genre.id) ) + genres = Genre.query.order_by('id').all() + return redirect( '/genres' ) ################################################################################ # /genre/ -> GET/POST(save or delete) -> shows/edits/delets a single genre @@ -49,22 +62,31 @@ def GetGenres(): @app.route("/genre/", methods=["GET", "POST"]) def genre(id): ### DDP: should this be request.form or request.values? - alert="Success" - genre_form = GenreForm(request.form) - if request.method == 'POST' and genre_form.validate(): - id = request.form['id'] + form = GenreForm(request.form) + if request.method == 'POST' and form.validate(): genre = Genre.query.get(id) - try: - request.form['submit'] - except: - message="Sorry, Deleting unsupported at present" - alert="Danger" - else: + if 'delete' in request.form: + st.SetMessage("Successfully deleted (id={}, name={})".format( genre.id, genre.name ) ) + genre = Genre.query.filter(Genre.id==id).delete() + if 'submit' in request.form: + st.SetMessage("Successfully Updated Genre (id={})".format(id) ) genre.name = request.form['name'] - db.session.commit() - message="Successfully Updated Genre (id={})".format(id) + db.session.commit() + return redirect( '/genres' ) else: genre = Genre.query.get(id) - genre_form = GenreForm(request.values, obj=genre) - message="" - return render_template("edit_id_name.html", genre=genre, alert=alert, message=message, form=genre_form, page_title='Edit Genre') + form = GenreForm(request.values, obj=genre) + return render_template("edit_id_name.html", form=form, page_title='Edit Genre' ) + +################################################################################ +# Gets the Genre matching id from DB, helper func in jinja2 code to show books +################################################################################ +def GetGenreById(id): + return Genre.query.get(id).name + +################################################################################ +# Gets the list of Genres, populates genre_list var in jinja2 code in book.html +################################################################################ +def GetGenres(): + genres = Genre.query.order_by('name').all() + return genres diff --git a/owned.py b/owned.py index dcc5373..5591ef9 100644 --- a/owned.py +++ b/owned.py @@ -37,35 +37,32 @@ class OwnedForm(FlaskForm): ################################################################################ @app.route("/owneds", methods=["GET"]) def owneds(): - owneds = Owned.query.order_by('id').all() - return render_template("show_id_name.html", objects=owneds, page_title='Show All Ownership types', url_base='owned', alert=st.GetAlert(), message=st.GetMessage() ) + objects = Owned.query.order_by('id').all() + return render_template("show_id_name.html", objects=objects, page_title='Show All Ownership types', url_base='owned', alert=st.GetAlert(), message=st.GetMessage() ) ################################################################################ # /owned -> GET/POST -> creates a new owned type and when created, takes you back to /owneds ################################################################################ @app.route("/owned", methods=["GET", "POST"]) def new_owned(): - owned_form = OwnedForm(request.form) + form = OwnedForm(request.form) if 'name' not in request.form: - owned=None - return render_template("edit_id_name.html", owned=owned, form=owned_form, page_title='Create new Owned Type' ) + return render_template("edit_id_name.html", form=form, page_title='Create new Owned Type' ) else: owned = Owned( name=request.form["name"] ) db.session.add(owned) db.session.commit() st.SetMessage( "Created new Owned Type (id={})".format(owned.id) ) - owneds = Owned.query.order_by('id').all() return redirect( '/owneds' ) ################################################################################ -# /owned/ -> GET/POST(save or delete) -> shows/edits/delets a single -# owned +# /owned/ -> GET/POST(save or delete) -> shows/edits/delets a single owned ################################################################################ @app.route("/owned/", methods=["GET", "POST"]) def owned(id): ### DDP: should this be request.form or request.values? - owned_form = OwnedForm(request.form) - if request.method == 'POST' and owned_form.validate(): + form = OwnedForm(request.form) + if request.method == 'POST' and form.validate(): owned = Owned.query.get(id) if 'delete' in request.form: st.SetMessage("Successfully deleted (id={}, name={})".format( owned.id, owned.name ) ) @@ -74,11 +71,14 @@ def owned(id): owned.name = request.form['name'] st.SetMessage("Successfully Updated Owned (id={})".format(id) ) db.session.commit() - owneds = Owned.query.order_by('id').all() return redirect( '/owneds' ) else: - owned = Owned.query.get(id) - owned_form = OwnedForm(request.values, obj=owned) - return render_template("edit_id_name.html", owned=owned, form=owned_form, page_title='Edit Owned Type' ) + obj = Owned.query.get(id) + form = OwnedForm(request.values, obj=obj) + return render_template("edit_id_name.html", form=form, page_title='Edit Owned Type' ) + +################################################################################ +# Gets the Owned matching id from DB, helper func in jinja2 code to show books +################################################################################ def GetOwnedById(id): return Owned.query.get(id).name diff --git a/rating.py b/rating.py index 67755d3..ee50184 100644 --- a/rating.py +++ b/rating.py @@ -1,14 +1,16 @@ from wtforms import SubmitField, StringField, HiddenField, SelectField, validators -from flask import request, render_template +from flask import request, render_template, redirect from flask_wtf import FlaskForm from __main__ import db, app, ma +from sqlalchemy import func, Sequence +from status import st, Status ################################################################################ # Class describing Rating in the database, and via sqlalchemy, connected to the DB as well ################################################################################ class Rating(db.Model): - id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) - name = db.Column(db.String(50), unique=False, nullable=False) + id = db.Column(db.Integer, db.Sequence('rating_id_seq'), primary_key=True ) + name = db.Column(db.String(50), unique=True, nullable=False) def __repr__(self): return "".format(self.id,self.name) @@ -19,7 +21,6 @@ class Rating(db.Model): class RatingSchema(ma.SQLAlchemyAutoSchema): class Meta: model = Rating - ################################################################################ # Helper class that defines a form for rating, used to make html , with field validation (via wtforms) ################################################################################ @@ -36,32 +37,49 @@ class RatingForm(FlaskForm): ################################################################################ @app.route("/ratings", methods=["GET"]) def ratings(): - ratings = Rating.query.order_by('id').all() - return render_template("show_id_name.html", objects=ratings, page_title='Show All Ratings', url_base='rating') + objects = Rating.query.order_by('id').all() + return render_template("show_id_name.html", objects=objects, page_title='Show All Ratings', url_base='rating', alert=st.GetAlert(), message=st.GetMessage() ) ################################################################################ -# /rating/ -> GET/POST(save or delete) -> shows/edits/delets a single -# rating +# /rating -> GET/POST -> creates a new rating type and when created, takes you back to /ratings +################################################################################ +@app.route("/rating", methods=["GET", "POST"]) +def new_rating(): + form = RatingForm(request.form) + if 'name' not in request.form: + return render_template("edit_id_name.html", form=form, page_title='Create new Rating' ) + else: + rating = Rating( name=request.form["name"] ) + db.session.add(rating) + db.session.commit() + st.SetMessage( "Created new Rating (id={})".format(rating.id) ) + ratings = Rating.query.order_by('id').all() + return redirect( '/ratings' ) + +################################################################################ +# /rating/ -> GET/POST(save or delete) -> shows/edits/delets a single rating ################################################################################ @app.route("/rating/", methods=["GET", "POST"]) def rating(id): ### DDP: should this be request.form or request.values? - alert="Success" - rating_form = RatingForm(request.form) - if request.method == 'POST' and rating_form.validate(): - id = request.form['id'] + form = RatingForm(request.form) + if request.method == 'POST' and form.validate(): rating = Rating.query.get(id) - try: - request.form['submit'] - except: - message="Sorry, Deleting unsupported at present" - alert="Danger" - else: + if 'delete' in request.form: + st.SetMessage("Successfully deleted (id={}, name={})".format( rating.id, rating.name ) ) + rating = Rating.query.filter(Rating.id==id).delete() + if 'submit' in request.form: + st.SetMessage("Successfully Updated Rating (id={})".format(id) ) rating.name = request.form['name'] - db.session.commit() - message="Successfully Updated Rating (id={})".format(id) + db.session.commit() + return redirect( '/ratings' ) else: rating = Rating.query.get(id) - rating_form = RatingForm(request.values, obj=rating) - message="" - return render_template("edit_id_name.html", rating=rating, alert=alert, message=message, form=rating_form, page_title='Edit Rating') + form = RatingForm(request.values, obj=rating) + return render_template("edit_id_name.html", form=form, page_title='Edit Rating' ) + +################################################################################ +# Gets the Rating matching id from DB, helper func in jinja2 code to show books +################################################################################ +def GetRatingById(id): + return Rating.query.get(id).name diff --git a/templates/base.html b/templates/base.html index 7393464..b83ccce 100644 --- a/templates/base.html +++ b/templates/base.html @@ -52,15 +52,15 @@