linkding/bookmarks/queries.py

49 lines
1.4 KiB
Python
Raw Normal View History

2019-06-29 10:53:37 +00:00
from django.contrib.auth.models import User
2019-06-30 05:15:46 +00:00
from django.db.models import Q, Count, Aggregate, CharField
2019-06-29 10:53:37 +00:00
from bookmarks.models import Bookmark
2019-06-30 05:15:46 +00:00
class Concat(Aggregate):
function = 'GROUP_CONCAT'
template = '%(function)s(%(distinct)s%(expressions)s)'
def __init__(self, expression, distinct=False, **extra):
super(Concat, self).__init__(
expression,
distinct='DISTINCT ' if distinct else '',
output_field=CharField(),
**extra)
2019-06-29 10:53:37 +00:00
def query_bookmarks(user: User, query_string: str):
2019-06-30 05:15:46 +00:00
# Add aggregated tag info to bookmark instances
query_set = Bookmark.objects \
2019-06-30 06:24:21 +00:00
.annotate(tag_count=Count('tags'),
tag_string=Concat('tags__name'))
2019-06-29 10:53:37 +00:00
# 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