context_processors.py 716 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from random import shuffle

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

    sequence = request.session['partner_sequence']
    chosen, rest = sequence[:2], sequence[2:]
    request.session['partner_sequence'] = rest + chosen

16
17
18
19
20
    try:
        partners = tuple(Partner.objects.get(id=id) for id in chosen)
    except Partner.DoesNotExist:
        del request.session['partner_sequence']
        return showcased_partners(request)
21
22

    return {'showcased_partners': partners}