2
0

0029_multinavs.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from django.db import migrations, models
  2. import django.db.models.deletion
  3. import modelcluster.fields
  4. def add_navbar_orderables(apps, schema_editor):
  5. Site = apps.get_model('wagtailcore', 'Site')
  6. LayoutSettings = apps.get_model('wagtailcrx', 'LayoutSettings')
  7. Navbar = apps.get_model('wagtailcrx', 'Navbar')
  8. NavbarOrderable = apps.get_model('wagtailcrx', 'NavbarOrderable')
  9. # If it's a new site, this migration will not run.
  10. try:
  11. site = Site.objects.get(is_default_site=True)
  12. layout = LayoutSettings.objects.get(site=site)
  13. except (Site.DoesNotExist, LayoutSettings.DoesNotExist):
  14. return
  15. current_navs = Navbar.objects.all()
  16. db_alias = schema_editor.connection.alias
  17. layout.site_navbar = []
  18. layout.save()
  19. for nav in current_navs:
  20. NavbarOrderable.objects.using(db_alias).create(navbar_chooser=layout, navbar=nav)
  21. def add_footer_orderables(apps, schema_editor):
  22. Site = apps.get_model('wagtailcore', 'Site')
  23. LayoutSettings = apps.get_model('wagtailcrx', 'LayoutSettings')
  24. Footer = apps.get_model('wagtailcrx', 'Footer')
  25. FooterOrderable = apps.get_model('wagtailcrx', 'FooterOrderable')
  26. # If it's a new site, this migration will not run.
  27. try:
  28. site = Site.objects.get(is_default_site=True)
  29. layout = LayoutSettings.objects.get(site=site)
  30. except (Site.DoesNotExist, LayoutSettings.DoesNotExist):
  31. return
  32. current_footers = Footer.objects.all()
  33. db_alias = schema_editor.connection.alias
  34. layout.site_footer = []
  35. layout.save()
  36. for footer in current_footers:
  37. FooterOrderable.objects.using(db_alias).create(footer_chooser=layout, footer=footer)
  38. class Migration(migrations.Migration):
  39. atomic = False
  40. dependencies = [
  41. ('wagtailcrx', '0028_auto_20220609_1532'),
  42. ]
  43. operations = [
  44. migrations.CreateModel(
  45. name='NavbarOrderable',
  46. fields=[
  47. ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
  48. ('sort_order', models.IntegerField(blank=True, editable=False, null=True)),
  49. ('navbar', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='wagtailcrx.navbar')),
  50. ('navbar_chooser', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='site_navbar', to='wagtailcrx.layoutsettings', verbose_name='Site Navbars')),
  51. ],
  52. options={
  53. 'ordering': ['sort_order'],
  54. 'abstract': False,
  55. },
  56. ),
  57. migrations.CreateModel(
  58. name='FooterOrderable',
  59. fields=[
  60. ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
  61. ('sort_order', models.IntegerField(blank=True, editable=False, null=True)),
  62. ('footer', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='wagtailcrx.footer')),
  63. ('footer_chooser', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='site_footer', to='wagtailcrx.layoutsettings', verbose_name='Site Footers')),
  64. ],
  65. options={
  66. 'ordering': ['sort_order'],
  67. 'abstract': False,
  68. },
  69. ),
  70. migrations.RunPython(add_navbar_orderables),
  71. migrations.RunPython(add_footer_orderables)
  72. ]