Implement tag search

This commit is contained in:
Sascha Ißbrücker 2019-06-30 19:54:33 +02:00
parent 4658ec9d7b
commit ff68d2591f
5 changed files with 31 additions and 11 deletions

View File

@ -29,17 +29,25 @@ def query_bookmarks(user: User, query_string: str):
# Filter for user # Filter for user
query_set = query_set.filter(owner=user) query_set = query_set.filter(owner=user)
# Split query into keywords # Split query into search terms and tags
keywords = query_string.strip().split(' ') keywords = query_string.strip().split(' ')
keywords = [word for word in keywords if word] keywords = [word for word in keywords if word]
# Filter for each keyword search_terms = [word for word in keywords if word[0] != '#']
for word in keywords: tag_names = [word[1:] for word in keywords if word[0] == '#']
# Filter for search terms and tags
for term in search_terms:
query_set = query_set.filter( query_set = query_set.filter(
Q(title__contains=word) Q(title__contains=term)
| Q(description__contains=word) | Q(description__contains=term)
| Q(website_title__contains=word) | Q(website_title__contains=term)
| Q(website_description__contains=word) | Q(website_description__contains=term)
)
for tag_name in tag_names:
query_set = query_set.filter(
tags__name=tag_name
) )
# Sort by modification date # Sort by modification date

View File

@ -1,3 +1,10 @@
.bookmarks-page {
.search input[type=search] {
width: 200px;
}
}
ul.bookmark-list { ul.bookmark-list {
list-style: none; list-style: none;

View File

@ -3,7 +3,7 @@
{% load bookmarks %} {% load bookmarks %}
{% block content %} {% block content %}
<div class="columns"> <div class="bookmarks-page columns">
{# Bookmark list #} {# Bookmark list #}
<section class="content-area column col-8"> <section class="content-area column col-8">
@ -13,7 +13,7 @@
<div class="search"> <div class="search">
<form action="{% url 'bookmarks:index' %}" method="get"> <form action="{% url 'bookmarks:index' %}" method="get">
<div class="input-group"> <div class="input-group">
<input type="search" name="q" placeholder="Search..." value="{{ query }}"> <input type="search" name="q" placeholder="Search for words or #tags" value="{{ query }}">
<input type="submit" value="Search" class="btn input-group-btn"> <input type="submit" value="Search" class="btn input-group-btn">
</div> </div>
</form> </form>
@ -29,7 +29,7 @@
{% if bookmark.tag_names %} {% if bookmark.tag_names %}
<span> <span>
{% for tag_name in bookmark.tag_names %} {% for tag_name in bookmark.tag_names %}
<a href="?{% append_query_param q=tag_name %}">#{{ tag_name }}</a> <a href="?{% append_query_param q=tag_name|hash_tag %}">{{ tag_name|hash_tag }}</a>
{% endfor %} {% endfor %}
</span> </span>
{% endif %} {% endif %}

View File

@ -5,7 +5,7 @@
<p class="group"> <p class="group">
<span class="group-label mr-2">{{ group.char }}</span> <span class="group-label mr-2">{{ group.char }}</span>
{% for tag in group.tags %} {% for tag in group.tags %}
<a href="?{% append_query_param q=tag.name %}"> <a href="?{% append_query_param q=tag.name|hash_tag %}">
<span class="mr-2">{{ tag.name }}</span> <span class="mr-2">{{ tag.name }}</span>
</a> </a>
{% endfor %} {% endfor %}

View File

@ -28,3 +28,8 @@ def append_query_param(context, **kwargs):
query.__setitem__(key, value) query.__setitem__(key, value)
return query.urlencode() return query.urlencode()
@register.filter(name='hash_tag')
def hash_tag(tag_name):
return '#' + tag_name