Add uuid werkzeug route converter for character routes #19

Merged
Jaculabilis merged 1 commits from tvb/guid-converter into develop 2021-09-03 01:46:52 +00:00
3 changed files with 26 additions and 9 deletions
Showing only changes of commit 3a9b016320 - Show all commits

View File

@ -9,6 +9,7 @@ from amanuensis.config import AmanuensisConfig, CommandLineConfig
from amanuensis.db import DbContext from amanuensis.db import DbContext
from amanuensis.parser import filesafe_title from amanuensis.parser import filesafe_title
import amanuensis.server.auth as auth import amanuensis.server.auth as auth
from amanuensis.server.helpers import UuidConverter
import amanuensis.server.home as home import amanuensis.server.home as home
import amanuensis.server.lexicon as lexicon import amanuensis.server.lexicon as lexicon
@ -75,6 +76,9 @@ def get_app(
app.template_filter("articlelink")(article_link) app.template_filter("articlelink")(article_link)
app.context_processor(include_backend) app.context_processor(include_backend)
# Set up uuid route converter
app.url_map.converters["uuid"] = UuidConverter
# Set up Flask-Login # Set up Flask-Login
auth.get_login_manager().init_app(app) auth.get_login_manager().init_app(app)

View File

@ -1,13 +1,31 @@
from functools import wraps from functools import wraps
from typing import Optional from typing import Optional, Any
from uuid import UUID
from flask import g, flash, redirect, url_for from flask import g, flash, redirect, url_for
from flask_login import current_user from flask_login import current_user
from werkzeug.routing import BaseConverter, ValidationError
from amanuensis.backend import lexiq, memq from amanuensis.backend import lexiq, memq
from amanuensis.db import DbContext, Lexicon, User, Membership from amanuensis.db import DbContext, Lexicon, User, Membership
class UuidConverter(BaseConverter):
"""Converter that matches version 4 UUIDs"""
regex = r"[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-4[0-9A-Fa-f]{3}-[89aAbB][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}"
def to_python(self, value: str) -> Any:
try:
return UUID(value)
except:
return ValidationError(f"Invalid UUID: {value}")
def to_url(self, value: Any) -> str:
if not isinstance(value, UUID):
raise ValueError(f"Expected UUID, got {type(value)}: {value}")
return str(value)
def lexicon_param(route): def lexicon_param(route):
""" """
Wrapper for loading a route's lexicon to `g`. Wrapper for loading a route's lexicon to `g`.

View File

@ -22,16 +22,11 @@ def list(name):
return render_template("characters.jinja", name=name) return render_template("characters.jinja", name=name)
@bp.route("/edit/<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): def edit(name, character_id: uuid.UUID):
try: character: Optional[Character] = charq.try_from_public_id(g.db, character_id)
char_uuid = uuid.UUID(character_id)
except:
flash("Character not found")
return redirect(url_for("lexicon.characters.list", name=name))
character: Optional[Character] = charq.try_from_public_id(g.db, char_uuid)
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", name=name))