Skip to content

Commit a71a703

Browse files
committed
Add example to the sandbox
1 parent 1c201b7 commit a71a703

File tree

6 files changed

+47
-43
lines changed

6 files changed

+47
-43
lines changed

tests/sandbox/pages/migrations/0003_personalisedfieldspage.py renamed to sandbox/sandbox/apps/home/migrations/0003_auto_20170531_1615.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Generated by Django 1.11.1 on 2017-05-31 11:29
2+
# Generated by Django 1.11.1 on 2017-05-31 16:15
33
from __future__ import unicode_literals
44

55
from django.db import migrations, models
@@ -14,7 +14,7 @@ class Migration(migrations.Migration):
1414

1515
dependencies = [
1616
('wagtailcore', '0033_remove_golive_expiry_help_text'),
17-
('pages', '0002_auto_20170531_0915'),
17+
('home', '0002_create_homepage'),
1818
]
1919

2020
operations = [
@@ -29,4 +29,9 @@ class Migration(migrations.Migration):
2929
},
3030
bases=('wagtailcore.page',),
3131
),
32+
migrations.AlterField(
33+
model_name='homepage',
34+
name='segment',
35+
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='wagtail_personalisation.Segment'),
36+
),
3237
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
from __future__ import absolute_import, unicode_literals
22

3+
from django.utils.translation import ugettext_lazy as _
4+
5+
from wagtail.wagtailadmin.edit_handlers import StreamFieldPanel
6+
from wagtail.wagtailcore import blocks, fields
37
from wagtail.wagtailcore.models import Page
48

9+
from wagtail_personalisation.blocks import (PersonalisedCharBlock,
10+
PersonalisedImageChooserBlock, PersonalisedRichTextBlock,
11+
PersonalisedStructBlock, PersonalisedTextBlock)
512
from wagtail_personalisation.models import PersonalisablePageMixin
613

714

815
class HomePage(Page, PersonalisablePageMixin):
916
pass
17+
18+
19+
class PersonalisedFieldsPage(Page):
20+
body = fields.StreamField([
21+
('personalised_block', PersonalisedStructBlock([
22+
('heading', blocks.CharBlock()),
23+
('paragraph', blocks.RichTextBlock())
24+
], render_fields=['heading', 'paragraph'])),
25+
('personalised_block_template', PersonalisedStructBlock([
26+
('heading', blocks.CharBlock()),
27+
('paragraph', blocks.RichTextBlock())
28+
], template='blocks/personalised_block_template.html', label=_('Block with template'))),
29+
('personalised_rich_text_block', PersonalisedRichTextBlock()),
30+
('personalised_image', PersonalisedImageChooserBlock()),
31+
('personalised_char', PersonalisedCharBlock()),
32+
('personalised_text', PersonalisedTextBlock()),
33+
])
34+
35+
content_panels = Page.content_panels + [
36+
StreamFieldPanel('body')
37+
]

tests/sandbox/templates/blocks/personalised_block_template.html renamed to sandbox/sandbox/apps/home/templates/blocks/personalised_block_template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% load wagtailcore_tags %}
22

3-
<div class="personalisation-block-template">
3+
<div class="personalisation-block-template" style="background-color: cornsilk;">
44
<p>This is a block with <strong>template</strong>.</p>
55
<h2>Heading: {{ value.heading }}</h2>
66
<div>

tests/sandbox/templates/pages/page_with_personalisable_fields.html renamed to sandbox/sandbox/apps/home/templates/home/personalised_fields_page.html

File renamed without changes.

src/wagtail_personalisation/blocks.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(self, *args, **kwargs):
4040
self._meta_class.render_fields)
4141
super(BasePersonalisedStructBlock, self).__init__(*args, **kwargs)
4242

43+
# Check "render_fields" are either a list or None.
4344
if isinstance(render_fields, tuple):
4445
render_fields = list(render_fields)
4546

@@ -51,7 +52,13 @@ def __init__(self, *args, **kwargs):
5152
raise ValueError('"render_fields" has to contain name(s) of the '
5253
'specified blocks.')
5354
else:
54-
setattr(self._meta_class, 'render_fields', render_fields)
55+
setattr(self.meta, 'render_fields', render_fields)
56+
57+
# Template can be used only when "render_fields" is set to None.
58+
if self.meta.render_fields is not None \
59+
and getattr(self.meta, 'template', None):
60+
raise ValueError('"render_fields" has to be set to None when using '
61+
'template.')
5562

5663

5764
def is_visible(self, value, request):
@@ -87,13 +94,13 @@ def render(self, value, context=None):
8794
if not self.is_visible(value, context['request']):
8895
return ""
8996

90-
if self._meta_class.render_fields is None:
97+
if self.meta.render_fields is None:
9198
return super(BasePersonalisedStructBlock, self).render(
9299
value, context)
93100

94-
if isinstance(self._meta_class.render_fields, list):
101+
if isinstance(self.meta.render_fields, list):
95102
render_value = ''
96-
for field_name in self._meta_class.render_fields:
103+
for field_name in self.meta.render_fields:
97104
if hasattr(value.bound_blocks[field_name], 'render_as_block'):
98105
block_value = value.bound_blocks[field_name] \
99106
.render_as_block(context=context)

tests/unit/test_blocks.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)