Create basic streamfield
This commit is contained in:
parent
1a30ce54ff
commit
b7163c3076
6 changed files with 146 additions and 2 deletions
|
@ -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,
|
||||
),
|
||||
),
|
||||
]
|
|
@ -1,11 +1,13 @@
|
|||
{% extends "wagtail_base.html" %}
|
||||
|
||||
{% load wagtailcore_tags %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
{% include "common/hero.html" %}
|
||||
|
||||
<section class="container content">
|
||||
{% lorem 10 p %}
|
||||
{% include_block page.body %}
|
||||
</section>
|
||||
|
||||
{% endblock content %}
|
||||
|
|
|
@ -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,
|
||||
),
|
||||
),
|
||||
]
|
|
@ -4,9 +4,11 @@ from django.db import models
|
|||
from django.http.request import HttpRequest
|
||||
from django.utils.functional import cached_property, classproperty
|
||||
from wagtail.admin.panels import FieldPanel
|
||||
from wagtail.fields import StreamField
|
||||
from wagtail.images import get_image_model_string
|
||||
from wagtail.models import Page
|
||||
|
||||
from .streamfield import get_blocks
|
||||
from .utils import TocEntry, get_table_of_contents
|
||||
|
||||
|
||||
|
@ -34,10 +36,12 @@ class BaseContentMixin(models.Model):
|
|||
hero_image = models.ForeignKey(
|
||||
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 = [
|
||||
FieldPanel("subtitle"),
|
||||
FieldPanel("hero_image"),
|
||||
FieldPanel("body"),
|
||||
]
|
||||
|
||||
class Meta:
|
||||
|
|
28
website/common/streamfield.py
Normal file
28
website/common/streamfield.py
Normal 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()),
|
||||
]
|
|
@ -1,11 +1,13 @@
|
|||
{% extends "wagtail_base.html" %}
|
||||
|
||||
{% load wagtailcore_tags %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
{% include "common/hero.html" %}
|
||||
|
||||
<section class="container content">
|
||||
{% lorem 10 p %}
|
||||
{% include_block page.body %}
|
||||
</section>
|
||||
|
||||
{% endblock content %}
|
||||
|
|
Loading…
Reference in a new issue