adding deletes

This commit is contained in:
evilchili 2024-02-04 15:22:54 -08:00
parent 669c9b46d6
commit 99ef4d61f9
4 changed files with 41 additions and 17 deletions

View File

@ -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 }} &nbsp; {{ c.form.delete }}
</form> </form>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -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)

View File

@ -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 return
if self.form.validate(): if not self.form.validate():
self.form.populate_obj(self.record) return
self.coerce_foreign_keys() self.form.populate_obj(self.record)
if not self.record.id: self.coerce_foreign_keys()
with db.transaction(): if self.record.id:
db.session.add(self.record) return
db.session.flush() with db.transaction():
logging.debug(f"Added {self.record = }") db.add(self.record)
location = f"{self.request.current_route_path()}/{self.record.uri}" logging.debug(f"Added {self.record = }")
return HTTPFound(location=location) location = f"{self.request.current_route_path()}/{self.record.uri}"
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()

View File

@ -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')