7

implement conditional rendering in block

This commit is contained in:
Pim Vernooij
2016-12-12 23:29:19 +01:00
parent ed3a9449fd
commit cde821a30a

View File

@ -3,10 +3,23 @@ from personalisation.models import Segment
class PersonalisedStructBlock(blocks.StructBlock):
if Segment.objects.count() > 0:
segment = blocks.ChoiceBlock(choices=[
(segment.pk, "{} ({})".format(segment.name, segment.status) ) \
for segment in Segment.objects.all()
segment = blocks.ChoiceBlock(choices=[
(segment.pk, "{} ({})".format(segment.name, segment.status) ) \
for segment in Segment.objects.all()
], required=False, label="Personalisation segment",
help_text="Only show this content block for users in this segment")
def render(self, value, context=None):
"""Only render block when the user is in the segment the block is
meant for"""
user_segments = context['request'].session['segments']
if value['segment']:
for segment in user_segments:
if segment['id'] == int(value['segment']):
return super(PersonalisedStructBlock, self).render(value,
context)
return ""