Commit b18e592a authored by Joost Rijneveld's avatar Joost Rijneveld
Browse files

Merge branch '159-partner-logos-stored-in-sessions-may-be-out-of-date' into 'master'

Update session-stored partners upon changes

Closes #159

See merge request !337
parents b83242cc 0c4753de
from random import shuffle
from random import sample
from partners.models import Partner
def showcased_partners(request):
if 'partner_sequence' not in request.session:
partner_ids = [p.id for p in Partner.objects.filter(is_active=True)]
shuffle(partner_ids)
request.session['partner_sequence'] = partner_ids
"""
Generate a sequence of showcased partners (banners)
For each user we generate a sequence of banners. This sequence
is then stored for that user, because doing it randomly creates
a feel of an artificial 'bias' towards certain sponsors. Doing
it this way ensures that everyone gets an exactly fair number of
views.
"""
all_partners = Partner.objects.filter(is_active=True).order_by('id')
ids = [partner.id for partner in all_partners]
# check if partners have changed; if so, update sequence in session
if ('partner_sequence' not in request.session or
'partner_ids' not in request.session or
request.session['partner_ids'] != ids):
# Store a list of partner ids to allow checking for changes in partners
request.session['partner_ids'] = ids
request.session['partner_sequence'] = sample(ids, len(ids))
sequence = request.session['partner_sequence']
chosen, rest = sequence[:2], sequence[2:]
request.session['partner_sequence'] = rest + chosen
try:
partners = tuple(Partner.objects.get(id=id) for id in chosen)
except Partner.DoesNotExist:
del request.session['partner_sequence']
return showcased_partners(request)
return {'showcased_partners': partners}
return {
'showcased_partners': [p for p in all_partners if p.id in chosen],
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment