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)

View File

@@ -58,7 +58,7 @@
<a class="dropdown-item" href="{{url_for('conditions')}}">Show Conditions</a>
<a class="dropdown-item" href="{{url_for('covertypes')}}">Create new Covertype</a>
<a class="dropdown-item" href="{{url_for('covertypes')}}">Show Covertypes</a>
<a class="dropdown-item" href="{{url_for('owneds')}}">Create new Owned Type</a>
<a class="dropdown-item" href="{{url_for('owned', id=-1)}}">Create new Owned Type</a>
<a class="dropdown-item" href="{{url_for('owneds')}}">Show Ownership Types</a>
<a class="dropdown-item" href="{{url_for('ratings')}}">Create new Rating</a>
<a class="dropdown-item" href="{{url_for('ratings')}}">Show Ratings</a>

View File

@@ -18,7 +18,11 @@
<br>
</div class="row">
<div class="form-row col-lg-12">
{% if 'Create' in page_title %}
{{ form.delete( class="btn btn-outline-danger col-lg-2", style="visibility:hidden" )}}
{% else %}
{{ form.delete( class="btn btn-outline-danger col-lg-2" )}}
{% endif %}
{{ form.submit( class="btn btn-primary col-lg-2" )}}
</div class="form-row">
</form>

View File

@@ -2,6 +2,11 @@
{% block main_content %}
<h3>{{page_title}}</h3>
{% if message|length %}
<div class="row alert alert-{{alert}}">
{{message}}
</div>
{% endif %}
<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>