|
| 1 | +from django.db import migrations |
| 2 | + |
| 3 | + |
| 4 | +def convert_item(config, direction): |
| 5 | + """Convert FrontendUIItem config to new djangocms_link format.""" |
| 6 | + if direction == "forward": |
| 7 | + if config.get("external_link"): |
| 8 | + if config.get("anchor"): |
| 9 | + anchor = "#" + config.get("anchor") |
| 10 | + del config["anchor"] |
| 11 | + else: |
| 12 | + anchor = "" |
| 13 | + config["link"] = {"external_link": config.get("external_link") + anchor} |
| 14 | + del config["external_link"] |
| 15 | + return True |
| 16 | + if config.get("internal_link"): |
| 17 | + model = config.get("internal_link").get("model") |
| 18 | + pk = config.get("internal_link").get("pk") |
| 19 | + config["link"] = {"internal_link": f"{model}:{pk}"} |
| 20 | + if config.get("anchor"): |
| 21 | + config["link"]["anchor"] = "#" + config.get("anchor") |
| 22 | + del config["anchor"] |
| 23 | + del config["internal_link"] |
| 24 | + return True |
| 25 | + if config.get("file_link"): |
| 26 | + config["link"] = {"file_link": config.get("file_link").get("pk")} |
| 27 | + del config["file_link"] |
| 28 | + return True |
| 29 | + if config.get("phone"): |
| 30 | + config["link"] = {"external_link": f"tel:{config.get('phone')}"} |
| 31 | + del config["phone"] |
| 32 | + return True |
| 33 | + if config.get("mailto"): |
| 34 | + config["link"] = {"external_link": f"mailto:{config.get('mailto')}"} |
| 35 | + del config["mailto"] |
| 36 | + return True |
| 37 | + if config.get("anchor"): |
| 38 | + config["link"] = {"external_link": "#" + config.get('anchor')} |
| 39 | + del config["anchor"] |
| 40 | + return True |
| 41 | + |
| 42 | + elif direction == "backward" and config.get("link"): |
| 43 | + link = config.get("link") |
| 44 | + if link.get("external_link"): |
| 45 | + if "#" in link.get("external_link"): |
| 46 | + config["anchor"] = link.get("external_link").split("#", 1)[1] |
| 47 | + ext = link.get("external_link").split("#", 1)[0] |
| 48 | + if ext.startswith("tel:"): |
| 49 | + config["phone"] = ext[4:] |
| 50 | + elif ext.startswith("mailto:"): |
| 51 | + config["mailto"] = ext[7:] |
| 52 | + elif ext: |
| 53 | + config["external_link"] = ext |
| 54 | + elif link.get("internal_link"): |
| 55 | + model, pk = link.get("internal_link").split(":") |
| 56 | + config["internal_link"] = {"model": model, "pk": int(pk)} |
| 57 | + if link.get("anchor"): |
| 58 | + config["anchor"] = link.get("anchor")[1:] |
| 59 | + elif link.get("file_link"): |
| 60 | + config["file_link"] = {"model": "filer.file", "pk": int(link.get("file_link"))} |
| 61 | + del config["link"] |
| 62 | + return True |
| 63 | + return False |
| 64 | + |
| 65 | + |
| 66 | +def convert(apps, schema_editor, direction): |
| 67 | + FrontendUIItem = apps.get_model("djangocms_frontend", "FrontendUIItem") |
| 68 | + for item in FrontendUIItem.objects.all(): |
| 69 | + changed = convert_item(item.config, direction) |
| 70 | + if changed: |
| 71 | + item.save() |
| 72 | + |
| 73 | + |
| 74 | +class Migration(migrations.Migration): |
| 75 | + dependencies = [ |
| 76 | + ("djangocms_frontend", "0001_initial"), |
| 77 | + ] |
| 78 | + |
| 79 | + operations = [ |
| 80 | + migrations.RunPython( |
| 81 | + lambda apps, schema_editor: convert(apps, schema_editor, "forward"), |
| 82 | + lambda apps, schema_editor: convert(apps, schema_editor, "backward"), |
| 83 | + elidable=True |
| 84 | + ), |
| 85 | + ] |
0 commit comments