Skip to content
Snippets Groups Projects
Commit 3382c07d authored by Štěpán Farka's avatar Štěpán Farka
Browse files

[FIX] format

parent ea26aeae
No related branches found
No related tags found
2 merge requests!436Release,!434Feature/misc enhancements 2
Pipeline #7265 passed
# Generated by Django 3.2.11 on 2022-03-13 16:32 # Generated by Django 4.0.3 on 2022-03-18 09:33
# -*- coding: utf-8 -*-
import json import json
import wagtail.core.blocks import wagtail.core.blocks
import wagtail.core.fields import wagtail.core.fields
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations from django.db import migrations
from wagtail.core.rich_text import RichText from wagtail.core.rich_text import RichText
def convert_data(apps, schema_editor): def page_to_streamfield(page):
changed = False
if page.text and not page.content:
page.content = [("text", RichText(page.text))]
changed = True
return page, changed
def pagerevision_to_streamfield(revision_data):
changed = False
body = revision_data.get("text")
if body:
try:
json.loads(body)
except ValueError:
revision_data["content"] = json.dumps(
[{"value": body, "type": "text"}], cls=DjangoJSONEncoder
)
changed = True
else:
# It's already valid JSON. Leave it.
pass
return revision_data, changed
def page_to_richtext(page):
changed = False
if page.content.raw_text is None:
raw_text = "".join(
[child.value.source for child in page.content if child.block_type == "text"]
)
page.text = raw_text
changed = True
return page, changed
def pagerevision_to_richtext(revision_data):
changed = False
body = revision_data.get("content", "definitely non-JSON string")
if body:
try:
body_data = json.loads(body)
except ValueError:
# It's not apparently a StreamField. Leave it.
pass
else:
raw_text = "".join(
[child["value"] for child in body_data if child["type"] == "text"]
)
revision_data["text"] = raw_text
changed = True
return revision_data, changed
def convert(apps, schema_editor, page_converter, pagerevision_converter):
DistrictArticlePage = apps.get_model("district", "DistrictArticlePage") DistrictArticlePage = apps.get_model("district", "DistrictArticlePage")
for post in DistrictArticlePage.objects.all(): for page in DistrictArticlePage.objects.all():
# edit the live post
if post.text and not post.content: page, changed = page_converter(page)
post.content = [("text", RichText(post.text))] if changed:
post.save() page.save()
# edit drafts associated with post for revision in page.revisions.all():
if post.has_unpublished_changes: revision_data = json.loads(revision.content_json)
for rev in post.revisions.all(): revision_data, changed = pagerevision_converter(revision_data)
data = json.loads(rev.content_json) if changed:
text = data["text"] revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
data["content"] = json.dumps([{"type": "text", "value": text}]) revision.save()
rev.content_json = json.dumps(data)
rev.save()
def convert_to_streamfield(apps, schema_editor):
return convert(
apps, schema_editor, page_to_streamfield, pagerevision_to_streamfield
)
def convert_to_richtext(apps, schema_editor):
return convert(apps, schema_editor, page_to_richtext, pagerevision_to_richtext)
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
("district", "0042_auto_20220309_1319"), ("district", "0043_alter_districtarticletag_tag_and_more"),
] ]
operations = [ operations = [
migrations.RemoveField(
model_name="districtarticlepage",
name="text",
),
migrations.RunPython(convert_data),
migrations.AddField( migrations.AddField(
model_name="districtarticlepage", model_name="districtarticlepage",
name="content", name="content",
...@@ -65,4 +125,12 @@ class Migration(migrations.Migration): ...@@ -65,4 +125,12 @@ class Migration(migrations.Migration):
verbose_name="Článek", verbose_name="Článek",
), ),
), ),
migrations.RunPython(
convert_to_streamfield,
convert_to_richtext,
),
migrations.RemoveField(
model_name="districtarticlepage",
name="text",
),
] ]
# Generated by Django 3.2.11 on 2022-03-13 16:23 # Generated by Django 3.2.11 on 2022-03-13 16:23
# -*- coding: utf-8 -*-
import json import json
import wagtail.core.blocks import wagtail.core.blocks
import wagtail.core.fields import wagtail.core.fields
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations from django.db import migrations
from wagtail.core.rich_text import RichText from wagtail.core.rich_text import RichText
def convert_data(apps, schema_editor): def page_to_streamfield(page):
changed = False
if page.text and not page.content:
page.content = [("text", RichText(page.text))]
changed = True
return page, changed
def pagerevision_to_streamfield(revision_data):
changed = False
body = revision_data.get("text")
if body:
try:
json.loads(body)
except ValueError:
revision_data["content"] = json.dumps(
[{"value": body, "type": "text"}], cls=DjangoJSONEncoder
)
changed = True
else:
# It's already valid JSON. Leave it.
pass
return revision_data, changed
def page_to_richtext(page):
changed = False
if page.content.raw_text is None:
raw_text = "".join(
[child.value.source for child in page.content if child.block_type == "text"]
)
page.text = raw_text
changed = True
return page, changed
def pagerevision_to_richtext(revision_data):
changed = False
body = revision_data.get("content", "definitely non-JSON string")
if body:
try:
body_data = json.loads(body)
except ValueError:
# It's not apparently a StreamField. Leave it.
pass
else:
raw_text = "".join(
[child["value"] for child in body_data if child["type"] == "text"]
)
revision_data["text"] = raw_text
changed = True
return revision_data, changed
def convert(apps, schema_editor, page_converter, pagerevision_converter):
Elections2021ArticlePage = apps.get_model( Elections2021ArticlePage = apps.get_model(
"elections2021", "Elections2021ArticlePage" "elections2021", "Elections2021ArticlePage"
) )
for post in Elections2021ArticlePage.objects.all(): for page in Elections2021ArticlePage.objects.all():
# edit the live post
if post.text and not post.content: page, changed = page_converter(page)
post.content = [("text", RichText(post.text))] if changed:
post.save() page.save()
# edit drafts associated with post for revision in page.revisions.all():
if post.has_unpublished_changes: revision_data = json.loads(revision.content_json)
for rev in post.revisions.all(): revision_data, changed = pagerevision_converter(revision_data)
data = json.loads(rev.content_json) if changed:
text = data["text"] revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
data["content"] = json.dumps([{"type": "text", "value": text}]) revision.save()
rev.content_json = json.dumps(data)
rev.save()
def convert_to_streamfield(apps, schema_editor):
return convert(
apps, schema_editor, page_to_streamfield, pagerevision_to_streamfield
)
def convert_to_richtext(apps, schema_editor):
return convert(apps, schema_editor, page_to_richtext, pagerevision_to_richtext)
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
("elections2021", "0049_auto_20210930_2104"), ("elections2021", "0050_alter_elections2021articletag_tag"),
] ]
operations = [ operations = [
...@@ -66,7 +131,10 @@ class Migration(migrations.Migration): ...@@ -66,7 +131,10 @@ class Migration(migrations.Migration):
verbose_name="Článek", verbose_name="Článek",
), ),
), ),
migrations.RunPython(convert_data), migrations.RunPython(
convert_to_streamfield,
convert_to_richtext,
),
migrations.RemoveField( migrations.RemoveField(
model_name="elections2021articlepage", model_name="elections2021articlepage",
name="text", name="text",
......
# Generated by Django 3.2.11 on 2022-03-13 15:44 # Generated by Django 3.2.11 on 2022-03-13 15:44
# -*- coding: utf-8 -*-
import json import json
import wagtail.core.blocks import wagtail.core.blocks
import wagtail.core.fields import wagtail.core.fields
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations from django.db import migrations
from wagtail.core.rich_text import RichText from wagtail.core.rich_text import RichText
def convert_data(apps, schema_editor): def page_to_streamfield(page):
changed = False
if page.text and not page.content:
page.content = [("text", RichText(page.text))]
changed = True
return page, changed
def pagerevision_to_streamfield(revision_data):
changed = False
body = revision_data.get("text")
if body:
try:
json.loads(body)
except ValueError:
revision_data["content"] = json.dumps(
[{"value": body, "type": "text"}], cls=DjangoJSONEncoder
)
changed = True
else:
# It's already valid JSON. Leave it.
pass
return revision_data, changed
def page_to_richtext(page):
changed = False
if page.content.raw_text is None:
raw_text = "".join(
[child.value.source for child in page.content if child.block_type == "text"]
)
page.text = raw_text
changed = True
return page, changed
def pagerevision_to_richtext(revision_data):
changed = False
body = revision_data.get("content", "definitely non-JSON string")
if body:
try:
body_data = json.loads(body)
except ValueError:
# It's not apparently a StreamField. Leave it.
pass
else:
raw_text = "".join(
[child["value"] for child in body_data if child["type"] == "text"]
)
revision_data["text"] = raw_text
changed = True
return revision_data, changed
def convert(apps, schema_editor, page_converter, pagerevision_converter):
RegionArticlePage = apps.get_model("region", "RegionArticlePage") RegionArticlePage = apps.get_model("region", "RegionArticlePage")
for post in RegionArticlePage.objects.all(): for page in RegionArticlePage.objects.all():
# edit the live post
if post.text and not post.content: page, changed = page_converter(page)
post.content = [("text", RichText(post.text))] if changed:
post.save() page.save()
# edit drafts associated with post for revision in page.revisions.all():
if post.has_unpublished_changes: revision_data = json.loads(revision.content_json)
for rev in post.revisions.all(): revision_data, changed = pagerevision_converter(revision_data)
data = json.loads(rev.content_json) if changed:
text = data["text"] revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
data["content"] = json.dumps([{"type": "text", "value": text}]) revision.save()
rev.content_json = json.dumps(data)
rev.save()
def convert_to_streamfield(apps, schema_editor):
return convert(
apps, schema_editor, page_to_streamfield, pagerevision_to_streamfield
)
def convert_to_richtext(apps, schema_editor):
return convert(apps, schema_editor, page_to_richtext, pagerevision_to_richtext)
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
("region", "0017_auto_20220309_1357"), ("region", "0018_alter_regionarticletag_tag_alter_regionpersontag_tag"),
] ]
operations = [ operations = [
...@@ -60,7 +125,10 @@ class Migration(migrations.Migration): ...@@ -60,7 +125,10 @@ class Migration(migrations.Migration):
verbose_name="Článek", verbose_name="Článek",
), ),
), ),
migrations.RunPython(convert_data), migrations.RunPython(
convert_to_streamfield,
convert_to_richtext,
),
migrations.RemoveField( migrations.RemoveField(
model_name="regionarticlepage", model_name="regionarticlepage",
name="text", name="text",
......
...@@ -53,7 +53,6 @@ class ArticleMixin(models.Model): ...@@ -53,7 +53,6 @@ class ArticleMixin(models.Model):
) )
date = models.DateField("datum", default=timezone.now) date = models.DateField("datum", default=timezone.now)
perex = models.TextField("perex") perex = models.TextField("perex")
# text = RichTextField("článek", blank=True, features=RICH_TEXT_FEATURES)
author = models.CharField("autor", max_length=250, blank=True, null=True) author = models.CharField("autor", max_length=250, blank=True, null=True)
image = models.ForeignKey( image = models.ForeignKey(
"wagtailimages.Image", "wagtailimages.Image",
...@@ -68,7 +67,6 @@ class ArticleMixin(models.Model): ...@@ -68,7 +67,6 @@ class ArticleMixin(models.Model):
content_panels = Page.content_panels + [ content_panels = Page.content_panels + [
FieldPanel("date"), FieldPanel("date"),
FieldPanel("perex"), FieldPanel("perex"),
# FieldPanel("text"),
StreamFieldPanel("content"), StreamFieldPanel("content"),
FieldPanel("author"), FieldPanel("author"),
ImageChooserPanel("image"), ImageChooserPanel("image"),
......
# Generated by Django 3.2.11 on 2022-03-13 16:19 # Generated by Django 3.2.11 on 2022-03-13 16:19
# -*- coding: utf-8 -*-
import json import json
import wagtail.core.blocks import wagtail.core.blocks
import wagtail.core.fields import wagtail.core.fields
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations from django.db import migrations
from wagtail.core.rich_text import RichText from wagtail.core.rich_text import RichText
def convert_data(apps, schema_editor): def page_to_streamfield(page):
changed = False
if page.text and not page.content:
page.content = [("text", RichText(page.text))]
changed = True
return page, changed
def pagerevision_to_streamfield(revision_data):
changed = False
body = revision_data.get("text")
if body:
try:
json.loads(body)
except ValueError:
revision_data["content"] = json.dumps(
[{"value": body, "type": "text"}], cls=DjangoJSONEncoder
)
changed = True
else:
# It's already valid JSON. Leave it.
pass
return revision_data, changed
def page_to_richtext(page):
changed = False
if page.content.raw_text is None:
raw_text = "".join(
[child.value.source for child in page.content if child.block_type == "text"]
)
page.text = raw_text
changed = True
return page, changed
def pagerevision_to_richtext(revision_data):
changed = False
body = revision_data.get("content", "definitely non-JSON string")
if body:
try:
body_data = json.loads(body)
except ValueError:
# It's not apparently a StreamField. Leave it.
pass
else:
raw_text = "".join(
[child["value"] for child in body_data if child["type"] == "text"]
)
revision_data["text"] = raw_text
changed = True
return revision_data, changed
def convert(apps, schema_editor, page_converter, pagerevision_converter):
UniwebArticlePage = apps.get_model("uniweb", "UniwebArticlePage") UniwebArticlePage = apps.get_model("uniweb", "UniwebArticlePage")
for post in UniwebArticlePage.objects.all(): for page in UniwebArticlePage.objects.all():
# edit the live post
if post.text and not post.content: page, changed = page_converter(page)
post.content = [("text", RichText(post.text))] if changed:
post.save() page.save()
# edit drafts associated with post for revision in page.revisions.all():
if post.has_unpublished_changes: revision_data = json.loads(revision.content_json)
for rev in post.revisions.all(): revision_data, changed = pagerevision_converter(revision_data)
data = json.loads(rev.content_json) if changed:
text = data["text"] revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
data["content"] = json.dumps([{"type": "text", "value": text}]) revision.save()
rev.content_json = json.dumps(data)
rev.save()
def convert_to_streamfield(apps, schema_editor):
return convert(
apps, schema_editor, page_to_streamfield, pagerevision_to_streamfield
)
def convert_to_richtext(apps, schema_editor):
return convert(apps, schema_editor, page_to_richtext, pagerevision_to_richtext)
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
("uniweb", "0020_auto_20220213_1210"), ("uniweb", "0021_alter_uniwebarticletag_tag"),
] ]
operations = [ operations = [
...@@ -60,7 +125,10 @@ class Migration(migrations.Migration): ...@@ -60,7 +125,10 @@ class Migration(migrations.Migration):
verbose_name="Článek", verbose_name="Článek",
), ),
), ),
migrations.RunPython(convert_data), migrations.RunPython(
convert_to_streamfield,
convert_to_richtext,
),
migrations.RemoveField( migrations.RemoveField(
model_name="uniwebarticlepage", model_name="uniwebarticlepage",
name="text", name="text",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment