| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from collections.abc import Callable |
| 4 |
|
| 5 |
from django.db import migrations |
| 6 |
from django.db.backends.base.schema import BaseDatabaseSchemaEditor |
| 7 |
from django.db.migrations.state import StateApps |
| 8 |
|
| 9 |
def permission_migration( |
| 10 |
*, old: str, new: str |
| 11 |
) -> Callable[[StateApps, BaseDatabaseSchemaEditor], None]: |
| 12 |
def migrate(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None: |
| 13 |
try: |
| 14 |
ContentType = apps.get_model("contenttypes", "ContentType") |
| 15 |
Permission = apps.get_model("auth", "Permission") |
| 16 |
Dashboard = apps.get_model("django_rq", "Dashboard") |
| 17 |
except LookupError: |
| 18 |
return |
| 19 |
|
| 20 |
Permission._default_manager.filter( |
| 21 |
content_type=ContentType._default_manager.get_for_model(Dashboard), |
| 22 |
codename=old, |
| 23 |
).update(codename=new) |
| 24 |
|
| 25 |
return migrate |
| 26 |
|
| 27 |
|
| 28 |
class Migration(migrations.Migration): |
| 29 |
dependencies = [('django_rq', '0002_delete_queue_create_dashboard')] |
| 30 |
|
| 31 |
operations = [ |
| 32 |
migrations.AlterModelOptions( |
| 33 |
name='dashboard', |
| 34 |
options={ |
| 35 |
'default_permissions': (), |
| 36 |
'managed': False, |
| 37 |
'permissions': (('admin', 'Access admin page'),), |
| 38 |
'verbose_name': 'Django-RQ', |
| 39 |
'verbose_name_plural': 'Django-RQ', |
| 40 |
}, |
| 41 |
), |
| 42 |
migrations.RunPython( |
| 43 |
permission_migration(old="view", new="admin"), |
| 44 |
permission_migration(old="admin", new="view"), |
| 45 |
) |
| 46 |
] |