From 764c1c3fa1e87293094a408a25b5640e32270d9f Mon Sep 17 00:00:00 2001 From: Pablo Vazquez Date: Thu, 18 Jul 2024 16:04:18 +0200 Subject: [PATCH 1/3] Homepage: Randomize the position of items Place items randomly on every load so it is a bit more fair for all the top extensions. --- extensions/views/public.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/extensions/views/public.py b/extensions/views/public.py index 10ca934a..c942ea65 100644 --- a/extensions/views/public.py +++ b/extensions/views/public.py @@ -1,5 +1,6 @@ from collections import OrderedDict import logging +import random from django.contrib.auth import get_user_model from django.db import connection @@ -44,9 +45,14 @@ class HomeView(ListedExtensionsView): def get_context_data(self, **kwargs): q = super().get_queryset().order_by('-rating_sortkey') context = { - 'addons': q.filter(type=EXTENSION_TYPE_CHOICES.BPY)[:8], - 'themes': q.filter(type=EXTENSION_TYPE_CHOICES.THEME)[:8], + 'addons': list(q.filter(type=EXTENSION_TYPE_CHOICES.BPY)[:8]), + 'themes': list(q.filter(type=EXTENSION_TYPE_CHOICES.THEME)[:8]), } + + # Randomize items in each list. + random.shuffle(context['addons']) + random.shuffle(context['themes']) + return context -- 2.30.2 From 34dd1c4a152895d96f485f606dd963918e462601 Mon Sep 17 00:00:00 2001 From: Pablo Vazquez Date: Fri, 19 Jul 2024 12:29:42 +0200 Subject: [PATCH 2/3] Get a longer list but display a smaller sample --- extensions/views/public.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/extensions/views/public.py b/extensions/views/public.py index c942ea65..c7ba2f24 100644 --- a/extensions/views/public.py +++ b/extensions/views/public.py @@ -44,14 +44,15 @@ class HomeView(ListedExtensionsView): def get_context_data(self, **kwargs): q = super().get_queryset().order_by('-rating_sortkey') - context = { - 'addons': list(q.filter(type=EXTENSION_TYPE_CHOICES.BPY)[:8]), - 'themes': list(q.filter(type=EXTENSION_TYPE_CHOICES.THEME)[:8]), - } - # Randomize items in each list. - random.shuffle(context['addons']) - random.shuffle(context['themes']) + addons_list = list(q.filter(type=EXTENSION_TYPE_CHOICES.BPY)[:24]) + themes_list = list(q.filter(type=EXTENSION_TYPE_CHOICES.THEME)[:12]) + + # Randomize a sample of the extensions to list. + context = { + 'addons': random.sample(addons_list, 8), + 'themes': random.sample(themes_list, 4), + } return context -- 2.30.2 From 2534781d99e5b0c09a190022026f60fc9c4f5d57 Mon Sep 17 00:00:00 2001 From: Pablo Vazquez Date: Fri, 19 Jul 2024 14:39:22 +0200 Subject: [PATCH 3/3] Fix issue where sample is larger than available extensions Simply pick the minimum between the desired sample size and length of the list of available extensions. --- extensions/views/public.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/extensions/views/public.py b/extensions/views/public.py index c7ba2f24..778a1c12 100644 --- a/extensions/views/public.py +++ b/extensions/views/public.py @@ -48,10 +48,14 @@ class HomeView(ListedExtensionsView): addons_list = list(q.filter(type=EXTENSION_TYPE_CHOICES.BPY)[:24]) themes_list = list(q.filter(type=EXTENSION_TYPE_CHOICES.THEME)[:12]) + # Get 8 add-ons, 4 themes, or the minimum available. + addons_sample_size = min(8, len(addons_list)) + themes_sample_size = min(4, len(themes_list)) + # Randomize a sample of the extensions to list. context = { - 'addons': random.sample(addons_list, 8), - 'themes': random.sample(themes_list, 4), + 'addons': random.sample(addons_list, addons_sample_size), + 'themes': random.sample(themes_list, themes_sample_size), } return context -- 2.30.2