adding deletes
This commit is contained in:
parent
669c9b46d6
commit
99ef4d61f9
|
@ -10,16 +10,14 @@
|
||||||
|
|
||||||
<form name="character_sheet" method="post" novalidate class="form">
|
<form name="character_sheet" method="post" novalidate class="form">
|
||||||
{{ c.form.csrf_token }}
|
{{ c.form.csrf_token }}
|
||||||
|
|
||||||
{% if 'process' in c.form.errors %}
|
|
||||||
Error: {{ c.form.errors.process |join(',') }}
|
|
||||||
{% endif %}
|
|
||||||
<ul>
|
<ul>
|
||||||
{% for field in c.form %}
|
{% for field in c.form %}
|
||||||
|
{% if field.name not in ['save', 'delete'] %}
|
||||||
<li>{{ field.label }}: {{ field }} {{ field.errors|join(',') }}</li>
|
<li>{{ field.label }}: {{ field }} {{ field.errors|join(',') }}</li>
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
<button type="submit">Submit</button>
|
{{ c.form.save }} {{ c.form.delete }}
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -52,6 +52,10 @@ class SQLDatabaseManager:
|
||||||
tm.abort()
|
tm.abort()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def add(self, *args, **kwargs):
|
||||||
|
self.session.add(*args, **kwargs)
|
||||||
|
self.session.flush()
|
||||||
|
|
||||||
def query(self, *args, **kwargs):
|
def query(self, *args, **kwargs):
|
||||||
return self.session.query(*args, **kwargs)
|
return self.session.query(*args, **kwargs)
|
||||||
|
|
||||||
|
|
|
@ -104,16 +104,34 @@ class BaseController:
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
def response(self):
|
def save(self):
|
||||||
if not (self.request.POST and self.form):
|
if not self.form.save.data:
|
||||||
|
return
|
||||||
|
if not self.form.validate():
|
||||||
return
|
return
|
||||||
if self.form.validate():
|
|
||||||
self.form.populate_obj(self.record)
|
self.form.populate_obj(self.record)
|
||||||
self.coerce_foreign_keys()
|
self.coerce_foreign_keys()
|
||||||
if not self.record.id:
|
if self.record.id:
|
||||||
|
return
|
||||||
with db.transaction():
|
with db.transaction():
|
||||||
db.session.add(self.record)
|
db.add(self.record)
|
||||||
db.session.flush()
|
|
||||||
logging.debug(f"Added {self.record = }")
|
logging.debug(f"Added {self.record = }")
|
||||||
location = f"{self.request.current_route_path()}/{self.record.uri}"
|
location = f"{self.request.current_route_path()}/{self.record.uri}"
|
||||||
return HTTPFound(location=location)
|
return HTTPFound(location=location)
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
if not self.record.id:
|
||||||
|
return
|
||||||
|
with db.transaction():
|
||||||
|
db.query(self.model).filter_by(id=self.record.id).delete()
|
||||||
|
logging.debug(f"Deleted {self.record = }")
|
||||||
|
location = self.request.current_route_path()
|
||||||
|
return HTTPFound(location=location)
|
||||||
|
|
||||||
|
def response(self):
|
||||||
|
if not self.form:
|
||||||
|
return
|
||||||
|
elif self.form.save.data:
|
||||||
|
return self.save()
|
||||||
|
elif self.form.delete.data:
|
||||||
|
return self.delete()
|
||||||
|
|
|
@ -3,6 +3,7 @@ from ttfrog.db.schema import Character, Ancestry
|
||||||
from ttfrog.db.manager import db
|
from ttfrog.db.manager import db
|
||||||
from wtforms_alchemy import ModelForm, QuerySelectField
|
from wtforms_alchemy import ModelForm, QuerySelectField
|
||||||
from wtforms.validators import InputRequired
|
from wtforms.validators import InputRequired
|
||||||
|
from wtforms.fields import SubmitField
|
||||||
|
|
||||||
|
|
||||||
class CharacterForm(ModelForm):
|
class CharacterForm(ModelForm):
|
||||||
|
@ -13,6 +14,9 @@ class CharacterForm(ModelForm):
|
||||||
def get_session():
|
def get_session():
|
||||||
return db.session
|
return db.session
|
||||||
|
|
||||||
|
save = SubmitField()
|
||||||
|
delete = SubmitField()
|
||||||
|
|
||||||
ancestry = QuerySelectField('Ancestry', validators=[InputRequired()],
|
ancestry = QuerySelectField('Ancestry', validators=[InputRequired()],
|
||||||
query_factory=query_factory(Ancestry), get_label='name')
|
query_factory=query_factory(Ancestry), get_label='name')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user