Browse Source

Modify 404 group page query

Now uses the 'word_similarity' function (and uses it by name) and
enforces a minimum match on the data in addition to the max 3 limit.
merge-requests/28/head
Celeo 7 years ago
parent
commit
889ec1beca
  1. 2
      tildes/tildes/templates/error_group_not_found.jinja2
  2. 13
      tildes/tildes/views/exceptions.py

2
tildes/tildes/templates/error_group_not_found.jinja2

@ -9,7 +9,7 @@ Group not found
{% block content %}
<div class="empty">
<h2 class="empty-title">No group by that name</h2>
<h2 class="empty-title">No group named '{{ supplied_name }}'</h2>
{% if group_suggestions %}
<p class="empty-subtitle">
Did you mean one of these groups instead?

13
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}
Loading…
Cancel
Save