diff --git a/bookmarks/queries.py b/bookmarks/queries.py
new file mode 100644
index 0000000..54eccf3
--- /dev/null
+++ b/bookmarks/queries.py
@@ -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
diff --git a/bookmarks/templates/bookmarks/index.html b/bookmarks/templates/bookmarks/index.html
index bcee6bf..05c2e87 100644
--- a/bookmarks/templates/bookmarks/index.html
+++ b/bookmarks/templates/bookmarks/index.html
@@ -1,10 +1,17 @@
{% extends "bookmarks/layout.html" %}
+{% load shared %}
{% block content %}
+
+
+
{% for bookmark in bookmarks %}
-
@@ -24,10 +31,10 @@
diff --git a/bookmarks/templatetags/shared.py b/bookmarks/templatetags/shared.py
new file mode 100644
index 0000000..2e79053
--- /dev/null
+++ b/bookmarks/templatetags/shared.py
@@ -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()
diff --git a/bookmarks/views/bookmarks.py b/bookmarks/views/bookmarks.py
index adc7adb..cb82062 100644
--- a/bookmarks/views/bookmarks.py
+++ b/bookmarks/views/bookmarks.py
@@ -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)