diff --git a/ttfrog/assets/templates/character_sheet.html b/ttfrog/assets/templates/character_sheet.html index 16220f8..a74d67a 100644 --- a/ttfrog/assets/templates/character_sheet.html +++ b/ttfrog/assets/templates/character_sheet.html @@ -10,16 +10,14 @@
{% endblock %} diff --git a/ttfrog/db/manager.py b/ttfrog/db/manager.py index 6f18766..2baa1b6 100644 --- a/ttfrog/db/manager.py +++ b/ttfrog/db/manager.py @@ -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) diff --git a/ttfrog/webserver/controllers/base.py b/ttfrog/webserver/controllers/base.py index a595545..da6f5c5 100644 --- a/ttfrog/webserver/controllers/base.py +++ b/ttfrog/webserver/controllers/base.py @@ -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() diff --git a/ttfrog/webserver/controllers/character_sheet.py b/ttfrog/webserver/controllers/character_sheet.py index 28d419a..2f4a31b 100644 --- a/ttfrog/webserver/controllers/character_sheet.py +++ b/ttfrog/webserver/controllers/character_sheet.py @@ -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')