Create basic streamfield

This commit is contained in:
Jake Howard 2022-06-26 18:37:04 +01:00
parent 1a30ce54ff
commit b7163c3076
Signed by: jake
GPG key ID: 57AFB45680EDD477
6 changed files with 146 additions and 2 deletions

View file

@ -0,0 +1,54 @@
# Generated by Django 4.0.5 on 2022-06-26 17:35
import wagtail.blocks
import wagtail.embeds.blocks
import wagtail.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("blog", "0003_blogpostpage_date"),
]
operations = [
migrations.AddField(
model_name="bloglistpage",
name="body",
field=wagtail.fields.StreamField(
[
("embed", wagtail.embeds.blocks.EmbedBlock()),
("rich_text", wagtail.blocks.RichTextBlock()),
(
"lorem",
wagtail.blocks.StructBlock(
[("paragraphs", wagtail.blocks.IntegerBlock(min_value=1))]
),
),
("html", wagtail.blocks.RawHTMLBlock()),
],
blank=True,
use_json_field=True,
),
),
migrations.AddField(
model_name="blogpostpage",
name="body",
field=wagtail.fields.StreamField(
[
("embed", wagtail.embeds.blocks.EmbedBlock()),
("rich_text", wagtail.blocks.RichTextBlock()),
(
"lorem",
wagtail.blocks.StructBlock(
[("paragraphs", wagtail.blocks.IntegerBlock(min_value=1))]
),
),
("html", wagtail.blocks.RawHTMLBlock()),
],
blank=True,
use_json_field=True,
),
),
]

View file

@ -1,11 +1,13 @@
{% extends "wagtail_base.html" %} {% extends "wagtail_base.html" %}
{% load wagtailcore_tags %}
{% block content %} {% block content %}
{% include "common/hero.html" %} {% include "common/hero.html" %}
<section class="container content"> <section class="container content">
{% lorem 10 p %} {% include_block page.body %}
</section> </section>
{% endblock content %} {% endblock content %}

View file

@ -0,0 +1,54 @@
# Generated by Django 4.0.5 on 2022-06-26 17:35
import wagtail.blocks
import wagtail.embeds.blocks
import wagtail.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("common", "0004_listingpage"),
]
operations = [
migrations.AddField(
model_name="contentpage",
name="body",
field=wagtail.fields.StreamField(
[
("embed", wagtail.embeds.blocks.EmbedBlock()),
("rich_text", wagtail.blocks.RichTextBlock()),
(
"lorem",
wagtail.blocks.StructBlock(
[("paragraphs", wagtail.blocks.IntegerBlock(min_value=1))]
),
),
("html", wagtail.blocks.RawHTMLBlock()),
],
blank=True,
use_json_field=True,
),
),
migrations.AddField(
model_name="listingpage",
name="body",
field=wagtail.fields.StreamField(
[
("embed", wagtail.embeds.blocks.EmbedBlock()),
("rich_text", wagtail.blocks.RichTextBlock()),
(
"lorem",
wagtail.blocks.StructBlock(
[("paragraphs", wagtail.blocks.IntegerBlock(min_value=1))]
),
),
("html", wagtail.blocks.RawHTMLBlock()),
],
blank=True,
use_json_field=True,
),
),
]

View file

@ -4,9 +4,11 @@ from django.db import models
from django.http.request import HttpRequest from django.http.request import HttpRequest
from django.utils.functional import cached_property, classproperty from django.utils.functional import cached_property, classproperty
from wagtail.admin.panels import FieldPanel from wagtail.admin.panels import FieldPanel
from wagtail.fields import StreamField
from wagtail.images import get_image_model_string from wagtail.images import get_image_model_string
from wagtail.models import Page from wagtail.models import Page
from .streamfield import get_blocks
from .utils import TocEntry, get_table_of_contents from .utils import TocEntry, get_table_of_contents
@ -34,10 +36,12 @@ class BaseContentMixin(models.Model):
hero_image = models.ForeignKey( hero_image = models.ForeignKey(
get_image_model_string(), null=True, blank=True, on_delete=models.SET_NULL get_image_model_string(), null=True, blank=True, on_delete=models.SET_NULL
) )
body = StreamField(get_blocks(), blank=True, use_json_field=True)
content_panels = [ content_panels = [
FieldPanel("subtitle"), FieldPanel("subtitle"),
FieldPanel("hero_image"), FieldPanel("hero_image"),
FieldPanel("body"),
] ]
class Meta: class Meta:

View file

@ -0,0 +1,28 @@
from django.utils import lorem_ipsum
from django.utils.html import format_html_join
from wagtail import blocks
from wagtail.embeds.blocks import EmbedBlock
class LoremBlock(blocks.StructBlock):
paragraphs = blocks.IntegerBlock(min_value=1)
def render(self, value: dict, context: dict) -> str:
return format_html_join(
"\n\n",
"<p>{}</p>",
[(paragraph,) for paragraph in lorem_ipsum.paragraphs(value["paragraphs"])],
)
class Meta:
icon = "openquote"
label = "Lorem Ipsum"
def get_blocks() -> list[tuple[str, blocks.BaseBlock]]:
return [
("embed", EmbedBlock()),
("rich_text", blocks.RichTextBlock()),
("lorem", LoremBlock()),
("html", blocks.RawHTMLBlock()),
]

View file

@ -1,11 +1,13 @@
{% extends "wagtail_base.html" %} {% extends "wagtail_base.html" %}
{% load wagtailcore_tags %}
{% block content %} {% block content %}
{% include "common/hero.html" %} {% include "common/hero.html" %}
<section class="container content"> <section class="container content">
{% lorem 10 p %} {% include_block page.body %}
</section> </section>
{% endblock content %} {% endblock content %}