Skip to content

Commit

Permalink
order favorites section by favorited date
Browse files Browse the repository at this point in the history
  • Loading branch information
facundoolano committed Jan 9, 2024
1 parent 84ec64e commit 813295e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
5 changes: 4 additions & 1 deletion feedi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,10 @@ def sorted_by(cls, user_id, ordering, start_at, **filters):
"""
query = cls._filtered_query(user_id, older_than=start_at, **filters)

if ordering == cls.ORDER_RECENCY:
if filters.get('favorited'):
return query.order_by(cls.favorited.desc())

elif ordering == cls.ORDER_RECENCY:
# reverse chronological order
return query.order_by(cls.sort_date.desc())

Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import uuid

import feedgen.feed as feedgen
Expand Down Expand Up @@ -96,3 +97,12 @@ def mock_request(url, body='', ctype='application/html'):
'Content-Type': ctype}, priority=1)
httpretty.register_uri(httpretty.GET, url, body=body, adding_headers={
'Content-Type': ctype}, priority=1)


def extract_entry_ids(response):
entry_ids_with_duplicates = re.findall(r'/entries/(\d+)', response.text)
entry_ids = []
for e in entry_ids_with_duplicates:
if e not in entry_ids:
entry_ids.append(e)
return entry_ids
30 changes: 18 additions & 12 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import datetime as dt
import re

from tests.conftest import create_feed, mock_feed, mock_request
from tests.conftest import (create_feed, extract_entry_ids, mock_feed,
mock_request)


def test_feed_add(client):
Expand Down Expand Up @@ -180,16 +181,25 @@ def test_sync_between_pages(client):

def test_favorites(client):
feed_domain = 'feed1.com'
response = create_feed(client, feed_domain, [{'title': 'my-first-article', 'date': '2023-10-01 00:00Z'},
{'title': 'my-second-article', 'date': '2023-10-10 00:00Z'}])

a2_favorite = re.search(r'/favorites/(\d+)', response.text).group(0)
response = client.put(a2_favorite)
response = create_feed(client, feed_domain, [{'title': 'my-third-article', 'date': '2023-11-10 00:00Z'},
{'title': 'my-second-article',
'date': '2023-10-10 00:00Z'},
{'title': 'my-first-article', 'date': '2023-10-01 00:00Z'}])
entry_ids = extract_entry_ids(response)

# pin the 3rd, then the 2nd
response = client.put(f'/favorites/{entry_ids[0]}')
assert response.status_code == 204
response = client.put(f'/favorites/{entry_ids[1]}')
assert response.status_code == 204

response = client.get('/favorites')
assert 'my-first-article' not in response.text
assert 'my-second-article' in response.text
assert 'my-third-article' in response.text

# 2nd appears first because most recent fav, even though it's older
assert response.text.find('my-second-article') < response.text.find('my-third-article')


def test_pinned(client):
Expand Down Expand Up @@ -320,14 +330,10 @@ def test_feed_delete(client):
assert 'plain-entry' in response.text

# pin the 1st, fav the 2nd
entry_ids_with_duplicates = re.findall(r'/entries/(\d+)', response.text)
entry_ids = []
for e in entry_ids_with_duplicates:
if e not in entry_ids:
entry_ids.append(e)

entry_ids = extract_entry_ids(response)
response = client.put('/pinned/' + entry_ids[0])
assert response.status_code == 200

response = client.put('/favorites/' + entry_ids[1])
assert response.status_code == 204

Expand Down

0 comments on commit 813295e

Please sign in to comment.