|
|
@ -3,7 +3,7 @@ |
|
|
from pyramid.httpexceptions import HTTPNotFound |
|
|
from pyramid.httpexceptions import HTTPNotFound |
|
|
from pyramid.request import Request |
|
|
from pyramid.request import Request |
|
|
from pyramid.view import forbidden_view_config, exception_view_config |
|
|
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 |
|
|
from tildes.models.group import Group |
|
|
|
|
|
|
|
|
@ -21,12 +21,13 @@ def forbidden(request: Request) -> dict: |
|
|
def group_not_found(request: Request) -> dict: |
|
|
def group_not_found(request: Request) -> dict: |
|
|
"""Show the user a customized 404 page for group names.""" |
|
|
"""Show the user a customized 404 page for group names.""" |
|
|
request.response.status_int = 404 |
|
|
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 = ( |
|
|
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) |
|
|
.limit(3) |
|
|
.all() |
|
|
.all() |
|
|
) |
|
|
) |
|
|
return {"group_suggestions": group_suggestions} |
|
|
|
|
|
|
|
|
return {"group_suggestions": group_suggestions, "supplied_name": supplied_name} |