From ea26aeae58cdb4b6d5a3cc8598c4745d69d5ae0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Farka?= <stepanfarka11@gmail.com> Date: Sun, 13 Mar 2022 17:50:58 +0100 Subject: [PATCH] [WIP] migration to streamfield --- .../migrations/0043_auto_20220313_1732.py | 68 +++++++++++++++++ .../district/district_article_page.html | 4 +- .../migrations/0050_auto_20220313_1723.py | 74 +++++++++++++++++++ elections2021/models.py | 14 +++- .../elections2021_article_page.html | 4 +- .../0018_regionarticlepage_content.py | 68 +++++++++++++++++ .../templates/region/region_article_page.html | 4 +- shared/models.py | 13 +++- uniweb/migrations/0021_auto_20220313_1719.py | 68 +++++++++++++++++ .../templates/uniweb/uniweb_article_page.html | 4 +- 10 files changed, 313 insertions(+), 8 deletions(-) create mode 100644 district/migrations/0043_auto_20220313_1732.py create mode 100644 elections2021/migrations/0050_auto_20220313_1723.py create mode 100644 region/migrations/0018_regionarticlepage_content.py create mode 100644 uniweb/migrations/0021_auto_20220313_1719.py diff --git a/district/migrations/0043_auto_20220313_1732.py b/district/migrations/0043_auto_20220313_1732.py new file mode 100644 index 00000000..29a22509 --- /dev/null +++ b/district/migrations/0043_auto_20220313_1732.py @@ -0,0 +1,68 @@ +# Generated by Django 3.2.11 on 2022-03-13 16:32 +import json + +import wagtail.core.blocks +import wagtail.core.fields +from django.db import migrations +from wagtail.core.rich_text import RichText + + +def convert_data(apps, schema_editor): + DistrictArticlePage = apps.get_model("district", "DistrictArticlePage") + for post in DistrictArticlePage.objects.all(): + # edit the live post + if post.text and not post.content: + post.content = [("text", RichText(post.text))] + post.save() + + # edit drafts associated with post + if post.has_unpublished_changes: + for rev in post.revisions.all(): + data = json.loads(rev.content_json) + text = data["text"] + data["content"] = json.dumps([{"type": "text", "value": text}]) + rev.content_json = json.dumps(data) + rev.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("district", "0042_auto_20220309_1319"), + ] + + operations = [ + migrations.RemoveField( + model_name="districtarticlepage", + name="text", + ), + migrations.RunPython(convert_data), + migrations.AddField( + model_name="districtarticlepage", + name="content", + field=wagtail.core.fields.StreamField( + [ + ( + "text", + wagtail.core.blocks.RichTextBlock( + features=[ + "h2", + "h3", + "h4", + "bold", + "italic", + "ol", + "ul", + "link", + "document-link", + "image", + ], + label="Textový editor", + ), + ) + ], + blank=True, + verbose_name="Článek", + ), + ), + ] diff --git a/district/templates/district/district_article_page.html b/district/templates/district/district_article_page.html index 4a1f4eba..2a3a4e17 100644 --- a/district/templates/district/district_article_page.html +++ b/district/templates/district/district_article_page.html @@ -39,7 +39,9 @@ <div class="lg:flex mt-8 lg:space-x-16"> <div class="lg:w-2/3"> <div itemprop="description" class="content-block w-full"> - {{ page.text|richtext }} + {% for block in page.content %} + {% include_block block %} + {% endfor %} {% if page.author_page %} {% include "shared/person_badge_snippet.html" with person_page=page.author_page title="Autor" %} diff --git a/elections2021/migrations/0050_auto_20220313_1723.py b/elections2021/migrations/0050_auto_20220313_1723.py new file mode 100644 index 00000000..71a72d3f --- /dev/null +++ b/elections2021/migrations/0050_auto_20220313_1723.py @@ -0,0 +1,74 @@ +# Generated by Django 3.2.11 on 2022-03-13 16:23 +import json + +import wagtail.core.blocks +import wagtail.core.fields +from django.db import migrations +from wagtail.core.rich_text import RichText + + +def convert_data(apps, schema_editor): + Elections2021ArticlePage = apps.get_model( + "elections2021", "Elections2021ArticlePage" + ) + for post in Elections2021ArticlePage.objects.all(): + # edit the live post + if post.text and not post.content: + post.content = [("text", RichText(post.text))] + post.save() + + # edit drafts associated with post + if post.has_unpublished_changes: + for rev in post.revisions.all(): + data = json.loads(rev.content_json) + text = data["text"] + data["content"] = json.dumps([{"type": "text", "value": text}]) + rev.content_json = json.dumps(data) + rev.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("elections2021", "0049_auto_20210930_2104"), + ] + + operations = [ + migrations.AddField( + model_name="elections2021articlepage", + name="content", + field=wagtail.core.fields.StreamField( + [ + ( + "text", + wagtail.core.blocks.RichTextBlock( + features=[ + "h2", + "h3", + "h4", + "bold", + "italic", + "superscript", + "subscript", + "strikethrough", + "ul-elections2021", + "ol-elections2021", + "blockquote-elections2021", + "link", + "image", + "document-link", + ], + label="Textový editor", + ), + ) + ], + blank=True, + verbose_name="Článek", + ), + ), + migrations.RunPython(convert_data), + migrations.RemoveField( + model_name="elections2021articlepage", + name="text", + ), + ] diff --git a/elections2021/models.py b/elections2021/models.py index 4f795f26..990d730e 100644 --- a/elections2021/models.py +++ b/elections2021/models.py @@ -27,6 +27,7 @@ from wagtail.admin.edit_handlers import ( ) from wagtail.contrib.routable_page.models import RoutablePageMixin, route from wagtail.core import blocks +from wagtail.core.blocks import RichTextBlock from wagtail.core.fields import RichTextField, StreamField from wagtail.core.models import Page from wagtail.documents.blocks import DocumentChooserBlock @@ -533,7 +534,18 @@ class Elections2021ArticleTag(TaggedItemBase): class Elections2021ArticlePage(ArticleMixin, SubpageMixin, MetadataPageMixin, Page): ### FIELDS - text = RichTextField("článek", blank=True, features=ARTICLE_RICH_TEXT_FEATURES) + content = StreamField( + [ + ( + "text", + RichTextBlock( + label="Textový editor", features=ARTICLE_RICH_TEXT_FEATURES + ), + ) + ], + verbose_name="Článek", + blank=True, + ) tags = ClusterTaggableManager(through=Elections2021ArticleTag, blank=True) card_style = models.CharField( "styl karty článku", choices=STYLE_CHOICES, default=WHITE, max_length=10 diff --git a/elections2021/templates/elections2021/elections2021_article_page.html b/elections2021/templates/elections2021/elections2021_article_page.html index e82d47f8..e745a67e 100644 --- a/elections2021/templates/elections2021/elections2021_article_page.html +++ b/elections2021/templates/elections2021/elections2021_article_page.html @@ -37,7 +37,9 @@ <div class="lg:flex mt-20 lg:space-x-16 justify-center"> <div class="lg:w-2/3"> <div class="content-block w-full"> - {{ page.text|richtext }} + {% for block in page.content %} + {% include_block block %} + {% endfor %} </div> </div> </div> diff --git a/region/migrations/0018_regionarticlepage_content.py b/region/migrations/0018_regionarticlepage_content.py new file mode 100644 index 00000000..7d56101b --- /dev/null +++ b/region/migrations/0018_regionarticlepage_content.py @@ -0,0 +1,68 @@ +# Generated by Django 3.2.11 on 2022-03-13 15:44 +import json + +import wagtail.core.blocks +import wagtail.core.fields +from django.db import migrations +from wagtail.core.rich_text import RichText + + +def convert_data(apps, schema_editor): + RegionArticlePage = apps.get_model("region", "RegionArticlePage") + for post in RegionArticlePage.objects.all(): + # edit the live post + if post.text and not post.content: + post.content = [("text", RichText(post.text))] + post.save() + + # edit drafts associated with post + if post.has_unpublished_changes: + for rev in post.revisions.all(): + data = json.loads(rev.content_json) + text = data["text"] + data["content"] = json.dumps([{"type": "text", "value": text}]) + rev.content_json = json.dumps(data) + rev.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("region", "0017_auto_20220309_1357"), + ] + + operations = [ + migrations.AddField( + model_name="regionarticlepage", + name="content", + field=wagtail.core.fields.StreamField( + [ + ( + "text", + wagtail.core.blocks.RichTextBlock( + features=[ + "h2", + "h3", + "h4", + "bold", + "italic", + "ol", + "ul", + "link", + "document-link", + "image", + ], + label="Textový editor", + ), + ) + ], + blank=True, + verbose_name="Článek", + ), + ), + migrations.RunPython(convert_data), + migrations.RemoveField( + model_name="regionarticlepage", + name="text", + ), + ] diff --git a/region/templates/region/region_article_page.html b/region/templates/region/region_article_page.html index 18cc14e2..8e2bc94a 100644 --- a/region/templates/region/region_article_page.html +++ b/region/templates/region/region_article_page.html @@ -39,7 +39,9 @@ <div class="lg:flex mt-8 lg:space-x-16"> <div class="lg:w-2/3"> <div itemprop="description" class="content-block w-full"> - {{ page.text|richtext }} + {% for block in page.content %} + {% include_block block %} + {% endfor %} {% if page.author_page %} {% include "shared/person_badge_snippet.html" with person_page=page.author_page title="Autor" %} diff --git a/shared/models.py b/shared/models.py index e399811a..d9d2ac50 100644 --- a/shared/models.py +++ b/shared/models.py @@ -1,7 +1,8 @@ from django.db import models from django.utils import timezone from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel, StreamFieldPanel -from wagtail.core.fields import RichTextField, StreamField +from wagtail.core.blocks import RichTextBlock +from wagtail.core.fields import StreamField from wagtail.core.models import Page from wagtail.images.edit_handlers import ImageChooserPanel @@ -45,9 +46,14 @@ class ArticleMixin(models.Model): ### FIELDS + content = StreamField( + [("text", RichTextBlock(label="Textový editor", features=RICH_TEXT_FEATURES))], + verbose_name="Článek", + blank=True, + ) date = models.DateField("datum", default=timezone.now) perex = models.TextField("perex") - text = RichTextField("článek", blank=True, features=RICH_TEXT_FEATURES) + # text = RichTextField("článek", blank=True, features=RICH_TEXT_FEATURES) author = models.CharField("autor", max_length=250, blank=True, null=True) image = models.ForeignKey( "wagtailimages.Image", @@ -62,7 +68,8 @@ class ArticleMixin(models.Model): content_panels = Page.content_panels + [ FieldPanel("date"), FieldPanel("perex"), - FieldPanel("text"), + # FieldPanel("text"), + StreamFieldPanel("content"), FieldPanel("author"), ImageChooserPanel("image"), ] diff --git a/uniweb/migrations/0021_auto_20220313_1719.py b/uniweb/migrations/0021_auto_20220313_1719.py new file mode 100644 index 00000000..d21c077f --- /dev/null +++ b/uniweb/migrations/0021_auto_20220313_1719.py @@ -0,0 +1,68 @@ +# Generated by Django 3.2.11 on 2022-03-13 16:19 +import json + +import wagtail.core.blocks +import wagtail.core.fields +from django.db import migrations +from wagtail.core.rich_text import RichText + + +def convert_data(apps, schema_editor): + UniwebArticlePage = apps.get_model("uniweb", "UniwebArticlePage") + for post in UniwebArticlePage.objects.all(): + # edit the live post + if post.text and not post.content: + post.content = [("text", RichText(post.text))] + post.save() + + # edit drafts associated with post + if post.has_unpublished_changes: + for rev in post.revisions.all(): + data = json.loads(rev.content_json) + text = data["text"] + data["content"] = json.dumps([{"type": "text", "value": text}]) + rev.content_json = json.dumps(data) + rev.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("uniweb", "0020_auto_20220213_1210"), + ] + + operations = [ + migrations.AddField( + model_name="uniwebarticlepage", + name="content", + field=wagtail.core.fields.StreamField( + [ + ( + "text", + wagtail.core.blocks.RichTextBlock( + features=[ + "h2", + "h3", + "h4", + "bold", + "italic", + "ol", + "ul", + "link", + "document-link", + "image", + ], + label="Textový editor", + ), + ) + ], + blank=True, + verbose_name="Článek", + ), + ), + migrations.RunPython(convert_data), + migrations.RemoveField( + model_name="uniwebarticlepage", + name="text", + ), + ] diff --git a/uniweb/templates/uniweb/uniweb_article_page.html b/uniweb/templates/uniweb/uniweb_article_page.html index 93f5fac3..a3b2cb71 100644 --- a/uniweb/templates/uniweb/uniweb_article_page.html +++ b/uniweb/templates/uniweb/uniweb_article_page.html @@ -40,7 +40,9 @@ <div class="lg:flex mt-8 lg:space-x-16"> <div itemprop="description" class="content-block w-full"> - {{ page.text|richtext }} + {% for block in page.content %} + {% include_block block %} + {% endfor %} </div> </div> </article> -- GitLab