Skip to content

Commit

Permalink
Make migration use setting for organization
Browse files Browse the repository at this point in the history
  • Loading branch information
john-westcott-iv committed Jan 11, 2024
1 parent 3543921 commit 747917b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
3 changes: 2 additions & 1 deletion ansible_base/migrations/0010_initial_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
migrations.swappable_dependency(settings.ANSIBLE_BASE_ORGANIZATION_MODEL),
('ansible_base', '0009_authenticator_users'),
]

Expand Down Expand Up @@ -81,7 +82,7 @@ class Migration(migrations.Migration):
('authorization_grant_type', models.CharField(choices=[('authorization-code', 'Authorization code'), ('password', 'Resource owner password-based')], help_text='The Grant type the user must use for acquire tokens for this application.', max_length=32)),
('created_by', models.ForeignKey(default=None, editable=False, help_text='The user who created this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_created+', to=settings.AUTH_USER_MODEL)),
('modified_by', models.ForeignKey(default=None, editable=False, help_text='The user who last modified this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_modified+', to=settings.AUTH_USER_MODEL)),
('organization', models.ForeignKey(help_text='Organization containing this application.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='applications', to='aap_gateway_api.organization')),
('organization', models.ForeignKey(help_text='Organization containing this application.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='applications', to=settings.ANSIBLE_BASE_ORGANIZATION_MODEL)),
],
options={
'verbose_name': 'application',
Expand Down
16 changes: 11 additions & 5 deletions ansible_base/models/oauth2_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ansible_base.utils.authentication import is_external_account
from ansible_base.utils.encryption import ansible_encryption
from ansible_base.utils.features import OAUTH2_PROVIDER, feature_enabled
from ansible_base.utils.models import get_organization_model
from ansible_base.utils.oauth2_provider import generate_client_id, generate_client_secret
from ansible_base.utils.settings import get_setting

Expand All @@ -30,9 +29,16 @@
# 3. Comment out all OAUTH2_PROVIDER_* settings in dynamic_settings.py
# 4. Change all classes in here to remove oauth2_models.Abstract* as superclasses (including the meta ones)
# 5. gateway-manage makemigrations && gateway-manage migrate ansible_base
# 6. Uncomment all OAUTH2_PROVIDER_* settings
# 7. Revert step 4
# 8. gateway-manage makemigrations && gateway-manage migrate ansible_base
# 6. Look at the generated migration, if this has a direct reference to your applications organization model in OAuth2Application model we need to update it
# for example, if it looks like:
# ('organization', ... to='<your app>.organization')),
# We want to change this to reference the setting:
# ('organization', ... to=settings.ANSIBLE_BASE_ORGANIZATION_MODEL)),
# We should also add this in the migration dependencies:
# migrations.swappable_dependency(settings.ANSIBLE_BASE_ORGANIZATION_MODEL),
# 7. Uncomment all OAUTH2_PROVIDER_* settings
# 8. Revert step 4
# 9. gateway-manage makemigrations && gateway-manage migrate ansible_base
# When you do this django does not realize that you are creating an initial migration and tell you its impossible to migrate so fields
# It will ask you to either: 1. Enter a default 2. Quit
# Tell it to use the default if it has one populated at the prompt. Other wise use django.utils.timezone.now for timestamps and '' for other items
Expand Down Expand Up @@ -90,7 +96,7 @@ class Meta(oauth2_models.AbstractAccessToken.Meta):
validators=[RegexValidator(DATA_URI_RE)],
)
organization = models.ForeignKey(
get_organization_model(),
getattr(settings, 'ANSIBLE_BASE_ORGANIZATION_MODEL'),
related_name='applications',
help_text=_('Organization containing this application.'),
on_delete=models.CASCADE,
Expand Down
16 changes: 0 additions & 16 deletions ansible_base/utils/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from itertools import chain

from django.apps import apps as django_apps
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import gettext as _
from inflection import underscore

from ansible_base.utils.settings import get_setting


def get_all_field_names(model):
# Implements compatibility with _meta.get_all_field_names
Expand Down Expand Up @@ -47,14 +42,3 @@ class AuthToken(BaseModel):
"""
setattr(relation, '__prevent_search__', True)
return relation


def get_organization_model():
setting = 'ANSIBLE_BASE_ORGANIZATION_MODEL'
org_model = get_setting(setting, '.Organization')
try:
return django_apps.get_model(org_model, require_ready=False)
except ValueError:
raise ImproperlyConfigured(_(f"{setting} must be of the form 'app_label.model_name', got {org_model}"))
except LookupError:
raise ImproperlyConfigured(_(f"{setting} refers to model '{org_model}' that has not been installed"))

0 comments on commit 747917b

Please sign in to comment.