Homepage: Randomize the position of items #215
@ -1,5 +1,6 @@
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import logging
|
import logging
|
||||||
|
import random
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
@ -43,10 +44,20 @@ class HomeView(ListedExtensionsView):
|
|||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
q = super().get_queryset().order_by('-rating_sortkey')
|
q = super().get_queryset().order_by('-rating_sortkey')
|
||||||
|
|
||||||
|
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 = {
|
context = {
|
||||||
'addons': q.filter(type=EXTENSION_TYPE_CHOICES.BPY)[:8],
|
'addons': random.sample(addons_list, addons_sample_size),
|
||||||
'themes': q.filter(type=EXTENSION_TYPE_CHOICES.THEME)[:8],
|
'themes': random.sample(themes_list, themes_sample_size),
|
||||||
}
|
}
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user