From bb49e319dd3c1b533abb1cfd42c3a17149ad46ed Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sat, 4 Sep 2021 09:21:05 -0700 Subject: [PATCH] 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. --- amanuensis/server/__init__.py | 2 +- amanuensis/server/helpers.py | 6 ++-- amanuensis/server/lexicon.jinja | 10 +++--- amanuensis/server/lexicon/__init__.py | 32 +++++++++++-------- .../server/lexicon/characters/__init__.py | 32 +++++++++++++------ .../lexicon/characters/characters.jinja | 4 +-- amanuensis/server/macros.jinja | 4 +-- 7 files changed, 54 insertions(+), 36 deletions(-) diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index f0f4d2f..31f0e50 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -27,7 +27,7 @@ def article_link(title): """Get the url for a lexicon by its title""" return url_for( 'lexicon.article', - name=g.lexicon.name, + lexicon_name=g.lexicon.name, title=filesafe_title(title)) diff --git a/amanuensis/server/helpers.py b/amanuensis/server/helpers.py index 06dc06c..8328ade 100644 --- a/amanuensis/server/helpers.py +++ b/amanuensis/server/helpers.py @@ -34,7 +34,7 @@ def lexicon_param(route): @wraps(route) def with_lexicon(*args, **kwargs): db: DbContext = g.db - name: str = kwargs.get('name') + name: str = kwargs.get('lexicon_name') lexicon: Optional[Lexicon] = lexiq.try_from_name(db, name) if lexicon is None: flash(f"Couldn't find a lexicon with the name \"{name}\"") @@ -71,7 +71,7 @@ def player_required(route): if not mem: flash("You must be a player to view this page") if lexicon.public: - return redirect(url_for('lexicon.contents', name=lexicon.name)) + return redirect(url_for('lexicon.contents', lexicon_name=lexicon.name)) else: return redirect(url_for('home.home')) return route(*args, **kwargs) @@ -108,6 +108,6 @@ def editor_required(route): mem: Optional[Membership] = memq.try_from_ids(db, user.id, lexicon.id) if not mem or not mem.is_editor: 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 editor_route diff --git a/amanuensis/server/lexicon.jinja b/amanuensis/server/lexicon.jinja index 70862b4..1d3e6c2 100644 --- a/amanuensis/server/lexicon.jinja +++ b/amanuensis/server/lexicon.jinja @@ -9,23 +9,23 @@ {% block sb_logo %}{% endblock %} {% block sb_characters %}Characters{% endblock %} {% block sb_contents %}Contents{% endblock %} {% block sb_rules %}Rules{% endblock %} {% block sb_session %}Session{% endblock %} {% block sb_stats %}Statistics{% endblock %} {% if current_user.is_authenticated and ( diff --git a/amanuensis/server/lexicon/__init__.py b/amanuensis/server/lexicon/__init__.py index 0d5abc6..b2fc7a2 100644 --- a/amanuensis/server/lexicon/__init__.py +++ b/amanuensis/server/lexicon/__init__.py @@ -10,14 +10,16 @@ from .characters import bp as characters_bp from .forms import LexiconJoinForm -bp = Blueprint("lexicon", __name__, url_prefix="/lexicon/", template_folder=".") +bp = Blueprint( + "lexicon", __name__, url_prefix="/lexicon/", template_folder="." +) bp.register_blueprint(characters_bp) @bp.route("/join/", methods=["GET", "POST"]) @lexicon_param @login_required -def join(name): +def join(lexicon_name): lexicon: Lexicon = g.lexicon if not lexicon.joinable: flash("This game isn't open for joining") @@ -27,7 +29,9 @@ def join(name): if not form.validate_on_submit(): # 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 # If the game is passworded, check password @@ -37,48 +41,48 @@ def join(name): ): # Bad creds, try again 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 user: User = current_user try: 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: 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/") @lexicon_param @player_required_if_not_public -def contents(name): +def contents(lexicon_name): # indexed = sort_by_index_spec(info, g.lexicon.cfg.article.index.list) # for articles in indexed.values(): # for i in range(len(articles)): # articles[i] = { # 'title': 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/") @lexicon_param @player_required_if_not_public -def article(name, title): +def article(lexicon_name, title): # 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/") @lexicon_param @player_required_if_not_public -def rules(name): - return render_template("lexicon.rules.jinja") +def rules(lexicon_name): + return render_template("lexicon.rules.jinja", lexicon_name=lexicon_name) @bp.get("/statistics/") @lexicon_param @player_required_if_not_public -def stats(name): - return render_template("lexicon.statistics.jinja") +def stats(lexicon_name): + return render_template("lexicon.statistics.jinja", lexicon_name=lexicon_name) diff --git a/amanuensis/server/lexicon/characters/__init__.py b/amanuensis/server/lexicon/characters/__init__.py index 000aa68..0a3483c 100644 --- a/amanuensis/server/lexicon/characters/__init__.py +++ b/amanuensis/server/lexicon/characters/__init__.py @@ -18,18 +18,18 @@ bp = Blueprint("characters", __name__, url_prefix="/characters", template_folder @bp.get("/") @lexicon_param @player_required -def list(name): - return render_template("characters.jinja", name=name) +def list(lexicon_name): + return render_template("characters.jinja", lexicon_name=lexicon_name) @bp.route("/edit/<uuid:character_id>", methods=["GET", "POST"]) @lexicon_param @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) if not character: 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() @@ -37,7 +37,12 @@ def edit(name, character_id: uuid.UUID): # GET form.name.data = character.name 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: # POST @@ -46,24 +51,33 @@ def edit(name, character_id: uuid.UUID): character.name = form.name.data character.signature = form.signature.data 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: # POST submitted invalid data return render_template( - "characters.edit.jinja", character=character, form=form + "characters.edit.jinja", + lexicon_name=lexicon_name, + character=character, + form=form, ) @bp.get("/new/") @lexicon_param @player_required -def new(name): +def new(lexicon_name): dummy_name = f"{current_user.username}'s new character" dummy_signature = "~" char = charq.create( g.db, g.lexicon.id, current_user.id, dummy_name, dummy_signature ) 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, + ) ) diff --git a/amanuensis/server/lexicon/characters/characters.jinja b/amanuensis/server/lexicon/characters/characters.jinja index 2d0a31c..278324e 100644 --- a/amanuensis/server/lexicon/characters/characters.jinja +++ b/amanuensis/server/lexicon/characters/characters.jinja @@ -13,7 +13,7 @@ <ul class="blockitem-list"> {% if characters|map(attribute="user_id")|select("equalto", current_user.id)|list|count < g.lexicon.character_limit %} <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> </li> {% endif %} @@ -25,7 +25,7 @@ {% endif %} <p>Player: {{ character.user.username }}</p> {% 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 %} </li> {% endfor %} diff --git a/amanuensis/server/macros.jinja b/amanuensis/server/macros.jinja index 7d06006..f361c16 100644 --- a/amanuensis/server/macros.jinja +++ b/amanuensis/server/macros.jinja @@ -3,7 +3,7 @@ <div class="dashboard-lexicon-item dashboard-lexicon-{{ status }}"> <p> <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> [{{ status.capitalize() }}] </p> @@ -29,7 +29,7 @@ Players: {{ lexicon.memberships|count }}{% if lexicon.player_limit is not none %} / {{ lexicon.player_limit }}{% endif -%} {%- 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 -%} </p>