tabletop-frog/ttfrog/webserver/controllers/character_sheet.py

52 lines
1.8 KiB
Python
Raw Normal View History

2024-02-08 01:14:35 -08:00
from ttfrog.webserver.controllers.base import BaseController
from ttfrog.webserver.forms import DeferredSelectField, DeferredSelectMultipleField
from ttfrog.db.schema import Character, Ancestry, CharacterClass, AncestryTrait, Modifier, STATS
from ttfrog.db.manager import db
from ttfrog.attribute_map import AttributeMap
from wtforms_alchemy import ModelForm
2024-02-08 01:14:35 -08:00
from wtforms.fields import SubmitField, SelectMultipleField
from wtforms.widgets import Select
2024-02-04 11:40:30 -08:00
class CharacterForm(ModelForm):
class Meta:
model = Character
exclude = ['slug']
2024-02-04 15:22:54 -08:00
save = SubmitField()
delete = SubmitField()
2024-02-08 01:14:35 -08:00
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)
2024-01-28 14:31:50 -08:00
2024-01-31 22:39:54 -08:00
class CharacterSheet(BaseController):
2024-02-04 11:40:30 -08:00
model = CharacterForm.Meta.model
model_form = CharacterForm
2024-02-08 01:14:35 -08:00
@property
def resources(self):
return super().resources + [
{'type': 'script', 'uri': 'js/character_sheet.js'},
]
def template_context(self, **kwargs) -> dict:
ctx = super().template_context(**kwargs)
if self.record.ancestry:
ancestry = db.query(Ancestry).filter_by(name=self.record.ancestry).one()
ctx['traits'] = {}
for trait in db.query(AncestryTrait).filter_by(ancestry_id=ancestry.id).all():
ctx['traits'][trait.description] = db.query(Modifier).filter_by(source_table_name=trait.__tablename__, source_table_id=trait.id).all()
else:
ctx['traits'] = {};
return ctx