decided to use thing/-1 to create a new thing. Tried it on owned first, works so now also can delete owned/<id> as well. Also made sure not to show the delete button when we are only creating a thing

This commit is contained in:
2020-12-04 18:01:54 +11:00
parent cc9e7dff5b
commit ed07d1f9cb
4 changed files with 41 additions and 16 deletions

View File

@@ -2,13 +2,14 @@ from wtforms import SubmitField, StringField, HiddenField, SelectField, validato
from flask import request, render_template
from flask_wtf import FlaskForm
from __main__ import db, app, ma
from sqlalchemy import func
################################################################################
# Class describing Owned in the database, and via sqlalchemy, connected to the DB as well
################################################################################
class Owned(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, primary_key=True )
name = db.Column(db.String(50), unique=True, nullable=False)
def __repr__(self):
return "<id: {}, name: {}>".format(self.id,self.name)
@@ -45,21 +46,36 @@ def owneds():
################################################################################
@app.route("/owned/<id>", methods=["GET", "POST"])
def owned(id):
### DDP: should this be request.form or request.values?
alert="Success"
alert="success"
owned_form = OwnedForm(request.form)
if request.method == 'POST' and owned_form.validate():
id = request.form['id']
owned = Owned.query.get(id)
try:
request.form['submit']
except:
message="Sorry, Deleting unsupported at present"
alert="Danger"
if id == "-1":
if 'name' not in request.form:
owned=None
message=""
return render_template("edit_id_name.html", owned=owned, alert=alert, message=message, form=owned_form, page_title='Create new Owned Type' )
else:
owned.name = request.form['name']
res=db.session.query(func.max(Owned.id)).all()
new_id=res[0][0]
new_id=new_id+1
owned = Owned( id=new_id, name=request.form["name"] )
db.session.add(owned)
db.session.commit()
message="Created new Owned Type (id={})".format(new_id)
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=alert, message=message)
### DDP: should this be request.form or request.values?
if request.method == 'POST' and owned_form.validate():
owned = Owned.query.get(id)
if 'delete' in request.form:
message="Successfully deleted (id={}, name={})".format( owned.id, owned.name )
owned = Owned.query.filter(Owned.id==id).delete()
if 'submit' in request.form:
owned.name = request.form['name']
message="Successfully Updated Owned (id={})".format(id)
db.session.commit()
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=alert, message=message)
else:
owned = Owned.query.get(id)
owned_form = OwnedForm(request.values, obj=owned)
@@ -67,4 +83,4 @@ def owned(id):
return render_template("edit_id_name.html", owned=owned, alert=alert, message=message, form=owned_form, page_title='Edit Owned Type' )
def GetOwnedById(id):
return Owned.query.get(id).name
return Owned.query.get(id).name