2023-10-27 17:59:06 +00:00
|
|
|
import unicodedata
|
2022-07-23 21:20:27 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from django.contrib.syndication.views import Feed
|
|
|
|
from django.db.models import QuerySet
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
from bookmarks import queries
|
2023-10-27 17:59:06 +00:00
|
|
|
from bookmarks.models import Bookmark, BookmarkSearch, FeedToken
|
2022-07-23 21:20:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class FeedContext:
|
|
|
|
feed_token: FeedToken
|
|
|
|
query_set: QuerySet[Bookmark]
|
|
|
|
|
|
|
|
|
2023-10-27 17:59:06 +00:00
|
|
|
def sanitize(text: str):
|
2023-11-04 08:56:06 +00:00
|
|
|
if not text:
|
|
|
|
return ''
|
2023-10-27 17:59:06 +00:00
|
|
|
# remove control characters
|
|
|
|
valid_chars = ['\n', '\r', '\t']
|
|
|
|
return ''.join(ch for ch in text if ch in valid_chars or unicodedata.category(ch)[0] != 'C')
|
|
|
|
|
|
|
|
|
2022-07-23 21:20:27 +00:00
|
|
|
class BaseBookmarksFeed(Feed):
|
|
|
|
def get_object(self, request, feed_key: str):
|
|
|
|
feed_token = FeedToken.objects.get(key__exact=feed_key)
|
2023-10-01 19:22:44 +00:00
|
|
|
search = BookmarkSearch(q=request.GET.get('q', ''))
|
2023-09-01 20:48:21 +00:00
|
|
|
query_set = queries.query_bookmarks(feed_token.user, feed_token.user.profile, search)
|
2022-07-23 21:20:27 +00:00
|
|
|
return FeedContext(feed_token, query_set)
|
|
|
|
|
|
|
|
def item_title(self, item: Bookmark):
|
2023-10-27 17:59:06 +00:00
|
|
|
return sanitize(item.resolved_title)
|
2022-07-23 21:20:27 +00:00
|
|
|
|
|
|
|
def item_description(self, item: Bookmark):
|
2023-10-27 17:59:06 +00:00
|
|
|
return sanitize(item.resolved_description)
|
2022-07-23 21:20:27 +00:00
|
|
|
|
|
|
|
def item_link(self, item: Bookmark):
|
|
|
|
return item.url
|
|
|
|
|
|
|
|
def item_pubdate(self, item: Bookmark):
|
|
|
|
return item.date_added
|
|
|
|
|
|
|
|
|
|
|
|
class AllBookmarksFeed(BaseBookmarksFeed):
|
|
|
|
title = 'All bookmarks'
|
|
|
|
description = 'All bookmarks'
|
|
|
|
|
|
|
|
def link(self, context: FeedContext):
|
|
|
|
return reverse('bookmarks:feeds.all', args=[context.feed_token.key])
|
|
|
|
|
|
|
|
def items(self, context: FeedContext):
|
|
|
|
return context.query_set
|
|
|
|
|
|
|
|
|
|
|
|
class UnreadBookmarksFeed(BaseBookmarksFeed):
|
|
|
|
title = 'Unread bookmarks'
|
|
|
|
description = 'All unread bookmarks'
|
|
|
|
|
|
|
|
def link(self, context: FeedContext):
|
|
|
|
return reverse('bookmarks:feeds.unread', args=[context.feed_token.key])
|
|
|
|
|
|
|
|
def items(self, context: FeedContext):
|
|
|
|
return context.query_set.filter(unread=True)
|