Skip to content

Commit

Permalink
Implement date_since in get_most_popular() inside search_promotions.m…
Browse files Browse the repository at this point in the history
…odels

- Note: This is currently unused but could be used in the future
- Fixes wagtail#10897
  • Loading branch information
TopDevPros authored and lb- committed Oct 1, 2023
1 parent 45ffd58 commit a8bc03d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/releases/5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions wagtail/contrib/search_promotions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down
63 changes: 63 additions & 0 deletions wagtail/contrib/search_promotions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a8bc03d

Please sign in to comment.