|
| 1 | +# Generated by Django 5.2.4 on 2025-08-20 21:43 |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | + |
| 6 | +def copy_2fa_setting_forward(apps, schema_editor): |
| 7 | + """Copy required_2fa setting from SiteConfiguration to TwoFactorConfig.""" |
| 8 | + # Get model classes for this migration point |
| 9 | + SiteConfiguration = apps.get_model('myapp', 'SiteConfiguration') |
| 10 | + TwoFactorConfig = apps.get_model('require2fa', 'TwoFactorConfig') |
| 11 | + |
| 12 | + # Get the current site configuration (if it exists) |
| 13 | + try: |
| 14 | + site_config = SiteConfiguration.objects.get() |
| 15 | + required_2fa = getattr(site_config, 'required_2fa', False) |
| 16 | + except SiteConfiguration.DoesNotExist: |
| 17 | + # No site configuration exists, default to False |
| 18 | + required_2fa = False |
| 19 | + |
| 20 | + # Create or update the TwoFactorConfig with the copied value |
| 21 | + TwoFactorConfig.objects.get_or_create( |
| 22 | + defaults={'required': required_2fa} |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def copy_2fa_setting_reverse(apps, schema_editor): |
| 27 | + """Reverse migration - copy back from TwoFactorConfig to SiteConfiguration.""" |
| 28 | + # Get model classes for this migration point |
| 29 | + SiteConfiguration = apps.get_model('myapp', 'SiteConfiguration') |
| 30 | + TwoFactorConfig = apps.get_model('require2fa', 'TwoFactorConfig') |
| 31 | + |
| 32 | + # Get the TwoFactorConfig value |
| 33 | + try: |
| 34 | + config = TwoFactorConfig.objects.get() |
| 35 | + required_2fa = config.required |
| 36 | + except TwoFactorConfig.DoesNotExist: |
| 37 | + required_2fa = False |
| 38 | + |
| 39 | + # Update SiteConfiguration if it exists |
| 40 | + try: |
| 41 | + site_config = SiteConfiguration.objects.get() |
| 42 | + site_config.required_2fa = required_2fa |
| 43 | + site_config.save() |
| 44 | + except SiteConfiguration.DoesNotExist: |
| 45 | + # If no SiteConfiguration exists, we can't copy back |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +class Migration(migrations.Migration): |
| 50 | + |
| 51 | + dependencies = [ |
| 52 | + ('require2fa', '0001_initial'), |
| 53 | + ('myapp', '0008_siteconfiguration_include_staff_in_analytics_and_more'), # Latest myapp migration |
| 54 | + ] |
| 55 | + |
| 56 | + operations = [ |
| 57 | + migrations.RunPython( |
| 58 | + copy_2fa_setting_forward, |
| 59 | + copy_2fa_setting_reverse |
| 60 | + ), |
| 61 | + ] |
0 commit comments