mirror of https://gitlab.com/tildes/tildes.git
Browse Source
Add extremely basic search
Add extremely basic search
Quite a few aspects of this are very hackish (especially as related to the templates and things that needed to be done to allow topic_listing.jinja2 to be inherited from for this new one), but it's a lot better than nothing.merge-requests/32/head
Deimos
6 years ago
12 changed files with 216 additions and 7 deletions
-
63tildes/alembic/versions/50c251c4a19c_add_search_column_index_for_topics.py
-
8tildes/scss/modules/_sidebar.scss
-
1tildes/scss/modules/_site-header.scss
-
15tildes/sql/init/triggers/topics/topics.sql
-
2tildes/tildes/__init__.py
-
10tildes/tildes/models/topic/topic.py
-
5tildes/tildes/models/topic/topic_query.py
-
2tildes/tildes/routes.py
-
7tildes/tildes/templates/home.jinja2
-
31tildes/tildes/templates/search.jinja2
-
22tildes/tildes/templates/topic_listing.jinja2
-
57tildes/tildes/views/topic.py
@ -0,0 +1,63 @@ |
|||
"""Add search column/index for topics |
|||
|
|||
Revision ID: 50c251c4a19c |
|||
Revises: d33fb803a153 |
|||
Create Date: 2018-08-20 19:18:04.129255 |
|||
|
|||
""" |
|||
from alembic import op |
|||
import sqlalchemy as sa |
|||
from sqlalchemy.dialects import postgresql |
|||
|
|||
# revision identifiers, used by Alembic. |
|||
revision = "50c251c4a19c" |
|||
down_revision = "d33fb803a153" |
|||
branch_labels = None |
|||
depends_on = None |
|||
|
|||
|
|||
def upgrade(): |
|||
op.add_column( |
|||
"topics", sa.Column("search_tsv", postgresql.TSVECTOR(), nullable=True) |
|||
) |
|||
op.create_index( |
|||
"ix_topics_search_tsv_gin", |
|||
"topics", |
|||
["search_tsv"], |
|||
unique=False, |
|||
postgresql_using="gin", |
|||
) |
|||
|
|||
op.execute( |
|||
""" |
|||
UPDATE topics |
|||
SET search_tsv = to_tsvector('pg_catalog.english', title) |
|||
|| to_tsvector('pg_catalog.english', COALESCE(markdown, '')); |
|||
""" |
|||
) |
|||
|
|||
op.execute( |
|||
""" |
|||
CREATE TRIGGER topic_update_search_tsv_insert |
|||
BEFORE INSERT ON topics |
|||
FOR EACH ROW |
|||
EXECUTE PROCEDURE tsvector_update_trigger(search_tsv, 'pg_catalog.english', title, markdown); |
|||
|
|||
CREATE TRIGGER topic_update_search_tsv_update |
|||
BEFORE UPDATE ON topics |
|||
FOR EACH ROW |
|||
WHEN ( |
|||
(OLD.title IS DISTINCT FROM NEW.title) |
|||
OR (OLD.markdown IS DISTINCT FROM NEW.markdown) |
|||
) |
|||
EXECUTE PROCEDURE tsvector_update_trigger(search_tsv, 'pg_catalog.english', title, markdown); |
|||
""" |
|||
) |
|||
|
|||
|
|||
def downgrade(): |
|||
op.drop_index("ix_topics_search_tsv_gin", table_name="topics") |
|||
op.drop_column("topics", "search_tsv") |
|||
|
|||
op.execute("DROP TRIGGER topic_update_search_tsv_insert ON topics") |
|||
op.execute("DROP TRIGGER topic_update_search_tsv_update ON topics") |
@ -0,0 +1,31 @@ |
|||
{% extends 'topic_listing.jinja2' %} |
|||
|
|||
{% block title %}Search results for "{{ search }}"{% endblock %} |
|||
|
|||
{% block header_context_link %}<span class="site-header-context">Search: "{{ search }}"</span>{% endblock %} |
|||
|
|||
{% block sidebar %} |
|||
<form class="form-search" method="GET" action="/search"> |
|||
<div class="input-group"> |
|||
<input type="text" class="form-input input-sm" name="q" id="q" value="{{ search }}"> |
|||
<button class="btn btn-sm input-group-btn">Search</button> |
|||
</div> |
|||
</form> |
|||
|
|||
<h2>Search results</h2> |
|||
|
|||
<p><a href="/">Back to home page</a></p> |
|||
|
|||
{% if request.user and request.user.subscriptions %} |
|||
<div class="divider"></div> |
|||
<ul class="nav"> |
|||
<li>Subscriptions</li> |
|||
<ul class="nav"> |
|||
{% for subscription in request.user.subscriptions|sort(attribute='group') %} |
|||
<li class="nav-item"><a href="/~{{ subscription.group.path }}">~{{ subscription.group.path }}</a></li> |
|||
{% endfor %} |
|||
</ul> |
|||
</ul> |
|||
{% endif %} |
|||
|
|||
{% endblock %} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue