Skip to content

Commit

Permalink
Add example to the sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
tm-kn committed May 31, 2017
1 parent 1c201b7 commit f5af8e8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
37 changes: 37 additions & 0 deletions sandbox/sandbox/apps/home/migrations/0003_auto_20170531_1615.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-31 16:15
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import wagtail.wagtailcore.blocks
import wagtail.wagtailcore.fields
import wagtail.wagtailimages.blocks
import wagtail_personalisation.blocks


class Migration(migrations.Migration):

dependencies = [
('wagtailcore', '0033_remove_golive_expiry_help_text'),
('home', '0002_create_homepage'),
]

operations = [
migrations.CreateModel(
name='PersonalisedFieldsPage',
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')),
('body', wagtail.wagtailcore.fields.StreamField((('personalised_block', wagtail.wagtailcore.blocks.StructBlock((('segment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=wagtail_personalisation.blocks.list_segment_choices, help_text='Only show this content block for users in this segment', label='Personalisation segment', required=False)), ('heading', wagtail.wagtailcore.blocks.CharBlock()), ('paragraph', wagtail.wagtailcore.blocks.RichTextBlock())))), ('personalised_block_template', wagtail.wagtailcore.blocks.StructBlock((('segment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=wagtail_personalisation.blocks.list_segment_choices, help_text='Only show this content block for users in this segment', label='Personalisation segment', required=False)), ('heading', wagtail.wagtailcore.blocks.CharBlock()), ('paragraph', wagtail.wagtailcore.blocks.RichTextBlock())), label='Block with template', template='blocks/personalised_block_template.html')), ('personalised_rich_text_block', wagtail.wagtailcore.blocks.StructBlock((('segment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=wagtail_personalisation.blocks.list_segment_choices, help_text='Only show this content block for users in this segment', label='Personalisation segment', required=False)), ('rich_text', wagtail.wagtailcore.blocks.RichTextBlock(label='Rich Text'))))), ('personalised_image', wagtail.wagtailcore.blocks.StructBlock((('segment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=wagtail_personalisation.blocks.list_segment_choices, help_text='Only show this content block for users in this segment', label='Personalisation segment', required=False)), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock(label='Image'))))), ('personalised_char', wagtail.wagtailcore.blocks.StructBlock((('segment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=wagtail_personalisation.blocks.list_segment_choices, help_text='Only show this content block for users in this segment', label='Personalisation segment', required=False)), ('char', wagtail.wagtailcore.blocks.CharBlock(label='Text'))))), ('personalised_text', wagtail.wagtailcore.blocks.StructBlock((('segment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=wagtail_personalisation.blocks.list_segment_choices, help_text='Only show this content block for users in this segment', label='Personalisation segment', required=False)), ('text', wagtail.wagtailcore.blocks.TextBlock(label='Mutli-line Text')))))))),
],
options={
'abstract': False,
},
bases=('wagtailcore.page',),
),
migrations.AlterField(
model_name='homepage',
name='segment',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='wagtail_personalisation.Segment'),
),
]
28 changes: 28 additions & 0 deletions sandbox/sandbox/apps/home/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
from __future__ import absolute_import, unicode_literals

from django.utils.translation import ugettext_lazy as _

from wagtail.wagtailadmin.edit_handlers import StreamFieldPanel
from wagtail.wagtailcore import blocks, fields
from wagtail.wagtailcore.models import Page

from wagtail_personalisation.blocks import (PersonalisedCharBlock,
PersonalisedImageChooserBlock, PersonalisedRichTextBlock,
PersonalisedStructBlock, PersonalisedTextBlock)
from wagtail_personalisation.models import PersonalisablePageMixin


class HomePage(Page, PersonalisablePageMixin):
pass


class PersonalisedFieldsPage(Page):
body = fields.StreamField([
('personalised_block', PersonalisedStructBlock([
('heading', blocks.CharBlock()),
('paragraph', blocks.RichTextBlock())
], render_fields=['heading', 'paragraph'])),
('personalised_block_template', PersonalisedStructBlock([
('heading', blocks.CharBlock()),
('paragraph', blocks.RichTextBlock())
], template='blocks/personalised_block_template.html', label=_('Block with template'))),
('personalised_rich_text_block', PersonalisedRichTextBlock()),
('personalised_image', PersonalisedImageChooserBlock()),
('personalised_char', PersonalisedCharBlock()),
('personalised_text', PersonalisedTextBlock()),
])

content_panels = Page.content_panels + [
StreamFieldPanel('body')
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% load wagtailcore_tags %}

<div class="personalisation-block-template" style="background-color: cornsilk;">
<p>This is a block with <strong>template</strong>.</p>
<h2>Heading: {{ value.heading }}</h2>
<div>
{{ value.paragraph|richtext }}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}
{% load wagtailcore_tags %}

{% block body_class %}template-homepage{% endblock %}

{% block content %}
<h1>{{ page.title }}</h1>
{% for block in page.body %}
<section class="section-{{ block.block_type }}">
<p><em>Section for {{ block.block_type }}.</em></p>
{% include_block block %}
</section>
<hr>
{% endfor %}
{% endblock %}
15 changes: 11 additions & 4 deletions src/wagtail_personalisation/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, *args, **kwargs):
self._meta_class.render_fields)
super(BasePersonalisedStructBlock, self).__init__(*args, **kwargs)

# Check "render_fields" are either a list or None.
if isinstance(render_fields, tuple):
render_fields = list(render_fields)

Expand All @@ -51,7 +52,13 @@ def __init__(self, *args, **kwargs):
raise ValueError('"render_fields" has to contain name(s) of the '
'specified blocks.')
else:
setattr(self._meta_class, 'render_fields', render_fields)
setattr(self.meta, 'render_fields', render_fields)

# Template can be used only when "render_fields" is set to None.
if self.meta.render_fields is not None \
and getattr(self.meta, 'template', None):
raise ValueError('"render_fields" has to be set to None when using '
'template.')


def is_visible(self, value, request):
Expand Down Expand Up @@ -87,13 +94,13 @@ def render(self, value, context=None):
if not self.is_visible(value, context['request']):
return ""

if self._meta_class.render_fields is None:
if self.meta.render_fields is None:
return super(BasePersonalisedStructBlock, self).render(
value, context)

if isinstance(self._meta_class.render_fields, list):
if isinstance(self.meta.render_fields, list):
render_value = ''
for field_name in self._meta_class.render_fields:
for field_name in self.meta.render_fields:
if hasattr(value.bound_blocks[field_name], 'render_as_block'):
block_value = value.bound_blocks[field_name] \
.render_as_block(context=context)
Expand Down

0 comments on commit f5af8e8

Please sign in to comment.