diff --git a/ttfrog/assets/templates/character_sheet.html b/ttfrog/assets/templates/character_sheet.html index d1c1ac6..9d94481 100644 --- a/ttfrog/assets/templates/character_sheet.html +++ b/ttfrog/assets/templates/character_sheet.html @@ -1,14 +1,11 @@ {% extends "base.html" %} +{% from "list.html" import build_list %} {% block content %} -
- -
-
+ +{{ build_list(all_records) }} + +

{{ record.name }}

@@ -18,8 +15,9 @@ Error: {{ form.errors['process'] |join(',') }} {% endif %}
    -
  • {{form.name.label}}: {{ form.name }} {{form.errors['name'] | join(',') }}
  • -
  • {{form.level.label}}: {{ form.level }} {{form.errors['level'] | join(',') }} + {% for field in form %} +
  • {{ field.label }}: {{ field }} {{ field.errors|join(',') }}
  • + {% endfor %}
@@ -27,9 +25,11 @@ {% endblock %} {% block debug %} +

Debug

Record

{{ record }}

Config

{{ config }}
+
{% endblock %} diff --git a/ttfrog/webserver/controllers/base.py b/ttfrog/webserver/controllers/base.py index 3e511d5..0aa5f53 100644 --- a/ttfrog/webserver/controllers/base.py +++ b/ttfrog/webserver/controllers/base.py @@ -1,3 +1,4 @@ +import logging from collections import defaultdict from wtforms_sqlalchemy.orm import model_form @@ -10,35 +11,64 @@ class BaseController: def __init__(self, request): self.request = request - self.record = None self.attrs = defaultdict(str) - self.configure() - if self.model: - self.model_form = model_form(self.model, db_session=db.session) + self.record = None + self.model_form = None - # load this from dotenv or something self.config = { 'static_url': '/static', 'project_name': 'TTFROG' } + self.configure() + self.configure_for_model() + + def configure_for_model(self): + if not self.model: + return + if not self.model_form: + self.model_form = model_form(self.model, db_session=db.session) + if not self.record: + self.record = self.load_from_slug() or self.load_from_id() + + if 'all_records' not in self.attrs: + self.attrs['all_records'] = db.query(self.model).all() + def configure(self): - self.load_from_id() + pass + + def load_from_slug(self): + if not self.model: + return + + parts = self.request.matchdict.get('uri', '').split('/') + if not parts: + return + try: + return db.query(self.model).filter(self.model.slug == parts[0])[0] + except IndexError: + logging.warning(f"Could not load record with slug {parts[0]}") def load_from_id(self): - if not self.request.POST['id']: + post_id = self.request.POST.get('id', None) + if not post_id: return - self.record = db.query(self.model).get(self.request.POST['id']) + return db.query(self.model).get(post_id) def form(self) -> str: - # no model? no form. if not self.model: - return '' + return if self.request.method == 'POST': + if not self.record: + self.record = self.model() form = self.model_form(self.request.POST, obj=self.record) if self.model.validate(form): form.populate_obj(self.record) + if not self.record.id: + with db.transaction(): + db.session.add(self.record) + logging.debug(f"Added {self.record = }") return form return self.model_form(obj=self.record) @@ -46,8 +76,8 @@ class BaseController: return dict( config=self.config, request=self.request, - record=self.record, - form=self.form(), + record=self.record or '', + form=self.form() or '', **self.attrs, **kwargs, ) diff --git a/ttfrog/webserver/controllers/character_sheet.py b/ttfrog/webserver/controllers/character_sheet.py index 9a4ae6f..c239135 100644 --- a/ttfrog/webserver/controllers/character_sheet.py +++ b/ttfrog/webserver/controllers/character_sheet.py @@ -1,20 +1,6 @@ -import logging - from ttfrog.webserver.controllers import BaseController -from ttfrog.db.manager import db from ttfrog.db.schema import Character class CharacterSheet(BaseController): model = Character - - def configure(self): - self.attrs['all_characters'] = db.query(Character).all() - slug = self.request.matchdict.get('slug', None) - if slug: - try: - self.record = db.query(Character).filter(Character.slug == slug)[0] - except IndexError: - logging.warning(f"Could not load record with slug {slug}") - else: - self.load_from_id() diff --git a/ttfrog/webserver/routes.py b/ttfrog/webserver/routes.py index 2ac0eb9..f6a8d63 100644 --- a/ttfrog/webserver/routes.py +++ b/ttfrog/webserver/routes.py @@ -1,3 +1,3 @@ def routes(config): config.add_route('index', '/') - config.add_route('sheet', '/sheet/{slug}/{name}', factory='ttfrog.webserver.controllers.CharacterSheet') + config.add_route('sheet', '/sheet/{uri:.*}', factory='ttfrog.webserver.controllers.CharacterSheet')