implement conditional rendering in block
This commit is contained in:
@ -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 ""
|
||||
|
Reference in New Issue
Block a user