Update post ordering for categories

By (descending) publication and rule name
This commit is contained in:
sonny 2020-06-03 20:30:30 +02:00
parent a99e3c1980
commit 35d848401e
3 changed files with 10 additions and 7 deletions

View file

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

View file

@ -92,7 +92,7 @@ class NestedPostCategoryView(ListAPIView):
queryset = Post.objects.filter( queryset = Post.objects.filter(
rule__in=category.rules.values_list("id", flat=True) rule__in=category.rules.values_list("id", flat=True)
).order_by("rule", "-publication_date") ).order_by("-publication_date", "rule__name")
return queryset return queryset

View file

@ -498,13 +498,12 @@ class NestedCategoryPostView(TestCase):
self.assertEquals(data["count"], 6) self.assertEquals(data["count"], 6)
self.assertEquals(posts[0]["title"], "Second BBC post") self.assertEquals(posts[0]["title"], "Second BBC post")
self.assertEquals(posts[1]["title"], "First BBC post") self.assertEquals(posts[1]["title"], "Second Reuters post")
self.assertEquals(posts[2]["title"], "Second Guardian post") self.assertEquals(posts[2]["title"], "Second Guardian post")
self.assertEquals(posts[3]["title"], "First Guardian post")
self.assertEquals(posts[4]["title"], "Second Reuters post") self.assertEquals(posts[3]["title"], "First BBC post")
self.assertEquals(posts[5]["title"], "First Reuters post") self.assertEquals(posts[4]["title"], "First Reuters post")
self.assertEquals(posts[5]["title"], "First Guardian post")
def test_only_posts_from_category_are_returned(self): def test_only_posts_from_category_are_returned(self):
category = CategoryFactory.create(user=self.user) category = CategoryFactory.create(user=self.user)