Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/reader/_storage/_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,17 @@ def entry_filter(
query: Query, filter: EntryFilter, keyword: str = 'WHERE'
) -> dict[str, Any]:
add = getattr(query, keyword)
feed_url, entry_id, read, important, has_enclosures, source_url, tags, feed_tags = (
filter
)
(
feed_url,
entry_id,
read,
important,
has_enclosures,
source_url,
tags,
feed_tags,
added_before,
) = filter

context = {}

Expand Down Expand Up @@ -569,6 +577,10 @@ def entry_filter(
add("json_extract(entries.source, '$.url') = :source_url")
context['source_url'] = source_url

if added_before is not None:
add("entries.first_updated < :added_before")
context['added_before'] = adapt_datetime(added_before)

context.update(entry_tags_filter(query, tags, keyword=keyword))
context.update(feed_tags_filter(query, feed_tags, 'entries.feed', keyword=keyword))

Expand Down
8 changes: 8 additions & 0 deletions src/reader/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ class EntryFilter(NamedTuple):
source: str | None = None
tags: TagFilter = ()
feed_tags: TagFilter = ()
added_before: datetime | None = None

@classmethod
def from_args(
Expand All @@ -604,6 +605,7 @@ def from_args(
source: FeedInput | None = None,
tags: TagFilterInput = None,
feed_tags: TagFilterInput = None,
added_before: datetime | None = None,
) -> Self:
feed_url = _feed_argument(feed) if feed is not None else None

Expand All @@ -625,6 +627,11 @@ def from_args(
tag_filter = tag_filter_argument(tags)
feed_tag_filter = tag_filter_argument(feed_tags, 'feed_tags')

if added_before is not None:
if not isinstance(added_before, datetime):
raise ValueError("added_before must be a datetime")
added_before = added_before.astimezone(timezone.utc)

return cls(
feed_url,
entry_id,
Expand All @@ -634,6 +641,7 @@ def from_args(
source_url,
tag_filter,
feed_tag_filter,
added_before,
)


Expand Down
68 changes: 64 additions & 4 deletions src/reader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ def get_entries(
source: FeedInput | None = None,
tags: TagFilterInput = None,
feed_tags: TagFilterInput = None,
added_before: datetime | None = None,
sort: EntrySort = EntrySort.RECENT,
limit: int | None = None,
starting_after: EntryInput | None = None,
Expand All @@ -1101,6 +1102,9 @@ def get_entries(
source: Only return the entries for this source.
tags: Only return entries matching these tags.
feed_tags: Only return entries from feeds matching these tags.
added_before: Only return entries added before this date and time.
Naive datetimes are normalized by passing them to
:meth:`~datetime.datetime.astimezone`.
sort: How to order entries.
limit: A limit on the number of entries to be returned;
by default, all entries are returned.
Expand Down Expand Up @@ -1137,13 +1141,24 @@ def get_entries(
Raise exception for invalid ``starting_after`` eagerly,
before the iterable is consumed.

.. versionadded:: 3.28
The ``added_before`` keyword argument.

"""

# If we ever implement pagination, consider following the guidance in
# https://specs.openstack.org/openstack/api-wg/guidelines/pagination_filter_sort.html

filter = EntryFilter.from_args(
feed, entry, read, important, has_enclosures, source, tags, feed_tags
feed,
entry,
read,
important,
has_enclosures,
source,
tags,
feed_tags,
added_before,
)
sort = EntrySort(sort)

Expand Down Expand Up @@ -1208,6 +1223,7 @@ def get_entry_counts(
source: FeedInput | None = None,
tags: TagFilterInput = None,
feed_tags: TagFilterInput = None,
added_before: datetime | None = None,
) -> EntryCounts:
"""Count all or some of the entries.

Expand All @@ -1220,6 +1236,9 @@ def get_entry_counts(
source: Only count the entries for this source.
tags: Only count entries matching these tags.
feed_tags: Only count entries from feeds matching these tags.
added_before: Only count entries added before this date and time.
Naive datetimes are normalized by passing them to
:meth:`~datetime.datetime.astimezone`.

Returns:
EntryCounts:
Expand All @@ -1238,10 +1257,21 @@ def get_entry_counts(
.. versionadded:: 3.16
The ``source`` keyword argument.

.. versionadded:: 3.28
The ``added_before`` keyword argument.

"""

filter = EntryFilter.from_args(
feed, entry, read, important, has_enclosures, source, tags, feed_tags
feed,
entry,
read,
important,
has_enclosures,
source,
tags,
feed_tags,
added_before,
)
now = self._now()
return self._storage.get_entry_counts(now, filter)
Expand Down Expand Up @@ -1678,6 +1708,7 @@ def search_entries(
source: FeedInput | None = None,
tags: TagFilterInput = None,
feed_tags: TagFilterInput = None,
added_before: datetime | None = None,
sort: EntrySearchSort = EntrySearchSort.RELEVANT,
limit: int | None = None,
starting_after: EntryInput | None = None,
Expand Down Expand Up @@ -1725,6 +1756,9 @@ def search_entries(
source: Only search the entries for this source.
tags: Only search entries matching these tags.
feed_tags: Only search entries from feeds matching these tags.
added_before: Only search entries added before this date and time.
Naive datetimes are normalized by passing them to
:meth:`~datetime.datetime.astimezone`.
sort: How to order results.
limit: A limit on the number of results to be returned;
by default, all results are returned.
Expand Down Expand Up @@ -1767,9 +1801,20 @@ def search_entries(
Raise exceptions for invalid ``query`` and ``starting_after`` eagerly,
before the iterable is consumed.

.. versionadded:: 3.28
The ``added_before`` keyword argument.

"""
filter = EntryFilter.from_args(
feed, entry, read, important, has_enclosures, source, tags, feed_tags
feed,
entry,
read,
important,
has_enclosures,
source,
tags,
feed_tags,
added_before,
)
sort = EntrySearchSort(sort)

Expand Down Expand Up @@ -1798,6 +1843,7 @@ def search_entry_counts(
source: FeedInput | None = None,
tags: TagFilterInput = None,
feed_tags: TagFilterInput = None,
added_before: datetime | None = None,
) -> EntrySearchCounts:
"""Count entries matching a full-text search query.

Expand All @@ -1815,6 +1861,9 @@ def search_entry_counts(
source: Only count the entries for this source.
tags: Only count entries matching these tags.
feed_tags: Only count entries from feeds matching these tags.
added_before: Only count entries added before this date and time.
Naive datetimes are normalized by passing them to
:meth:`~datetime.datetime.astimezone`.

Returns:
EntrySearchCounts:
Expand All @@ -1839,10 +1888,21 @@ def search_entry_counts(
.. versionadded:: 3.16
The ``source`` keyword argument.

.. versionadded:: 3.28
The ``added_before`` keyword argument.

"""

filter = EntryFilter.from_args(
feed, entry, read, important, has_enclosures, source, tags, feed_tags
feed,
entry,
read,
important,
has_enclosures,
source,
tags,
feed_tags,
added_before,
)
now = self._now()
return self._search.search_entry_counts(query, now, filter)
Expand Down
15 changes: 13 additions & 2 deletions tests/test_reader_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,18 @@ def reader_entries():
read = parser.entry(1, 'read')
important = parser.entry(1, 'important')
unimportant = parser.entry(1, 'unimportant')
enclosures = parser.entry(1, 'enclosures', enclosures=[Enclosure('http://e2')])
two = parser.feed(2)
source = parser.entry(2, 'source', source=EntrySource(url='source'))

reader.add_feed(one)
reader.add_feed(two)

reader._now = lambda: datetime(2010, 1, 1)
reader.update_feeds()

enclosures = parser.entry(1, 'enclosures', enclosures=[Enclosure('http://e2')])
source = parser.entry(2, 'source', source=EntrySource(url='source'))

reader._now = lambda: datetime(2010, 1, 2)
reader.update_feeds()
reader.update_search()

Expand Down Expand Up @@ -217,6 +223,10 @@ def reader_entries():
(dict(entry=('inexistent', 'idem')), set()),
(dict(source='source'), {'source'}),
(dict(source=Feed('source')), {'source'}),
(dict(added_before=None), ALL_IDS),
(dict(added_before=datetime(2010, 1, 1)), set()),
(dict(added_before=datetime(2010, 1, 2)), ALL_IDS - {'source', 'enclosures'}),
(dict(added_before=datetime(2010, 1, 3)), ALL_IDS),
],
ids=kwargs_ids,
)
Expand All @@ -238,6 +248,7 @@ def test_entries(reader_entries, get_entries, kwargs, expected):
dict(feed=object()),
dict(entry=object()),
dict(source=object()),
dict(added_before=object()),
],
)
def test_entries_error(reader, get_entries, kwargs):
Expand Down
Loading