Rename route parameter to lexicon_name

This disambiguates and future-proofs the route parameters so they can be more
easily referenced in before_request contexts shared by all routes.
This commit is contained in:
Tim Van Baak 2021-09-04 09:21:05 -07:00
parent 3a9b016320
commit ccb285cbf0
8 changed files with 56 additions and 38 deletions

View File

@ -27,7 +27,7 @@ def article_link(title):
"""Get the url for a lexicon by its title""" """Get the url for a lexicon by its title"""
return url_for( return url_for(
'lexicon.article', 'lexicon.article',
name=g.lexicon.name, lexicon_name=g.lexicon.name,
title=filesafe_title(title)) title=filesafe_title(title))

View File

@ -34,7 +34,7 @@ def lexicon_param(route):
@wraps(route) @wraps(route)
def with_lexicon(*args, **kwargs): def with_lexicon(*args, **kwargs):
db: DbContext = g.db db: DbContext = g.db
name: str = kwargs.get('name') name: str = kwargs.get('lexicon_name')
lexicon: Optional[Lexicon] = lexiq.try_from_name(db, name) lexicon: Optional[Lexicon] = lexiq.try_from_name(db, name)
if lexicon is None: if lexicon is None:
flash(f"Couldn't find a lexicon with the name \"{name}\"") flash(f"Couldn't find a lexicon with the name \"{name}\"")
@ -71,7 +71,7 @@ def player_required(route):
if not mem: if not mem:
flash("You must be a player to view this page") flash("You must be a player to view this page")
if lexicon.public: if lexicon.public:
return redirect(url_for('lexicon.contents', name=lexicon.name)) return redirect(url_for('lexicon.contents', lexicon_name=lexicon.name))
else: else:
return redirect(url_for('home.home')) return redirect(url_for('home.home'))
return route(*args, **kwargs) return route(*args, **kwargs)
@ -108,6 +108,6 @@ def editor_required(route):
mem: Optional[Membership] = memq.try_from_ids(db, user.id, lexicon.id) mem: Optional[Membership] = memq.try_from_ids(db, user.id, lexicon.id)
if not mem or not mem.is_editor: if not mem or not mem.is_editor:
flash("You must be the editor to view this page") flash("You must be the editor to view this page")
return redirect(url_for('lexicon.contents', name=lexicon.name)) return redirect(url_for('lexicon.contents', lexicon_name=lexicon.name))
return route(*args, **kwargs) return route(*args, **kwargs)
return editor_route return editor_route

View File

@ -9,23 +9,23 @@
{% block sb_logo %}{% endblock %} {% block sb_logo %}{% endblock %}
{% block sb_characters %}<a {% block sb_characters %}<a
{% if current_page == "characters" %}class="current-page" {% if current_page == "characters" %}class="current-page"
{% else %}href="{{ url_for('lexicon.characters.list', name=g.lexicon.name) }}" {% else %}href="{{ url_for('lexicon.characters.list', lexicon_name=g.lexicon.name) }}"
{% endif %}>Characters</a>{% endblock %} {% endif %}>Characters</a>{% endblock %}
{% block sb_contents %}<a {% block sb_contents %}<a
{% if current_page == "contents" %}class="current-page" {% if current_page == "contents" %}class="current-page"
{% else %}href="{{ url_for('lexicon.contents', name=g.lexicon.name) }}" {% else %}href="{{ url_for('lexicon.contents', lexicon_name=g.lexicon.name) }}"
{% endif %}>Contents</a>{% endblock %} {% endif %}>Contents</a>{% endblock %}
{% block sb_rules %}<a {% block sb_rules %}<a
{% if current_page == "rules" %}class="current-page" {% if current_page == "rules" %}class="current-page"
{% else %}href="{{ url_for('lexicon.rules', name=g.lexicon.name) }}" {% else %}href="{{ url_for('lexicon.rules', lexicon_name=g.lexicon.name) }}"
{% endif %}>Rules</a>{% endblock %} {% endif %}>Rules</a>{% endblock %}
{% block sb_session %}<a {% block sb_session %}<a
{% if current_page == "session" %}class="current-page" {% if current_page == "session" %}class="current-page"
{% else %}href="#{#{ url_for('session.session', name=g.lexicon.name) }#}" {% else %}href="#{#{ url_for('session.session', lexicon_name=g.lexicon.name) }}"
{% endif %}>Session</a>{% endblock %} {% endif %}>Session</a>{% endblock %}
{% block sb_stats %}<a {% block sb_stats %}<a
{% if current_page == "statistics" %}class="current-page" {% if current_page == "statistics" %}class="current-page"
{% else %}href="{{ url_for('lexicon.stats', name=g.lexicon.name) }}" {% else %}href="{{ url_for('lexicon.stats', lexicon_name=g.lexicon.name) }}"
{% endif %}>Statistics</a>{% endblock %} {% endif %}>Statistics</a>{% endblock %}
{% if current_user.is_authenticated and ( {% if current_user.is_authenticated and (

View File

@ -10,14 +10,16 @@ from .characters import bp as characters_bp
from .forms import LexiconJoinForm from .forms import LexiconJoinForm
bp = Blueprint("lexicon", __name__, url_prefix="/lexicon/<name>", template_folder=".") bp = Blueprint(
"lexicon", __name__, url_prefix="/lexicon/<lexicon_name>", template_folder="."
)
bp.register_blueprint(characters_bp) bp.register_blueprint(characters_bp)
@bp.route("/join/", methods=["GET", "POST"]) @bp.route("/join/", methods=["GET", "POST"])
@lexicon_param @lexicon_param
@login_required @login_required
def join(name): def join(lexicon_name):
lexicon: Lexicon = g.lexicon lexicon: Lexicon = g.lexicon
if not lexicon.joinable: if not lexicon.joinable:
flash("This game isn't open for joining") flash("This game isn't open for joining")
@ -27,7 +29,9 @@ def join(name):
if not form.validate_on_submit(): if not form.validate_on_submit():
# GET or POST with invalid form data # GET or POST with invalid form data
return render_template("lexicon.join.jinja", form=form) return render_template(
"lexicon.join.jinja", lexicon_name=lexicon_name, form=form
)
# POST with valid data # POST with valid data
# If the game is passworded, check password # If the game is passworded, check password
@ -37,48 +41,48 @@ def join(name):
): ):
# Bad creds, try again # Bad creds, try again
flash("Incorrect password") flash("Incorrect password")
return redirect(url_for("lexicon.join", name=name)) return redirect(url_for("lexicon.join", lexicon_name=lexicon_name))
# If the password was correct, check if the user can join # If the password was correct, check if the user can join
user: User = current_user user: User = current_user
try: try:
memq.create(db, user.id, lexicon.id, is_editor=False) memq.create(db, user.id, lexicon.id, is_editor=False)
return redirect(url_for("session.session", name=name)) return redirect(url_for("session.session", lexicon_name=lexicon_name))
except ArgumentError: except ArgumentError:
flash("Could not join game") flash("Could not join game")
return redirect(url_for("home.home", name=name)) return redirect(url_for("home.home", lexicon_name=lexicon_name))
@bp.get("/contents/") @bp.get("/contents/")
@lexicon_param @lexicon_param
@player_required_if_not_public @player_required_if_not_public
def contents(name): def contents(lexicon_name):
# indexed = sort_by_index_spec(info, g.lexicon.cfg.article.index.list) # indexed = sort_by_index_spec(info, g.lexicon.cfg.article.index.list)
# for articles in indexed.values(): # for articles in indexed.values():
# for i in range(len(articles)): # for i in range(len(articles)):
# articles[i] = { # articles[i] = {
# 'title': articles[i], # 'title': articles[i],
# **info.get(articles[i])} # **info.get(articles[i])}
return render_template("lexicon.contents.jinja") return render_template("lexicon.contents.jinja", lexicon_name=lexicon_name)
@bp.get("/article/<title>") @bp.get("/article/<title>")
@lexicon_param @lexicon_param
@player_required_if_not_public @player_required_if_not_public
def article(name, title): def article(lexicon_name, title):
# article = {**a, 'html': Markup(a['html'])} # article = {**a, 'html': Markup(a['html'])}
return render_template("lexicon.article.jinja") return render_template("lexicon.article.jinja", lexicon_name=lexicon_name)
@bp.get("/rules/") @bp.get("/rules/")
@lexicon_param @lexicon_param
@player_required_if_not_public @player_required_if_not_public
def rules(name): def rules(lexicon_name):
return render_template("lexicon.rules.jinja") return render_template("lexicon.rules.jinja", lexicon_name=lexicon_name)
@bp.get("/statistics/") @bp.get("/statistics/")
@lexicon_param @lexicon_param
@player_required_if_not_public @player_required_if_not_public
def stats(name): def stats(lexicon_name):
return render_template("lexicon.statistics.jinja") return render_template("lexicon.statistics.jinja", lexicon_name=lexicon_name)

View File

@ -18,18 +18,18 @@ bp = Blueprint("characters", __name__, url_prefix="/characters", template_folder
@bp.get("/") @bp.get("/")
@lexicon_param @lexicon_param
@player_required @player_required
def list(name): def list(lexicon_name):
return render_template("characters.jinja", name=name) return render_template("characters.jinja", lexicon_name=lexicon_name)
@bp.route("/edit/<uuid:character_id>", methods=["GET", "POST"]) @bp.route("/edit/<uuid:character_id>", methods=["GET", "POST"])
@lexicon_param @lexicon_param
@player_required @player_required
def edit(name, character_id: uuid.UUID): def edit(lexicon_name, character_id: uuid.UUID):
character: Optional[Character] = charq.try_from_public_id(g.db, character_id) character: Optional[Character] = charq.try_from_public_id(g.db, character_id)
if not character: if not character:
flash("Character not found") flash("Character not found")
return redirect(url_for("lexicon.characters.list", name=name)) return redirect(url_for("lexicon.characters.list", lexicon_name=lexicon_name))
form = CharacterCreateForm() form = CharacterCreateForm()
@ -37,7 +37,12 @@ def edit(name, character_id: uuid.UUID):
# GET # GET
form.name.data = character.name form.name.data = character.name
form.signature.data = character.signature form.signature.data = character.signature
return render_template("characters.edit.jinja", character=character, form=form) return render_template(
"characters.edit.jinja",
lexicon_name=lexicon_name,
character=character,
form=form,
)
else: else:
# POST # POST
@ -46,24 +51,33 @@ def edit(name, character_id: uuid.UUID):
character.name = form.name.data character.name = form.name.data
character.signature = form.signature.data character.signature = form.signature.data
g.db.session.commit() g.db.session.commit()
return redirect(url_for("lexicon.characters.list", name=name)) return redirect(
url_for("lexicon.characters.list", lexicon_name=lexicon_name)
)
else: else:
# POST submitted invalid data # POST submitted invalid data
return render_template( return render_template(
"characters.edit.jinja", character=character, form=form "characters.edit.jinja",
lexicon_name=lexicon_name,
character=character,
form=form,
) )
@bp.get("/new/") @bp.get("/new/")
@lexicon_param @lexicon_param
@player_required @player_required
def new(name): def new(lexicon_name):
dummy_name = f"{current_user.username}'s new character" dummy_name = f"{current_user.username}'s new character"
dummy_signature = "~" dummy_signature = "~"
char = charq.create( char = charq.create(
g.db, g.lexicon.id, current_user.id, dummy_name, dummy_signature g.db, g.lexicon.id, current_user.id, dummy_name, dummy_signature
) )
return redirect( return redirect(
url_for("lexicon.characters.edit", name=name, character_id=char.public_id) url_for(
"lexicon.characters.edit",
lexicon_name=lexicon_name,
character_id=char.public_id,
)
) )

View File

@ -13,7 +13,7 @@
<ul class="blockitem-list"> <ul class="blockitem-list">
{% if characters|map(attribute="user_id")|select("equalto", current_user.id)|list|count < g.lexicon.character_limit %} {% if characters|map(attribute="user_id")|select("equalto", current_user.id)|list|count < g.lexicon.character_limit %}
<li> <li>
<h3><a href="{{ url_for('lexicon.characters.new', name=name) }}">Create a new character</a></h3> <h3><a href="{{ url_for('lexicon.characters.new', lexicon_name=lexicon_name) }}">Create a new character</a></h3>
<p>You have created {{ characters|map(attribute="user_id")|select("equalto", current_user.id)|list|count }} out of {{ g.lexicon.character_limit }} allowed characters.</p> <p>You have created {{ characters|map(attribute="user_id")|select("equalto", current_user.id)|list|count }} out of {{ g.lexicon.character_limit }} allowed characters.</p>
</li> </li>
{% endif %} {% endif %}
@ -25,7 +25,7 @@
{% endif %} {% endif %}
<p>Player: {{ character.user.username }}</p> <p>Player: {{ character.user.username }}</p>
{% if character.user == current_user %} {% if character.user == current_user %}
<p><a href="{{ url_for('lexicon.characters.edit', name=g.lexicon.name, character_id=character.public_id) }}">Edit this character</a></p> <p><a href="{{ url_for('lexicon.characters.edit', lexicon_name=lexicon_name, character_id=character.public_id) }}">Edit this character</a></p>
{% endif %} {% endif %}
</li> </li>
{% endfor %} {% endfor %}

View File

@ -3,7 +3,7 @@
<div class="dashboard-lexicon-item dashboard-lexicon-{{ status }}"> <div class="dashboard-lexicon-item dashboard-lexicon-{{ status }}">
<p> <p>
<span class="dashboard-lexicon-item-title"> <span class="dashboard-lexicon-item-title">
<a href="{{ url_for('lexicon.contents', name=lexicon.name) }}">{{ lexicon.full_title }}</a> <a href="{{ url_for('lexicon.contents', lexicon_name=lexicon.name) }}">{{ lexicon.full_title }}</a>
</span> </span>
[{{ status.capitalize() }}] [{{ status.capitalize() }}]
</p> </p>
@ -29,7 +29,7 @@
Players: {{ lexicon.memberships|count }}{% if lexicon.player_limit is not none %} / {{ lexicon.player_limit }}{% endif -%} Players: {{ lexicon.memberships|count }}{% if lexicon.player_limit is not none %} / {{ lexicon.player_limit }}{% endif -%}
{%- {%-
if lexicon.public and lexicon.joinable if lexicon.public and lexicon.joinable
%} / <a href="{{ url_for('lexicon.join', name=lexicon.name) }}">Join game</a> %} / <a href="{{ url_for('lexicon.join', lexicon_name=lexicon.name) }}">Join game</a>
{%- endif -%} {%- endif -%}
{%- endif -%} {%- endif -%}
</p> </p>

View File

@ -32,12 +32,12 @@ def test_character_view(db: DbContext, app: Flask, make: ObjectFactory):
assert mem assert mem
# The character page exists # The character page exists
list_url = url_for("lexicon.characters.list", name=lexicon.name) list_url = url_for("lexicon.characters.list", lexicon_name=lexicon.name)
response = client.get(list_url) response = client.get(list_url)
assert response.status_code == 200 assert response.status_code == 200
assert charname.encode("utf8") not in response.data assert charname.encode("utf8") not in response.data
assert char_sig.encode("utf8") not in response.data assert char_sig.encode("utf8") not in response.data
new_url = url_for("lexicon.characters.new", name=lexicon.name) new_url = url_for("lexicon.characters.new", lexicon_name=lexicon.name)
assert new_url.encode("utf8") in response.data assert new_url.encode("utf8") in response.data
# The character creation endpoint works # The character creation endpoint works