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 wagtail.core.blocks
import wagtail.core.fields
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations
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")
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()
for page in DistrictArticlePage.objects.all():
page, changed = page_converter(page)
if changed:
page.save()
for revision in page.revisions.all():
revision_data = json.loads(revision.content_json)
revision_data, changed = pagerevision_converter(revision_data)
if changed:
revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
revision.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):
dependencies = [
("district", "0042_auto_20220309_1319"),
("district", "0043_alter_districtarticletag_tag_and_more"),
]
operations = [
migrations.RemoveField(
model_name="districtarticlepage",
name="text",
),
migrations.RunPython(convert_data),
migrations.AddField(
model_name="districtarticlepage",
name="content",
......@@ -65,4 +125,12 @@ class Migration(migrations.Migration):
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
# -*- coding: utf-8 -*-
import json
import wagtail.core.blocks
import wagtail.core.fields
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations
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(
"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()
for page in Elections2021ArticlePage.objects.all():
page, changed = page_converter(page)
if changed:
page.save()
for revision in page.revisions.all():
revision_data = json.loads(revision.content_json)
revision_data, changed = pagerevision_converter(revision_data)
if changed:
revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
revision.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):
dependencies = [
("elections2021", "0049_auto_20210930_2104"),
("elections2021", "0050_alter_elections2021articletag_tag"),
]
operations = [
......@@ -66,7 +131,10 @@ class Migration(migrations.Migration):
verbose_name="Článek",
),
),
migrations.RunPython(convert_data),
migrations.RunPython(
convert_to_streamfield,
convert_to_richtext,
),
migrations.RemoveField(
model_name="elections2021articlepage",
name="text",
......
# Generated by Django 3.2.11 on 2022-03-13 15:44
# -*- coding: utf-8 -*-
import json
import wagtail.core.blocks
import wagtail.core.fields
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations
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")
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()
for page in RegionArticlePage.objects.all():
page, changed = page_converter(page)
if changed:
page.save()
for revision in page.revisions.all():
revision_data = json.loads(revision.content_json)
revision_data, changed = pagerevision_converter(revision_data)
if changed:
revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
revision.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):
dependencies = [
("region", "0017_auto_20220309_1357"),
("region", "0018_alter_regionarticletag_tag_alter_regionpersontag_tag"),
]
operations = [
......@@ -60,7 +125,10 @@ class Migration(migrations.Migration):
verbose_name="Článek",
),
),
migrations.RunPython(convert_data),
migrations.RunPython(
convert_to_streamfield,
convert_to_richtext,
),
migrations.RemoveField(
model_name="regionarticlepage",
name="text",
......
......@@ -53,7 +53,6 @@ class ArticleMixin(models.Model):
)
date = models.DateField("datum", default=timezone.now)
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)
image = models.ForeignKey(
"wagtailimages.Image",
......@@ -68,7 +67,6 @@ class ArticleMixin(models.Model):
content_panels = Page.content_panels + [
FieldPanel("date"),
FieldPanel("perex"),
# FieldPanel("text"),
StreamFieldPanel("content"),
FieldPanel("author"),
ImageChooserPanel("image"),
......
# Generated by Django 3.2.11 on 2022-03-13 16:19
# -*- coding: utf-8 -*-
import json
import wagtail.core.blocks
import wagtail.core.fields
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations
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")
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()
for page in UniwebArticlePage.objects.all():
page, changed = page_converter(page)
if changed:
page.save()
for revision in page.revisions.all():
revision_data = json.loads(revision.content_json)
revision_data, changed = pagerevision_converter(revision_data)
if changed:
revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
revision.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):
dependencies = [
("uniweb", "0020_auto_20220213_1210"),
("uniweb", "0021_alter_uniwebarticletag_tag"),
]
operations = [
......@@ -60,7 +125,10 @@ class Migration(migrations.Migration):
verbose_name="Článek",
),
),
migrations.RunPython(convert_data),
migrations.RunPython(
convert_to_streamfield,
convert_to_richtext,
),
migrations.RemoveField(
model_name="uniwebarticlepage",
name="text",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment