adding create
This commit is contained in:
parent
5de3f74a88
commit
9277494a05
|
@ -1,14 +1,11 @@
|
|||
{% extends "base.html" %}
|
||||
{% from "list.html" import build_list %}
|
||||
|
||||
{% block content %}
|
||||
<div style='float:left; max-width:20%;height: 90%'>
|
||||
<ul>
|
||||
{% for char in all_characters %}
|
||||
<li><a href="/sheet/{{char['slug']}}/{{char['name']}}">{{ char['name'] }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
{{ build_list(all_records) }}
|
||||
|
||||
<div style='float:left;'>
|
||||
<h1>{{ record.name }}</h1>
|
||||
|
||||
<form name="character_sheet" method="post" novalidate class="form">
|
||||
|
@ -18,8 +15,9 @@
|
|||
Error: {{ form.errors['process'] |join(',') }}
|
||||
{% endif %}
|
||||
<ul>
|
||||
<li>{{form.name.label}}: {{ form.name }} {{form.errors['name'] | join(',') }}</li>
|
||||
<li>{{form.level.label}}: {{ form.level }} {{form.errors['level'] | join(',') }}<//li>
|
||||
{% for field in form %}
|
||||
<li>{{ field.label }}: {{ field }} {{ field.errors|join(',') }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
|
@ -27,9 +25,11 @@
|
|||
{% endblock %}
|
||||
|
||||
{% block debug %}
|
||||
<div style='clear:both;display:block;'>
|
||||
<h2>Debug</h2>
|
||||
<h3>Record</h3>
|
||||
<pre>{{ record }}</pre>
|
||||
<h3>Config</h3>
|
||||
<pre>{{ config }}</pre>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue
Block a user