Skip to content

Commit

Permalink
Merge pull request #186 from wagtail/feature/show-to-everyone-block
Browse files Browse the repository at this point in the history
Add an option to show a personalised block to everyone (no segment)
  • Loading branch information
blurrah committed Aug 6, 2018
2 parents a47803e + 3ce0aef commit 4e9a6e9
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/wagtail_personalisation/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


def list_segment_choices():
yield -1, ("Show to everyone")
for pk, name in Segment.objects.values_list('pk', 'name'):
yield pk, name

Expand Down Expand Up @@ -35,10 +36,19 @@ def render(self, value, context=None):
adapter = get_segment_adapter(request)
user_segments = adapter.get_segments()

if value['segment']:
try:
segment_id = int(value['segment'])
except (ValueError, TypeError):
return ''

if segment_id > 0:
for segment in user_segments:
if segment.id == int(value['segment']):
if segment.id == segment_id:
return super(PersonalisedStructBlock, self).render(
value, context)

return ""
if segment_id == -1:
return super(PersonalisedStructBlock, self).render(
value, context)

return ''

0 comments on commit 4e9a6e9

Please sign in to comment.