diff --git a/common/djangoapps/third_party_auth/migrations/0013_default_site_id_wrapper_function.py b/common/djangoapps/third_party_auth/migrations/0013_default_site_id_wrapper_function.py new file mode 100644 index 000000000000..250e5370854e --- /dev/null +++ b/common/djangoapps/third_party_auth/migrations/0013_default_site_id_wrapper_function.py @@ -0,0 +1,36 @@ +# Generated by Django 4.2.13 on 2024-05-13 19:22 + +import common.djangoapps.third_party_auth.models +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('sites', '0002_alter_domain_unique'), + ('third_party_auth', '0012_alter_ltiproviderconfig_site_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='ltiproviderconfig', + name='site', + field=models.ForeignKey(default=common.djangoapps.third_party_auth.models._get_site_id_from_settings, help_text='The Site that this provider configuration belongs to.', on_delete=django.db.models.deletion.CASCADE, related_name='%(class)ss', to='sites.site'), + ), + migrations.AlterField( + model_name='oauth2providerconfig', + name='site', + field=models.ForeignKey(default=common.djangoapps.third_party_auth.models._get_site_id_from_settings, help_text='The Site that this provider configuration belongs to.', on_delete=django.db.models.deletion.CASCADE, related_name='%(class)ss', to='sites.site'), + ), + migrations.AlterField( + model_name='samlconfiguration', + name='site', + field=models.ForeignKey(default=common.djangoapps.third_party_auth.models._get_site_id_from_settings, help_text='The Site that this SAML configuration belongs to.', on_delete=django.db.models.deletion.CASCADE, related_name='%(class)ss', to='sites.site'), + ), + migrations.AlterField( + model_name='samlproviderconfig', + name='site', + field=models.ForeignKey(default=common.djangoapps.third_party_auth.models._get_site_id_from_settings, help_text='The Site that this provider configuration belongs to.', on_delete=django.db.models.deletion.CASCADE, related_name='%(class)ss', to='sites.site'), + ), + ] diff --git a/common/djangoapps/third_party_auth/models.py b/common/djangoapps/third_party_auth/models.py index 1ea265e868f2..6d244d96eddd 100644 --- a/common/djangoapps/third_party_auth/models.py +++ b/common/djangoapps/third_party_auth/models.py @@ -88,6 +88,24 @@ def __str__(self): ) +def _get_site_id_from_settings() -> int: + """ + Simply return SITE_ID from settings. + + We define this function so the current SITE_ID can be used as the default value on a + couple fields in this module. We can't use `settings.SITE_ID` directly in the model class, + because then the migration file will contain whatever the value of SITE_ID was when the + migration was created. This value is usually 1, but in the event that a developer is + running a non-default site, they would get a value like 2 or 3, which will result in + a dirty migration state. By defining this wrapper function, the name + `_get_site_id_from_settings` will be serialized to the migration file as the default, + regardless of what any developer's active SITE_ID is. + + Reference: https://docs.djangoproject.com/en/dev/ref/models/fields/#default + """ + return settings.SITE_ID + + class ProviderConfig(ConfigurationModel): """ Abstract Base Class for configuring a third_party_auth provider @@ -143,7 +161,7 @@ class ProviderConfig(ConfigurationModel): ) site = models.ForeignKey( Site, - default=settings.SITE_ID, + default=_get_site_id_from_settings, related_name='%(class)ss', help_text=_( 'The Site that this provider configuration belongs to.' @@ -455,7 +473,7 @@ class SAMLConfiguration(ConfigurationModel): KEY_FIELDS = ('site_id', 'slug') site = models.ForeignKey( Site, - default=settings.SITE_ID, + default=_get_site_id_from_settings, related_name='%(class)ss', help_text=_( 'The Site that this SAML configuration belongs to.'