Verified Commit 6117f554 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Move context preprocessor to template tag

parent 40d343f8
......@@ -25,14 +25,6 @@ partners.apps module
:undoc-members:
:show-inheritance:
partners.context\_processors module
-----------------------------------
.. automodule:: partners.context_processors
:members:
:undoc-members:
:show-inheritance:
partners.emails module
----------------------
......
from random import sample
from partners.models import Partner
def showcased_partners(request):
"""
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 partners. 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[:4], sequence[4:]
request.session['partner_sequence'] = rest + chosen
return {
'showcased_partners': [p for p in all_partners if p.id in chosen],
}
from random import sample
from django import template
from partners.models import Partner
register = template.Library()
@register.inclusion_tag('partners/banners.html', takes_context=True)
def render_partner_banners(context):
request = context['request']
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[:4], sequence[4:]
request.session['partner_sequence'] = rest + chosen
return {
'partners': context['showcased_partners']
'partners': [p for p in all_partners if p.id in chosen]
}
......@@ -100,7 +100,6 @@ TEMPLATES = [
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'partners.context_processors.showcased_partners',
'announcements.context_processors.announcements',
],
'loaders': [
......
......@@ -120,7 +120,6 @@ TEMPLATES = [
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'partners.context_processors.showcased_partners',
'announcements.context_processors.announcements',
'thaliawebsite.context_processors.source_commit',
'thaliawebsite.context_processors.thumbnail_sizes',
......
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