2022-08-04 17:37:16 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2019-06-29 10:53:37 +00:00
|
|
|
from django.contrib.auth.models import User
|
2022-09-09 17:46:55 +00:00
|
|
|
from django.db.models import Q, QuerySet
|
2019-06-29 10:53:37 +00:00
|
|
|
|
2019-06-30 19:15:02 +00:00
|
|
|
from bookmarks.models import Bookmark, Tag
|
2021-01-02 10:30:20 +00:00
|
|
|
from bookmarks.utils import unique
|
2019-06-29 10:53:37 +00:00
|
|
|
|
|
|
|
|
2021-02-14 17:00:22 +00:00
|
|
|
def query_bookmarks(user: User, query_string: str) -> QuerySet:
|
|
|
|
return _base_bookmarks_query(user, query_string) \
|
|
|
|
.filter(is_archived=False)
|
|
|
|
|
|
|
|
|
|
|
|
def query_archived_bookmarks(user: User, query_string: str) -> QuerySet:
|
|
|
|
return _base_bookmarks_query(user, query_string) \
|
|
|
|
.filter(is_archived=True)
|
|
|
|
|
|
|
|
|
2022-08-04 17:37:16 +00:00
|
|
|
def query_shared_bookmarks(user: Optional[User], query_string: str) -> QuerySet:
|
|
|
|
return _base_bookmarks_query(user, query_string) \
|
|
|
|
.filter(shared=True) \
|
|
|
|
.filter(owner__profile__enable_sharing=True)
|
|
|
|
|
|
|
|
|
|
|
|
def _base_bookmarks_query(user: Optional[User], query_string: str) -> QuerySet:
|
2022-09-09 17:46:55 +00:00
|
|
|
query_set = Bookmark.objects
|
2019-06-29 10:53:37 +00:00
|
|
|
|
|
|
|
# Filter for user
|
2022-08-04 17:37:16 +00:00
|
|
|
if user:
|
|
|
|
query_set = query_set.filter(owner=user)
|
2019-06-29 10:53:37 +00:00
|
|
|
|
2019-06-30 17:54:33 +00:00
|
|
|
# Split query into search terms and tags
|
2022-08-04 18:31:24 +00:00
|
|
|
query = parse_query_string(query_string)
|
2019-06-30 17:54:33 +00:00
|
|
|
|
|
|
|
# Filter for search terms and tags
|
2019-06-30 19:15:02 +00:00
|
|
|
for term in query['search_terms']:
|
2019-06-30 17:54:33 +00:00
|
|
|
query_set = query_set.filter(
|
2023-02-20 21:49:08 +00:00
|
|
|
Q(title__icontains=term)
|
|
|
|
| Q(description__icontains=term)
|
|
|
|
| Q(website_title__icontains=term)
|
|
|
|
| Q(website_description__icontains=term)
|
|
|
|
| Q(url__icontains=term)
|
2019-06-30 17:54:33 +00:00
|
|
|
)
|
|
|
|
|
2019-06-30 19:15:02 +00:00
|
|
|
for tag_name in query['tag_names']:
|
2019-06-29 10:53:37 +00:00
|
|
|
query_set = query_set.filter(
|
2021-01-02 10:30:20 +00:00
|
|
|
tags__name__iexact=tag_name
|
2019-06-29 10:53:37 +00:00
|
|
|
)
|
|
|
|
|
2022-05-21 07:50:51 +00:00
|
|
|
# Untagged bookmarks
|
|
|
|
if query['untagged']:
|
|
|
|
query_set = query_set.filter(
|
|
|
|
tags=None
|
|
|
|
)
|
2022-07-23 20:17:20 +00:00
|
|
|
# Unread bookmarks
|
|
|
|
if query['unread']:
|
|
|
|
query_set = query_set.filter(
|
|
|
|
unread=True
|
|
|
|
)
|
2022-05-21 07:50:51 +00:00
|
|
|
|
2021-03-31 07:08:19 +00:00
|
|
|
# Sort by date added
|
|
|
|
query_set = query_set.order_by('-date_added')
|
2019-06-29 10:53:37 +00:00
|
|
|
|
|
|
|
return query_set
|
2019-06-30 19:15:02 +00:00
|
|
|
|
|
|
|
|
2021-02-14 17:00:22 +00:00
|
|
|
def query_bookmark_tags(user: User, query_string: str) -> QuerySet:
|
2021-08-15 07:28:40 +00:00
|
|
|
bookmarks_query = query_bookmarks(user, query_string)
|
2021-02-14 17:00:22 +00:00
|
|
|
|
2021-08-15 07:28:40 +00:00
|
|
|
query_set = Tag.objects.filter(bookmark__in=bookmarks_query)
|
2021-02-14 17:00:22 +00:00
|
|
|
|
2021-08-15 07:28:40 +00:00
|
|
|
return query_set.distinct()
|
2019-12-26 22:51:52 +00:00
|
|
|
|
2019-06-30 19:15:02 +00:00
|
|
|
|
2021-08-15 07:28:40 +00:00
|
|
|
def query_archived_bookmark_tags(user: User, query_string: str) -> QuerySet:
|
|
|
|
bookmarks_query = query_archived_bookmarks(user, query_string)
|
2019-06-30 19:15:02 +00:00
|
|
|
|
2021-08-15 07:28:40 +00:00
|
|
|
query_set = Tag.objects.filter(bookmark__in=bookmarks_query)
|
2019-06-30 19:15:02 +00:00
|
|
|
|
|
|
|
return query_set.distinct()
|
|
|
|
|
|
|
|
|
2022-08-04 17:37:16 +00:00
|
|
|
def query_shared_bookmark_tags(user: Optional[User], query_string: str) -> QuerySet:
|
|
|
|
bookmarks_query = query_shared_bookmarks(user, query_string)
|
|
|
|
|
|
|
|
query_set = Tag.objects.filter(bookmark__in=bookmarks_query)
|
|
|
|
|
|
|
|
return query_set.distinct()
|
|
|
|
|
|
|
|
|
|
|
|
def query_shared_bookmark_users(query_string: str) -> QuerySet:
|
|
|
|
bookmarks_query = query_shared_bookmarks(None, query_string)
|
|
|
|
|
|
|
|
query_set = User.objects.filter(bookmark__in=bookmarks_query)
|
|
|
|
|
|
|
|
return query_set.distinct()
|
|
|
|
|
|
|
|
|
2019-12-27 11:32:44 +00:00
|
|
|
def get_user_tags(user: User):
|
|
|
|
return Tag.objects.filter(owner=user).all()
|
|
|
|
|
|
|
|
|
2022-08-04 18:31:24 +00:00
|
|
|
def parse_query_string(query_string):
|
2019-06-30 19:15:02 +00:00
|
|
|
# Sanitize query params
|
|
|
|
if not query_string:
|
|
|
|
query_string = ''
|
|
|
|
|
|
|
|
# Split query into search terms and tags
|
|
|
|
keywords = query_string.strip().split(' ')
|
|
|
|
keywords = [word for word in keywords if word]
|
|
|
|
|
2022-05-21 07:50:51 +00:00
|
|
|
search_terms = [word for word in keywords if word[0] != '#' and word[0] != '!']
|
2019-06-30 19:15:02 +00:00
|
|
|
tag_names = [word[1:] for word in keywords if word[0] == '#']
|
2021-01-02 10:30:20 +00:00
|
|
|
tag_names = unique(tag_names, str.lower)
|
2019-06-30 19:15:02 +00:00
|
|
|
|
2022-05-21 07:50:51 +00:00
|
|
|
# Special search commands
|
|
|
|
untagged = '!untagged' in keywords
|
2022-07-23 20:17:20 +00:00
|
|
|
unread = '!unread' in keywords
|
2022-05-21 07:50:51 +00:00
|
|
|
|
2019-06-30 19:15:02 +00:00
|
|
|
return {
|
|
|
|
'search_terms': search_terms,
|
|
|
|
'tag_names': tag_names,
|
2022-05-21 07:50:51 +00:00
|
|
|
'untagged': untagged,
|
2022-07-23 20:17:20 +00:00
|
|
|
'unread': unread,
|
2019-06-30 19:15:02 +00:00
|
|
|
}
|