mirror of
https://github.com/ansible/awx.git
synced 2026-01-16 12:20:45 -03:30
Cascade delete teams when their organization is deleted
This commit is contained in:
parent
aaab80ad60
commit
994a72967d
20
awx/main/migrations/0027_v300_team_migrations.py
Normal file
20
awx/main/migrations/0027_v300_team_migrations.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from awx.main.migrations import _rbac as rbac
|
||||
from awx.main.migrations import _team_cleanup as team_cleanup
|
||||
from awx.main.migrations import _migration_utils as migration_utils
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('main', '0026_v300_credential_unique'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(migration_utils.set_current_apps_for_migrations),
|
||||
migrations.RunPython(team_cleanup.migrate_team),
|
||||
migrations.RunPython(rbac.rebuild_role_hierarchy),
|
||||
]
|
||||
20
awx/main/migrations/0028_v300_org_team_cascade.py
Normal file
20
awx/main/migrations/0028_v300_org_team_cascade.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import awx.main.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('main', '0027_v300_team_migrations'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='team',
|
||||
name='organization',
|
||||
field=models.ForeignKey(related_name='teams', to='main.Organization', null=True),
|
||||
),
|
||||
]
|
||||
30
awx/main/migrations/_team_cleanup.py
Normal file
30
awx/main/migrations/_team_cleanup.py
Normal file
@ -0,0 +1,30 @@
|
||||
# Python
|
||||
import logging
|
||||
from django.utils.encoding import smart_text
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def log_migration(wrapped):
|
||||
'''setup the logging mechanism for each migration method
|
||||
as it runs, Django resets this, so we use a decorator
|
||||
to re-add the handler for each method.
|
||||
'''
|
||||
handler = logging.FileHandler("/tmp/tower_rbac_migrations.log", mode="a", encoding="UTF-8")
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
handler.setLevel(logging.DEBUG)
|
||||
handler.setFormatter(formatter)
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
logger.handlers = []
|
||||
logger.addHandler(handler)
|
||||
return wrapped(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
@log_migration
|
||||
def migrate_team(apps, schema_editor):
|
||||
'''If an orphan team exists that is still active, delete it.'''
|
||||
Team = apps.get_model('main', 'Team')
|
||||
for team in Team.objects.iterator():
|
||||
if team.organization is None:
|
||||
logger.info(smart_text(u"Deleting orphaned team: {}".format(team.name)))
|
||||
team.delete()
|
||||
@ -93,7 +93,7 @@ class Team(CommonModelNameNotUnique, ResourceMixin):
|
||||
'Organization',
|
||||
blank=False,
|
||||
null=True,
|
||||
on_delete=models.SET_NULL,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='teams',
|
||||
)
|
||||
deprecated_projects = models.ManyToManyField(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user