Skip to content

Commit

Permalink
Add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Nov 2, 2024
1 parent ff7002c commit 832d5f0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
8 changes: 1 addition & 7 deletions djangocms_frontend/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

import django.core.serializers.json
import django.db.models.deletion
from cms.utils.compat import DJANGO_3_1
from django.db import migrations, models

import djangocms_frontend.fields

if DJANGO_3_1:
from django_jsonfield_backport.models import JSONField
else:
JSONField = models.JSONField


class Migration(migrations.Migration):
initial = True
Expand Down Expand Up @@ -49,7 +43,7 @@ class Migration(migrations.Migration):
),
(
"config",
JSONField(
models.JSONField(
default=dict,
encoder=django.core.serializers.json.DjangoJSONEncoder,
),
Expand Down
85 changes: 85 additions & 0 deletions djangocms_frontend/migrations/0002_migrate_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from django.db import migrations


def convert_item(config, direction):
"""Convert FrontendUIItem config to new djangocms_link format."""
if direction == "forward":
if config.get("external_link"):
if config.get("anchor"):
anchor = "#" + config.get("anchor")
del config["anchor"]
else:
anchor = ""
config["link"] = {"external_link": config.get("external_link") + anchor}
del config["external_link"]
return True
if config.get("internal_link"):
model = config.get("internal_link").get("model")
pk = config.get("internal_link").get("pk")
config["link"] = {"internal_link": f"{model}:{pk}"}
if config.get("anchor"):
config["link"]["anchor"] = "#" + config.get("anchor")
del config["anchor"]
del config["internal_link"]
return True
if config.get("file_link"):
config["link"] = {"file_link": config.get("file_link").get("pk")}
del config["file_link"]
return True
if config.get("phone"):
config["link"] = {"external_link": f"tel:{config.get('phone')}"}
del config["phone"]
return True
if config.get("mailto"):
config["link"] = {"external_link": f"mailto:{config.get('mailto')}"}
del config["mailto"]
return True
if config.get("anchor"):
config["link"] = {"external_link": "#" + config.get('anchor')}
del config["anchor"]
return True

elif direction == "backward" and config.get("link"):
link = config.get("link")
if link.get("external_link"):
if "#" in link.get("external_link"):
config["anchor"] = link.get("external_link").split("#", 1)[1]
ext = link.get("external_link").split("#", 1)[0]
if ext.startswith("tel:"):
config["phone"] = ext[4:]
elif ext.startswith("mailto:"):
config["mailto"] = ext[7:]
elif ext:
config["external_link"] = ext
elif link.get("internal_link"):
model, pk = link.get("internal_link").split(":")
config["internal_link"] = {"model": model, "pk": int(pk)}
if link.get("anchor"):
config["anchor"] = link.get("anchor")[1:]
elif link.get("file_link"):
config["file_link"] = {"model": "filer.file", "pk": int(link.get("file_link"))}
del config["link"]
return True
return False


def convert(apps, schema_editor, direction):
FrontendUIItem = apps.get_model("djangocms_frontend", "FrontendUIItem")
for item in FrontendUIItem.objects.all():
changed = convert_item(item.config, direction)
if changed:
item.save()


class Migration(migrations.Migration):
dependencies = [
("djangocms_frontend", "0001_initial"),
]

operations = [
migrations.RunPython(
lambda apps, schema_editor: convert(apps, schema_editor, "forward"),
lambda apps, schema_editor: convert(apps, schema_editor, "backward"),
elidable=True
),
]
27 changes: 27 additions & 0 deletions tests/link/test_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.test.testcases import TestCase


class LinkMigrationTestCase(TestCase):
def setUp(self):
import importlib

link_migration = importlib.import_module("djangocms_frontend.migrations.0002_migrate_links")
self.convert = staticmethod(link_migration.convert_item)

def test_link_migration(self):
test_links = (
dict(external_link="https://www.django-cms.com"),
dict(external_link="https://www.django-cms.com", anchor="top"),
dict(internal_link=dict(model="cms.page", pk=1)),
dict(internal_link=dict(model="cms.page", pk=1), anchor="top"),
dict(file_link=dict(model="filer.file", pk=1)),
dict(phone="1234567890"),
dict(mailto="[email protected]")
)

for link in test_links:
with self.subTest(link=link):
config = link.copy()
self.convert(config, "forward")
self.convert(config, "backward")
self.assertEqual(link, config)

0 comments on commit 832d5f0

Please sign in to comment.