Skip to content

Commit

Permalink
Improve filtering for top rated sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Nov 30, 2023
1 parent 842319f commit e518a02
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions general/management/commands/create_front_page_caches.py
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ def handle(self, **options):
NUM_ITEMS_PER_SECTION = 9

last_week = get_n_weeks_back_datetime(n_weeks=1) # Use later to filter queries
last_two_weeks = get_n_weeks_back_datetime(n_weeks=2) # Use later to filter queries

cache_time = 24 * 60 * 60 # 1 day cache time
# NOTE: The specific cache time is not important as long as it is bigger than the frequency with which we run
@@ -90,16 +91,17 @@ def handle(self, **options):
random.shuffle(trending_new_pack_ids) # Randomize the order of the packs
cache.set("trending_new_pack_ids", trending_new_pack_ids[0:NUM_ITEMS_PER_SECTION], cache_time)

# Generate top rated new sounds cache (top rated sounds from those created last week)
# Generate top rated new sounds cache (top rated sounds from those created last two weeks)
# Note we use two weeks here instead of one to make sure we have enough sounds to choose from
top_rated_new_sound_ids = Sound.public.select_related('license', 'user') \
.annotate(greatest_date=Greatest('created', 'moderation_date')) \
.filter(greatest_date__gte=last_week).exclude(is_explicit=True) \
.filter(greatest_date__gte=last_two_weeks).exclude(is_explicit=True) \
.filter(num_ratings__gt=settings.MIN_NUMBER_RATINGS) \
.order_by("-avg_rating", "-num_ratings").values_list('id', flat=True)[0:NUM_ITEMS_PER_SECTION * 5]
top_rated_new_sound_ids = list(top_rated_new_sound_ids)
random.shuffle(top_rated_new_sound_ids) # Randomize the order of the sounds
cache.set("top_rated_new_sound_ids", top_rated_new_sound_ids[0:NUM_ITEMS_PER_SECTION], cache_time)


# Generate latest "random sound of the day" ids
recent_random_sound_ids = [sd.sound_id for sd in SoundOfTheDay.objects.filter(date_display__lt=datetime.datetime.today()).order_by('-date_display')[:NUM_ITEMS_PER_SECTION]]
cache.set("recent_random_sound_ids", list(recent_random_sound_ids), cache_time)
8 changes: 4 additions & 4 deletions templates/front.html
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ <h3 class="v-spacing-4">Latest additions</h3>
{% if top_rated_new_sound_ids %}
{% cache 300 bw_front_page_top_rated_new_sound_ids %}
<section class="v-spacing-3">
<h3 class="v-spacing-4 v-spacing-top-3" title="Top rated sounds uploaded during the last week">Top rated new sounds</h3>
<h3 class="v-spacing-4 v-spacing-top-3" title="Top rated sounds uploaded during the last days">Top rated new sounds</h3>
{% sound_carousel top_rated_new_sound_ids %}
</section>
<hr />
@@ -61,7 +61,7 @@ <h3 class="v-spacing-4 v-spacing-top-3" title="Top rated sounds uploaded during
{% if trending_new_sound_ids %}
{% cache 300 bw_front_page_trending_new_sounds %}
<section class="v-spacing-3">
<h3 class="v-spacing-4 v-spacing-top-3" title="Most downloaded sounds uploaded during the last week">Most downloaded new sounds</h3>
<h3 class="v-spacing-4 v-spacing-top-3" title="Most downloaded sounds uploaded during the last days">Most downloaded new sounds</h3>
{% sound_carousel trending_new_sound_ids %}
</section>
<hr />
@@ -71,7 +71,7 @@ <h3 class="v-spacing-4 v-spacing-top-3" title="Most downloaded sounds uploaded d
{% if trending_new_pack_ids %}
{% cache 300 bw_front_page_trending_new_packs %}
<section class="v-spacing-3">
<h3 class="v-spacing-4 v-spacing-top-3" title="Most downloaded packs created during the last week">Most downloaded new packs</h3>
<h3 class="v-spacing-4 v-spacing-top-3" title="Most downloaded packs created during the last days">Most downloaded new packs</h3>
{% pack_carousel trending_new_pack_ids %}
</section>
<hr />
@@ -81,7 +81,7 @@ <h3 class="v-spacing-4 v-spacing-top-3" title="Most downloaded packs created dur
{% if trending_sound_ids %}
{% cache 300 bw_front_page_trending_sounds %}
<section class="v-spacing-3">
<h3 class="v-spacing-4 v-spacing-top-3" title="Sounds most downloaded during the last week">Most recently downloaded sounds</h3>
<h3 class="v-spacing-4 v-spacing-top-3" title="Sounds most downloaded during the last days">Most recently downloaded sounds</h3>
{% sound_carousel trending_sound_ids %}
</section>
<hr />

0 comments on commit e518a02

Please sign in to comment.