diff --git a/tildes/tildes/templates/error_group_not_found.jinja2 b/tildes/tildes/templates/error_group_not_found.jinja2 index 2d01de2..ab14250 100644 --- a/tildes/tildes/templates/error_group_not_found.jinja2 +++ b/tildes/tildes/templates/error_group_not_found.jinja2 @@ -9,7 +9,7 @@ Group not found {% block content %}
-

No group by that name

+

No group named '{{ supplied_name }}'

{% if group_suggestions %}

Did you mean one of these groups instead? diff --git a/tildes/tildes/views/exceptions.py b/tildes/tildes/views/exceptions.py index b7d308c..e598f6b 100644 --- a/tildes/tildes/views/exceptions.py +++ b/tildes/tildes/views/exceptions.py @@ -3,7 +3,7 @@ from pyramid.httpexceptions import HTTPNotFound from pyramid.request import Request from pyramid.view import forbidden_view_config, exception_view_config -from sqlalchemy import text +from sqlalchemy import cast, desc, func, Text from tildes.models.group import Group @@ -21,12 +21,13 @@ def forbidden(request: Request) -> dict: def group_not_found(request: Request) -> dict: """Show the user a customized 404 page for group names.""" request.response.status_int = 404 - supplied = request.matchdict.get("group_path") + supplied_name = request.matchdict.get("group_path") + # the 'word_similarity' function here is from the 'pg_trgm' extension group_suggestions = ( - request.db_session.query(Group) - .order_by(text("ltree2text(path) <<-> :supplied")) - .params(supplied=supplied) + request.query(Group) + .filter(func.word_similarity(cast(Group.path, Text), supplied_name) > 0) + .order_by(desc(func.word_similarity(cast(Group.path, Text), supplied_name))) .limit(3) .all() ) - return {"group_suggestions": group_suggestions} + return {"group_suggestions": group_suggestions, "supplied_name": supplied_name}