added transaction log, UX scaffolding
This commit is contained in:
@@ -5,10 +5,10 @@ from collections import defaultdict
|
||||
|
||||
from pyramid.httpexceptions import HTTPFound
|
||||
from pyramid.interfaces import IRoutesMapper
|
||||
from wtforms.fields import SelectField
|
||||
|
||||
from ttfrog.attribute_map import AttributeMap
|
||||
from ttfrog.db.manager import db
|
||||
from ttfrog.db import transaction_log
|
||||
|
||||
|
||||
def get_all_routes(request):
|
||||
@@ -26,12 +26,6 @@ def get_all_routes(request):
|
||||
return routes
|
||||
|
||||
|
||||
class DeferredSelectField(SelectField):
|
||||
def __init__(self, *args, model=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.choices = db.query(model).all()
|
||||
|
||||
|
||||
class BaseController:
|
||||
model = None
|
||||
model_form = None
|
||||
@@ -77,6 +71,12 @@ class BaseController:
|
||||
self._form = self.model_form(obj=self.record)
|
||||
return self._form
|
||||
|
||||
@property
|
||||
def resources(self):
|
||||
return [
|
||||
{'type': 'style', 'uri': 'css/styles.css'},
|
||||
]
|
||||
|
||||
def configure_for_model(self):
|
||||
if 'all_records' not in self.attrs:
|
||||
self.attrs['all_records'] = db.query(self.model).all()
|
||||
@@ -89,6 +89,7 @@ class BaseController:
|
||||
form=self.form,
|
||||
record=self.record,
|
||||
routes=get_all_routes(self.request),
|
||||
resources=self.resources,
|
||||
**self.attrs,
|
||||
**kwargs,
|
||||
)
|
||||
@@ -99,7 +100,9 @@ class BaseController:
|
||||
return
|
||||
if not self.form.validate():
|
||||
return
|
||||
previous = dict(self.record)
|
||||
self.form.populate_obj(self.record)
|
||||
transaction_log.record(previous, self.record)
|
||||
if self.record.id:
|
||||
return
|
||||
with db.transaction():
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from ttfrog.webserver.controllers.base import BaseController, DeferredSelectField
|
||||
from ttfrog.db.schema import Character, Ancestry
|
||||
from ttfrog.webserver.controllers.base import BaseController
|
||||
from ttfrog.webserver.forms import DeferredSelectField, DeferredSelectMultipleField
|
||||
from ttfrog.db.schema import Character, Ancestry, CharacterClass, STATS
|
||||
from wtforms_alchemy import ModelForm
|
||||
from wtforms.fields import SubmitField
|
||||
from wtforms.fields import SubmitField, SelectMultipleField
|
||||
from wtforms.widgets import Select
|
||||
|
||||
|
||||
class CharacterForm(ModelForm):
|
||||
@@ -11,9 +13,25 @@ class CharacterForm(ModelForm):
|
||||
|
||||
save = SubmitField()
|
||||
delete = SubmitField()
|
||||
ancestry = DeferredSelectField('Ancestry', model=Ancestry, coerce=str, validate_choice=True)
|
||||
|
||||
ancestry = DeferredSelectField('Ancestry', model=Ancestry, validate_choice=True, widget=Select())
|
||||
|
||||
character_class = DeferredSelectMultipleField(
|
||||
'CharacterClass',
|
||||
model=CharacterClass,
|
||||
validate_choice=True,
|
||||
# option_widget=Select(multiple=True)
|
||||
)
|
||||
|
||||
saving_throws = SelectMultipleField('Saving Throws', validate_choice=True, choices=STATS)
|
||||
|
||||
|
||||
class CharacterSheet(BaseController):
|
||||
model = CharacterForm.Meta.model
|
||||
model_form = CharacterForm
|
||||
|
||||
@property
|
||||
def resources(self):
|
||||
return super().resources + [
|
||||
{'type': 'script', 'uri': 'js/character_sheet.js'},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user