From a71a703cc8d8465bec37f523cd4790d96fda3624 Mon Sep 17 00:00:00 2001 From: Tomasz Knapik Date: Wed, 31 May 2017 17:16:21 +0100 Subject: [PATCH] Add example to the sandbox --- .../migrations/0003_auto_20170531_1615.py | 9 +++-- sandbox/sandbox/apps/home/models.py | 28 +++++++++++++++ .../blocks/personalised_block_template.html | 2 +- .../home/personalised_fields_page.html | 0 src/wagtail_personalisation/blocks.py | 15 +++++--- tests/unit/test_blocks.py | 36 ------------------- 6 files changed, 47 insertions(+), 43 deletions(-) rename tests/sandbox/pages/migrations/0003_personalisedfieldspage.py => sandbox/sandbox/apps/home/migrations/0003_auto_20170531_1615.py (90%) rename {tests/sandbox => sandbox/sandbox/apps/home}/templates/blocks/personalised_block_template.html (70%) rename tests/sandbox/templates/pages/page_with_personalisable_fields.html => sandbox/sandbox/apps/home/templates/home/personalised_fields_page.html (100%) delete mode 100644 tests/unit/test_blocks.py diff --git a/tests/sandbox/pages/migrations/0003_personalisedfieldspage.py b/sandbox/sandbox/apps/home/migrations/0003_auto_20170531_1615.py similarity index 90% rename from tests/sandbox/pages/migrations/0003_personalisedfieldspage.py rename to sandbox/sandbox/apps/home/migrations/0003_auto_20170531_1615.py index ef3d4995..c0394d4f 100644 --- a/tests/sandbox/pages/migrations/0003_personalisedfieldspage.py +++ b/sandbox/sandbox/apps/home/migrations/0003_auto_20170531_1615.py @@ -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 @@ -14,7 +14,7 @@ class Migration(migrations.Migration): dependencies = [ ('wagtailcore', '0033_remove_golive_expiry_help_text'), - ('pages', '0002_auto_20170531_0915'), + ('home', '0002_create_homepage'), ] operations = [ @@ -29,4 +29,9 @@ class Migration(migrations.Migration): }, 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'), + ), ] diff --git a/sandbox/sandbox/apps/home/models.py b/sandbox/sandbox/apps/home/models.py index d8af3e4e..f5ca4e10 100644 --- a/sandbox/sandbox/apps/home/models.py +++ b/sandbox/sandbox/apps/home/models.py @@ -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') + ] diff --git a/tests/sandbox/templates/blocks/personalised_block_template.html b/sandbox/sandbox/apps/home/templates/blocks/personalised_block_template.html similarity index 70% rename from tests/sandbox/templates/blocks/personalised_block_template.html rename to sandbox/sandbox/apps/home/templates/blocks/personalised_block_template.html index dde8b256..e87b1ac5 100644 --- a/tests/sandbox/templates/blocks/personalised_block_template.html +++ b/sandbox/sandbox/apps/home/templates/blocks/personalised_block_template.html @@ -1,6 +1,6 @@ {% load wagtailcore_tags %} -
+

This is a block with template.

Heading: {{ value.heading }}

diff --git a/tests/sandbox/templates/pages/page_with_personalisable_fields.html b/sandbox/sandbox/apps/home/templates/home/personalised_fields_page.html similarity index 100% rename from tests/sandbox/templates/pages/page_with_personalisable_fields.html rename to sandbox/sandbox/apps/home/templates/home/personalised_fields_page.html diff --git a/src/wagtail_personalisation/blocks.py b/src/wagtail_personalisation/blocks.py index 6136962b..3d4b2049 100644 --- a/src/wagtail_personalisation/blocks.py +++ b/src/wagtail_personalisation/blocks.py @@ -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) @@ -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): @@ -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) diff --git a/tests/unit/test_blocks.py b/tests/unit/test_blocks.py deleted file mode 100644 index 040f3489..00000000 --- a/tests/unit/test_blocks.py +++ /dev/null @@ -1,36 +0,0 @@ -from __future__ import absolute_import, unicode_literals - -import pytest - -from tests.factories.segment import SegmentFactory -from test.factories.pages import PersonalisedFieldsPageFactory -from tests.utils import render_template - -@pytest.mark.django_db -def test_render_block(rf): - SegmentFactory(name='test', persistent=True) - - request = rf.get('/') - - request.session['segments'] = [{ - "encoded_name": 'test', - "id": 1, - "timestamp": int(time.time()), - "persistent": True - }] - - PersonalisedFieldsPageFactory(body=) - - content = render_template(""" - {% load wagtail_personalisation_tags %} - {% segment name='test' %}Test{% endsegment %} - """, request=request).strip() - - assert content == "Test" - - content = render_template(""" - {% load wagtail_personalisation_tags %} - {% segment name='test2' %}Test{% endsegment %} - """, request=request).strip() - - assert content == ""