7
This repository has been archived on 2023-05-07. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cavemanon-wagtail-personali…/src/personalisation/blocks.py
2017-01-24 13:38:43 +01:00

26 lines
900 B
Python

from wagtail.wagtailcore import blocks
from personalisation.models import Segment
class PersonalisedStructBlock(blocks.StructBlock):
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 ""