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 Jun 5, 2017
1 parent 543fb8a commit 912b46c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 49 deletions.
2 changes: 1 addition & 1 deletion sandbox/sandbox/apps/home/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('wagtailcore', '0033_remove_golive_expiry_help_text'),
('wagtailcore', '0001_initial'),
('wagtail_personalisation', '0011_personalisablepagemetadata'),
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-31 11:29
# Generated by Django 1.11.1 on 2017-05-31 16:15
from __future__ import unicode_literals

from django.db import migrations, models
Expand All @@ -13,8 +13,8 @@
class Migration(migrations.Migration):

dependencies = [
('wagtailcore', '0033_remove_golive_expiry_help_text'),
('pages', '0002_auto_20170531_0915'),
('wagtailcore', '0001_initial'),
('home', '0003_homepage_text_content'),
]

operations = [
Expand Down
30 changes: 27 additions & 3 deletions sandbox/sandbox/apps/home/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from __future__ import absolute_import, unicode_literals

from wagtail.wagtailadmin.edit_handlers import RichTextFieldPanel, StreamFieldPanel
from django.utils.translation import ugettext_lazy as _

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

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


class HomePage(PersonalisablePageMixin, Page):
intro = RichTextField()
Expand All @@ -21,3 +24,24 @@ class HomePage(PersonalisablePageMixin, Page):
RichTextFieldPanel('intro'),
StreamFieldPanel('body'),
]


class PersonalisedFieldsPage(Page):
body = 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
@@ -1,6 +1,6 @@
{% load wagtailcore_tags %}

<div class="personalisation-block-template">
<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>
Expand Down
19 changes: 14 additions & 5 deletions src/wagtail_personalisation/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ def __init__(self, *args, **kwargs):
Keyword Arguments:
render_fields: List with field names to be rendered or None to use
the default block rendering.
the default block rendering. Please set to None if using block
with template since then it's the template that takes care
of what fields are rendered.
"""
render_fields = kwargs.pop('render_fields',
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 +54,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 +96,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
36 changes: 0 additions & 36 deletions tests/unit/test_blocks.py

This file was deleted.

0 comments on commit 912b46c

Please sign in to comment.