Unverified Commit 74071d81 authored by Joost Rijneveld's avatar Joost Rijneveld
Browse files

Update session-stored partners upon changes

parent 0f324b06
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)]
request.session['partner_sequence'] = partner_ids
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):
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
partners = tuple(Partner.objects.get(id=id) for id in chosen)
partners = tuple(p for p in all_partners if p.id in chosen)
except Partner.DoesNotExist:
del request.session['partner_sequence']
return showcased_partners(request)
Supports Markdown
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