Implement basic search
This commit is contained in:
parent
0177660cf6
commit
10b1570a64
33
bookmarks/queries.py
Normal file
33
bookmarks/queries.py
Normal file
@ -0,0 +1,33 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import Q
|
||||
|
||||
from bookmarks.models import Bookmark
|
||||
|
||||
|
||||
def query_bookmarks(user: User, query_string: str):
|
||||
query_set = Bookmark.objects
|
||||
|
||||
# Sanitize query params
|
||||
if not query_string:
|
||||
query_string = ''
|
||||
|
||||
# Filter for user
|
||||
query_set = query_set.filter(owner=user)
|
||||
|
||||
# Split query into keywords
|
||||
keywords = query_string.strip().split(' ')
|
||||
keywords = [word for word in keywords if word]
|
||||
|
||||
# Filter for each keyword
|
||||
for word in keywords:
|
||||
query_set = query_set.filter(
|
||||
Q(title__contains=word)
|
||||
| Q(description__contains=word)
|
||||
| Q(website_title__contains=word)
|
||||
| Q(website_description__contains=word)
|
||||
)
|
||||
|
||||
# Sort by modification date
|
||||
query_set = query_set.order_by('-date_modified')
|
||||
|
||||
return query_set
|
@ -1,10 +1,17 @@
|
||||
{% extends "bookmarks/layout.html" %}
|
||||
{% load shared %}
|
||||
|
||||
{% block content %}
|
||||
<div>
|
||||
<h2>Bookmarks</h2>
|
||||
<a href="{% url 'bookmarks:new' %}">Add</a>
|
||||
</div>
|
||||
<div>
|
||||
<form method="get">
|
||||
<input type="search" name="q" placeholder="Search..." value="{{ query }}">
|
||||
<input type="submit" value="Search">
|
||||
</form>
|
||||
</div>
|
||||
<ul class="bookmark-list">
|
||||
{% for bookmark in bookmarks %}
|
||||
<li>
|
||||
@ -24,10 +31,10 @@
|
||||
</ul>
|
||||
<div class="pagination">
|
||||
{% if bookmarks.has_next %}
|
||||
<a href="?page={{ bookmarks.next_page_number }}">< Older</a>
|
||||
<a href="?{% update_query_string page=bookmarks.next_page_number %}">< Older</a>
|
||||
{% endif %}
|
||||
{% if bookmarks.has_previous %}
|
||||
<a href="?page={{ bookmarks.previous_page_number }}">Newer ></a>
|
||||
<a href="?{% update_query_string page=bookmarks.previous_page_number %}">Newer ></a>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
|
14
bookmarks/templatetags/shared.py
Normal file
14
bookmarks/templatetags/shared.py
Normal file
@ -0,0 +1,14 @@
|
||||
from django import template
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def update_query_string(context, **kwargs):
|
||||
query = context['request'].GET.copy()
|
||||
|
||||
# Replace query params with the ones from tag parameters
|
||||
for key in kwargs:
|
||||
query.__setitem__(key, kwargs[key])
|
||||
|
||||
return query.urlencode()
|
@ -1,8 +1,9 @@
|
||||
from django.core.paginator import Paginator
|
||||
from django.http import HttpResponseRedirect, HttpRequest
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse
|
||||
|
||||
from bookmarks import queries
|
||||
from bookmarks.models import Bookmark, BookmarkForm
|
||||
from bookmarks.services.bookmarks import create_bookmark, update_bookmark
|
||||
|
||||
@ -11,20 +12,19 @@ _default_page_size = 30
|
||||
|
||||
def index(request):
|
||||
page = request.GET.get('page')
|
||||
paginator = Paginator(_get_bookmark_list(), _default_page_size)
|
||||
query_string = request.GET.get('q')
|
||||
query_set = queries.query_bookmarks(request.user, query_string)
|
||||
paginator = Paginator(query_set, _default_page_size)
|
||||
bookmarks = paginator.get_page(page)
|
||||
|
||||
context = {
|
||||
'bookmarks': bookmarks
|
||||
'bookmarks': bookmarks,
|
||||
'query': query_string if query_string else '',
|
||||
}
|
||||
return render(request, 'bookmarks/index.html', context)
|
||||
|
||||
|
||||
def _get_bookmark_list():
|
||||
return Bookmark.objects.order_by('-date_added')
|
||||
|
||||
|
||||
def new(request: HttpRequest):
|
||||
def new(request):
|
||||
if request.method == 'POST':
|
||||
form = BookmarkForm(request.POST)
|
||||
if form.is_valid():
|
||||
@ -38,7 +38,7 @@ def new(request: HttpRequest):
|
||||
return render(request, 'bookmarks/new.html', {'form': form})
|
||||
|
||||
|
||||
def edit(request, bookmark_id):
|
||||
def edit(request, bookmark_id: int):
|
||||
bookmark = Bookmark.objects.get(pk=bookmark_id)
|
||||
if request.method == 'POST':
|
||||
form = BookmarkForm(request.POST, instance=bookmark)
|
||||
|
Loading…
Reference in New Issue
Block a user