adding create
This commit is contained in:
parent
5de3f74a88
commit
9277494a05
|
@ -1,14 +1,11 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
{% from "list.html" import build_list %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div style='float:left; max-width:20%;height: 90%'>
|
|
||||||
<ul>
|
{{ build_list(all_records) }}
|
||||||
{% for char in all_characters %}
|
|
||||||
<li><a href="/sheet/{{char['slug']}}/{{char['name']}}">{{ char['name'] }}</a></li>
|
<div style='float:left;'>
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<h1>{{ record.name }}</h1>
|
<h1>{{ record.name }}</h1>
|
||||||
|
|
||||||
<form name="character_sheet" method="post" novalidate class="form">
|
<form name="character_sheet" method="post" novalidate class="form">
|
||||||
|
@ -18,8 +15,9 @@
|
||||||
Error: {{ form.errors['process'] |join(',') }}
|
Error: {{ form.errors['process'] |join(',') }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<ul>
|
<ul>
|
||||||
<li>{{form.name.label}}: {{ form.name }} {{form.errors['name'] | join(',') }}</li>
|
{% for field in form %}
|
||||||
<li>{{form.level.label}}: {{ form.level }} {{form.errors['level'] | join(',') }}<//li>
|
<li>{{ field.label }}: {{ field }} {{ field.errors|join(',') }}</li>
|
||||||
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
<button type="submit">Submit</button>
|
<button type="submit">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -27,9 +25,11 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block debug %}
|
{% block debug %}
|
||||||
|
<div style='clear:both;display:block;'>
|
||||||
<h2>Debug</h2>
|
<h2>Debug</h2>
|
||||||
<h3>Record</h3>
|
<h3>Record</h3>
|
||||||
<pre>{{ record }}</pre>
|
<pre>{{ record }}</pre>
|
||||||
<h3>Config</h3>
|
<h3>Config</h3>
|
||||||
<pre>{{ config }}</pre>
|
<pre>{{ config }}</pre>
|
||||||
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from wtforms_sqlalchemy.orm import model_form
|
from wtforms_sqlalchemy.orm import model_form
|
||||||
|
@ -10,35 +11,64 @@ class BaseController:
|
||||||
|
|
||||||
def __init__(self, request):
|
def __init__(self, request):
|
||||||
self.request = request
|
self.request = request
|
||||||
self.record = None
|
|
||||||
self.attrs = defaultdict(str)
|
self.attrs = defaultdict(str)
|
||||||
self.configure()
|
self.record = None
|
||||||
if self.model:
|
self.model_form = None
|
||||||
self.model_form = model_form(self.model, db_session=db.session)
|
|
||||||
|
|
||||||
# load this from dotenv or something
|
|
||||||
self.config = {
|
self.config = {
|
||||||
'static_url': '/static',
|
'static_url': '/static',
|
||||||
'project_name': 'TTFROG'
|
'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):
|
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):
|
def load_from_id(self):
|
||||||
if not self.request.POST['id']:
|
post_id = self.request.POST.get('id', None)
|
||||||
|
if not post_id:
|
||||||
return
|
return
|
||||||
self.record = db.query(self.model).get(self.request.POST['id'])
|
return db.query(self.model).get(post_id)
|
||||||
|
|
||||||
def form(self) -> str:
|
def form(self) -> str:
|
||||||
# no model? no form.
|
|
||||||
if not self.model:
|
if not self.model:
|
||||||
return ''
|
return
|
||||||
|
|
||||||
if self.request.method == 'POST':
|
if self.request.method == 'POST':
|
||||||
|
if not self.record:
|
||||||
|
self.record = self.model()
|
||||||
form = self.model_form(self.request.POST, obj=self.record)
|
form = self.model_form(self.request.POST, obj=self.record)
|
||||||
if self.model.validate(form):
|
if self.model.validate(form):
|
||||||
form.populate_obj(self.record)
|
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 form
|
||||||
return self.model_form(obj=self.record)
|
return self.model_form(obj=self.record)
|
||||||
|
|
||||||
|
@ -46,8 +76,8 @@ class BaseController:
|
||||||
return dict(
|
return dict(
|
||||||
config=self.config,
|
config=self.config,
|
||||||
request=self.request,
|
request=self.request,
|
||||||
record=self.record,
|
record=self.record or '',
|
||||||
form=self.form(),
|
form=self.form() or '',
|
||||||
**self.attrs,
|
**self.attrs,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,20 +1,6 @@
|
||||||
import logging
|
|
||||||
|
|
||||||
from ttfrog.webserver.controllers import BaseController
|
from ttfrog.webserver.controllers import BaseController
|
||||||
from ttfrog.db.manager import db
|
|
||||||
from ttfrog.db.schema import Character
|
from ttfrog.db.schema import Character
|
||||||
|
|
||||||
|
|
||||||
class CharacterSheet(BaseController):
|
class CharacterSheet(BaseController):
|
||||||
model = Character
|
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):
|
def routes(config):
|
||||||
config.add_route('index', '/')
|
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