|
5 | 5 | from django.db import migrations, models |
6 | 6 |
|
7 | 7 |
|
| 8 | +def create_types_and_permissions(apps, schema_editor): |
| 9 | + """Before we can migrate to the new DABContentType, entries in that table must be created. |
| 10 | +
|
| 11 | + This method runs what is ordinarily the post_migrate logic, but in the migration case here. |
| 12 | + """ |
| 13 | + from ansible_base.rbac.management import create_dab_permissions |
| 14 | + |
| 15 | + create_dab_permissions(apps.get_app_config('dab_rbac'), apps=apps) |
| 16 | + |
| 17 | + |
| 18 | +def migrate_content_type(apps, schema_editor): |
| 19 | + ct_cls = apps.get_model('dab_rbac', 'DABContentType') |
| 20 | + ct_cls.objects.clear_cache() |
| 21 | + for model_name in ('dabpermission', 'objectrole', 'roledefinition', 'roleuserassignment', 'roleteamassignment'): |
| 22 | + cls = apps.get_model('dab_rbac', model_name) |
| 23 | + for obj in cls.objects.all(): |
| 24 | + old_ct = obj.content_type |
| 25 | + # NOTE: could give duplicate normally, but that is impossible in migration path |
| 26 | + obj.new_content_type = ct_cls.objects.get_by_natural_key(old_ct.app_label, old_ct.model) |
| 27 | + obj.save() |
| 28 | + for model_name in ('roleevaluation', 'roleevaluationuuid'): |
| 29 | + cls = apps.get_model('dab_rbac', model_name) |
| 30 | + cls.objects.all().delete() |
| 31 | + |
| 32 | + |
8 | 33 | class Migration(migrations.Migration): |
9 | 34 |
|
10 | 35 | dependencies = [ |
@@ -175,4 +200,6 @@ class Migration(migrations.Migration): |
175 | 200 | name='api_slug', |
176 | 201 | field=models.CharField(default='', help_text='String to use for references to this type from other models in the API.', max_length=201), |
177 | 202 | ), |
| 203 | + migrations.RunPython(create_types_and_permissions, migrations.RunPython.noop), |
| 204 | + migrations.RunPython(migrate_content_type, migrations.RunPython.noop), |
178 | 205 | ] |
0 commit comments