Fix character edit form

This commit is contained in:
Tim Van Baak 2020-04-27 23:24:16 -07:00
parent c09851333e
commit 916d5c2eb5
4 changed files with 71 additions and 54 deletions

View File

@ -95,7 +95,7 @@ def player_can_create_character(
def create_character_in_lexicon( def create_character_in_lexicon(
player: UserModel, player: UserModel,
lexicon: LexiconModel, lexicon: LexiconModel,
name: str) -> None: name: str) -> str:
""" """
Unconditionally creates a character for a player Unconditionally creates a character for a player
""" """
@ -129,3 +129,5 @@ def create_character_in_lexicon(
# Log addition # Log addition
lexicon.log(f'Character "{name}" created ({character.cid})') lexicon.log(f'Character "{name}" created ({character.cid})')
return character.cid

View File

@ -9,7 +9,10 @@ from flask import (
Markup) Markup)
from flask_login import current_user from flask_login import current_user
from amanuensis.lexicon import attempt_publish from amanuensis.lexicon import (
attempt_publish,
get_player_characters,
create_character_in_lexicon)
from amanuensis.models import LexiconModel from amanuensis.models import LexiconModel
from amanuensis.parser import ( from amanuensis.parser import (
parse_raw_markdown, parse_raw_markdown,
@ -57,35 +60,53 @@ def session(name):
characters=characters) characters=characters)
def edit_character(name, form, cid): def edit_character(name, form, character):
if form.validate_on_submit(): if not form.is_submitted():
# Update character # GET, populate with values
form.update_character(g.lexicon, cid) return render_template(
'session.character.jinja', form=form.for_character(character))
if not form.validate():
# POST with invalid data, return unchanged
return render_template('session.character.jinja', form=form)
# POST with valid data, update character
with g.lexicon.ctx.edit_config() as cfg:
char = cfg.character[character.cid]
char.name = form.characterName.data
char.signature = form.defaultSignature.data
flash('Character updated') flash('Character updated')
return redirect(url_for('session.session', name=name)) return redirect(url_for('session.session', name=name))
if not form.is_submitted():
# On GET, populate with the character
form.for_character(g.lexicon, cid)
return render_template('session.character.jinja', form=form, action='edit')
def create_character(name: str, form: LexiconCharacterForm):
def create_character(name, form): # Characters can't be created if the game has already started
if form.validate_on_submit(): if g.lexicon.status != LexiconModel.PREGAME:
# On POST, verify character can be added flash("Characters can't be added after the game has started")
if not g.lexicon.can_add_character(current_user.uid):
flash('Operation not permitted')
return redirect(url_for('session.session', name=name)) return redirect(url_for('session.session', name=name))
# Add the character # Characters can't be created beyond the per-player limit
form.add_character(g.lexicon, current_user) player_characters = get_player_characters(g.lexicon, current_user.uid)
if len(list(player_characters)) >= g.lexicon.cfg.join.chars_per_player:
flash("Can't create more characters")
return redirect(url_for('session.session', name=name))
if not form.is_submitted():
# GET, populate with default values
return render_template(
'session.character.jinja', form=form.for_new())
if not form.validate():
# POST with invalid data, return unchanged
return render_template('session.character.jinja', form=form)
# POST with valid data, create character
char_name = form.characterName.data
cid = create_character_in_lexicon(current_user, g.lexicon, char_name)
with g.lexicon.ctx.edit_config() as cfg:
cfg.character[cid].signature = form.defaultSignature.data
flash('Character created') flash('Character created')
return redirect(url_for('session.session', name=name)) return redirect(url_for('session.session', name=name))
if not form.is_submitted():
# On GET, populate form for new character
form.for_new()
return render_template('session.character.jinja', form=form, action='create')
@bp_session.route('/character/', methods=['GET', 'POST']) @bp_session.route('/character/', methods=['GET', 'POST'])
@lexicon_param @lexicon_param
@ -93,16 +114,21 @@ def create_character(name, form):
def character(name): def character(name):
form = LexiconCharacterForm() form = LexiconCharacterForm()
cid = request.args.get('cid') cid = request.args.get('cid')
if cid: if not cid:
if cid not in g.lexicon.cfg.character: # No character specified, creating a new character
return create_character(name, form)
character = g.lexicon.cfg.character.get(cid)
if not character:
# Bad character id, abort
flash('Character not found') flash('Character not found')
return redirect(url_for('session.session', name=name)) return redirect(url_for('session.session', name=name))
if (g.lexicon.cfg.character.get(cid).player != current_user.uid if current_user.uid not in (character.player, g.lexicon.cfg.editor):
and g.lexicon.cfg.editor != current_user.uid): # Only its owner and the editor can edit a character
flash('Access denied') flash('Access denied')
return redirect(url_for('session.session', name=name)) return redirect(url_for('session.session', name=name))
return edit_character(name, form, cid) # Edit allowed
return create_character(name, form) return edit_character(name, form, character)
@bp_session.route('/settings/', methods=['GET', 'POST']) @bp_session.route('/settings/', methods=['GET', 'POST'])

View File

@ -3,8 +3,6 @@ from wtforms import (
StringField, SubmitField, TextAreaField, RadioField) StringField, SubmitField, TextAreaField, RadioField)
from wtforms.validators import DataRequired from wtforms.validators import DataRequired
from amanuensis.lexicon import create_character_in_lexicon
class LexiconCharacterForm(FlaskForm): class LexiconCharacterForm(FlaskForm):
"""/lexicon/<name>/session/character/""" """/lexicon/<name>/session/character/"""
@ -17,24 +15,12 @@ class LexiconCharacterForm(FlaskForm):
def for_new(self): def for_new(self):
self.characterName.data = "" self.characterName.data = ""
self.defaultSignature.data = "~" self.defaultSignature.data = "~"
return self
def for_character(self, lexicon, cid): def for_character(self, character):
char = lexicon.cfg.character.get(cid) self.characterName.data = character.name
self.characterName.data = char.name self.defaultSignature.data = character.signature
self.defaultSignature.data = char.signature return self
def add_character(self, lexicon, user):
create_character_in_lexicon(user, lexicon, self.characterName.data)
# add_character(lexicon, user, {
# 'name': self.characterName.data,
# 'signature': self.defaultSignature.data,
# })
def update_character(self, lexicon, cid):
with lexicon.ctx.edit_config() as cfg:
char = cfg.character.get(cid)
char.name = self.characterName.data
char.signature = self.defaultSignature.data
class LexiconReviewForm(FlaskForm): class LexiconReviewForm(FlaskForm):

View File

@ -9,6 +9,9 @@
<p> <p>
{{ form.characterName.label }}<br>{{ form.characterName(size=32) }} {{ form.characterName.label }}<br>{{ form.characterName(size=32) }}
</p> </p>
{% for error in form.characterName.errors %}
<span style="color: #ff0000">{{ error }}</span><br>
{% endfor %}</p>
<p> <p>
{{ form.defaultSignature.label }}<br>{{ form.defaultSignature(class_='fullwidth') }} {{ form.defaultSignature.label }}<br>{{ form.defaultSignature(class_='fullwidth') }}
</p> </p>