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