diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 189f45c60f8f..3b7bc298366f 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -75,6 +75,7 @@ Changelog * Maintenance: Simplify code for registering page listing action buttons (Matt Westcott) * Maintenance: Removed the unused, legacy, Wagtail userbar views set up for an old iframe approach (Sage Abdullah) * Maintenance: Optimise `lru_cache` usage (Jake Howard) + * Maintenance: Implement `date_since`` in `get_most_popular`` inside `search_promotions.models.Query (TopDevPros) 5.1.2 (25.09.2023) diff --git a/docs/releases/5.2.md b/docs/releases/5.2.md index a0b1f7012565..c9660a535fc1 100644 --- a/docs/releases/5.2.md +++ b/docs/releases/5.2.md @@ -96,6 +96,7 @@ depth: 1 * Simplify code for registering page listing action buttons (Matt Westcott) * Removed the unused, legacy, Wagtail userbar views set up for an old iframe approach (Sage Abdullah) * Optimise `lru_cache` usage (Jake Howard) + * Implement `date_since`` in `get_most_popular`` inside `search_promotions.models.Query (TopDevPros) ## Upgrade considerations - changes affecting all projects diff --git a/wagtail/contrib/search_promotions/models.py b/wagtail/contrib/search_promotions/models.py index 28a0655322d5..49610caddb7b 100644 --- a/wagtail/contrib/search_promotions/models.py +++ b/wagtail/contrib/search_promotions/models.py @@ -56,10 +56,13 @@ def get(cls, query_string): @classmethod def get_most_popular(cls, date_since=None): - # TODO: Implement date_since + objects = cls.objects.filter(daily_hits__isnull=False) + + if date_since: + objects = objects.filter(daily_hits__date__gte=date_since) + return ( - cls.objects.filter(daily_hits__isnull=False) - .annotate(_hits=models.Sum("daily_hits__hits")) + objects.annotate(_hits=models.Sum("daily_hits__hits")) .distinct() .order_by("-_hits") ) diff --git a/wagtail/contrib/search_promotions/tests.py b/wagtail/contrib/search_promotions/tests.py index 168e0e3c0c4b..4d29e0b7dd09 100644 --- a/wagtail/contrib/search_promotions/tests.py +++ b/wagtail/contrib/search_promotions/tests.py @@ -78,6 +78,69 @@ def test_search_pick_ordering(self): Query.get("root page").editors_picks.last().description, "Last search pick" ) + def test_get_most_popular(self): + popularQuery = Query.get("popular") + for i in range(5): + popularQuery.add_hit() + SearchPromotion.objects.create( + query=Query.get("popular"), + page_id=2, + sort_order=0, + description="Popular search pick", + ) + SearchPromotion.objects.create( + query=Query.get("uninteresting"), + page_id=1, + sort_order=2, + description="Uninteresting search pick", + ) + + # Check + self.assertEqual(Query.get_most_popular().count(), 1) + popular_picks = Query.get("popular").editors_picks.first() + self.assertEqual( + popular_picks.description, + "Popular search pick", + ) + self.assertEqual(popular_picks.query.hits, 5) + + def test_get_most_popular_since(self): + TODAY = date.today() + TWO_DAYS_AGO = TODAY - timedelta(days=2) + FIVE_DAYS_AGO = TODAY - timedelta(days=5) + + popularQuery = Query.get("popular") + for i in range(5): + popularQuery.add_hit(date=FIVE_DAYS_AGO) + + surpriseQuery = Query.get("surprise") + surpriseQuery.add_hit(date=TODAY) + surpriseQuery.add_hit(date=TWO_DAYS_AGO) + surpriseQuery.add_hit(date=FIVE_DAYS_AGO) + SearchPromotion.objects.create( + query=Query.get("popular"), + page_id=2, + sort_order=0, + description="Popular search pick", + ) + SearchPromotion.objects.create( + query=Query.get("surprise"), + page_id=2, + sort_order=2, + description="Surprising search pick", + ) + + # Check + most_popular_queries = Query.get_most_popular(date_since=TWO_DAYS_AGO) + self.assertEqual(most_popular_queries.count(), 1) + editors_picks = Query.get("surprise").editors_picks + surprise_picks = editors_picks.first() + self.assertEqual( + surprise_picks.description, + "Surprising search pick", + ) + self.assertEqual(surprise_picks.query.hits, 3) + class TestGetSearchPromotionsTemplateTag(TestCase): def test_get_search_promotions_template_tag(self):