Skip to content

Commit

Permalink
Fix migration
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 26, 2024
1 parent 80d8cd4 commit 3da1aef
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion djangocms_link/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class LinkField(JSONField):
"""A link is a JSON field with a default LinkFormField"""

def __init__(self, *args, **kwargs):
kwargs.setdefault("default", {})
kwargs.setdefault("default", dict)
kwargs.setdefault("blank", True)
kwargs.setdefault("help_text", "-")
super().__init__(*args, **kwargs)
Expand Down
7 changes: 4 additions & 3 deletions djangocms_link/migrations/0017_link_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

def forward(apps, schema_editor):
Link = apps.get_model("djangocms_link", "Link")
for link in Link.objects.all():
links = Link.objects.all()
for link in links:
if link.external_link:
anchor = "#" + link.anchor if link.anchor else ""
link.link = {"external_link": link.url + anchor}
link.link = {"external_link": link.external_link + anchor}
elif link.internal_link:
opt = link.internal_link._meta
link.link = {"internal_link": f"{opt.app_label}.{opt.model_name}:{link.internal_link.pk}"}
Expand All @@ -24,7 +25,7 @@ def forward(apps, schema_editor):
link.link = {"external_link": f"mailto:{link.mailto}"}
elif link.anchor:
link.link = {"external_link": "#" + link.anchor}
Link.objects.bulk_update(Link.objects.all(), ["link"])
Link.objects.bulk_update(links, ["link"])


def backward(apps, schema_editor):
Expand Down
70 changes: 70 additions & 0 deletions tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# original from
# http://tech.octopus.energy/news/2016/01/21/testing-for-missing-migrations-in-django.html
from io import StringIO
from unittest import skipIf

from django.core.management import call_command
from django.forms import model_to_dict
from django.test import TestCase, override_settings

from cms.api import create_page

from django_test_migrations.contrib.unittest_case import MigratorTestCase

from djangocms_link import __version__
from djangocms_link.helpers import get_link
from tests.helpers import get_filer_file


class MigrationTestCase(TestCase):
@override_settings(MIGRATION_MODULES={})
Expand All @@ -27,3 +37,63 @@ def test_for_missing_migrations(self):

if status_code == '1' and "djangocms_link" in output:
self.fail(f'There are missing migrations:\n {output.getvalue()}')

# @skipIf(__version__ >= '5', "Migration has already been tested before releasing version 5")
class MigrationToVersion5(MigratorTestCase):
migrate_from = ('djangocms_link', '0016_alter_link_cmsplugin_ptr')
migrate_to = ('djangocms_link', '0018_remove_link_anchor_remove_link_external_link_and_more')

def setUp(self):
self.page = model_to_dict(create_page(
title='test',
template='page.html',
language='en',
))
if hasattr(self.page, "node"):
self.node = model_to_dict(self.page.node)
self.file = model_to_dict(get_filer_file())
super().setUp()

def prepare(self):
Link = self.old_state.apps.get_model('djangocms_link', 'Link')
Page = self.old_state.apps.get_model('cms', 'Page')
File = self.old_state.apps.get_model('filer', 'File')

self.links = [
# Link.objects.create(
# template="default",
# name="My Link",
# internal_link=self.page,
# anchor="some_id",
# ),
Link.objects.create(
template="default",
name="My Link",
external_link="http://www.django-cms.com",
),
# Link.objects.create(
# template="default",
# name="My Link",
# file_link=self.file,
# ),
Link.objects.create(
template="default",
name="My Link",
mailto="[email protected]",
),
Link.objects.create(
template="default",
name="My Link",
phone="+01 234 567 89",
),
]
self.urls = ["http://www.django-cms.com", "mailto:[email protected]", "tel:+0123456789"]

def test_tags_migrated(self):
Link = self.new_state.apps.get_model('djangocms_link', 'Link')
links = Link.objects.all()

for link, url in zip(links, self.urls):
with self.subTest(link=link, url=url):
self.assertEqual(get_link(link.link), url)

0 comments on commit 3da1aef

Please sign in to comment.