- Fix sorting posts by rule
This commit is contained in:
Sonny Bakker 2020-08-31 22:42:18 +02:00
parent c94158a3a6
commit 30bd140483
3 changed files with 17 additions and 6 deletions

View file

@ -11,7 +11,9 @@ export const filterPostsByRule = (rule = {}, posts = []) => {
const filteredData = filteredPosts.map(post => ({ ...post, rule: { ...rule } }));
return filteredData.length > 0 ? [...filteredData] : [];
return filteredData.sort((firstPost, secondPost) => {
return new Date(secondPost.publicationDate) - new Date(firstPost.publicationDate);
});
};
export const filterPostsByCategory = (category = {}, rules = [], posts = []) => {

View file

@ -165,7 +165,12 @@ class NestedRuleListViewTestCase(TestCase):
def test_pagination(self):
rule = FeedFactory.create(user=self.user)
FeedPostFactory.create_batch(size=80, rule=rule)
posts = sorted(
FeedPostFactory.create_batch(size=80, rule=rule),
key=lambda post: post.publication_date,
reverse=True,
)
response = self.client.get(
reverse("api:news:collection:rules-nested-posts", kwargs={"pk": rule.pk}),
@ -177,6 +182,10 @@ class NestedRuleListViewTestCase(TestCase):
self.assertEquals(data["count"], 80)
self.assertEquals(len(data["results"]), 30)
self.assertEquals(
[post["id"] for post in data["results"]], [post.id for post in posts[:30]]
)
def test_empty(self):
rule = FeedFactory.create(user=self.user)