| 1 |
import sys |
| 2 |
|
| 3 |
from django.core.management.color import color_style |
| 4 |
from django.db import IntegrityError, migrations, transaction |
| 5 |
from django.db.models import Q |
| 6 |
|
| 7 |
WARNING = """ |
| 8 |
A problem arose migrating proxy model permissions for {old} to {new}. |
| 9 |
|
| 10 |
Permission(s) for {new} already existed. |
| 11 |
Codenames Q: {query} |
| 12 |
|
| 13 |
Ensure to audit ALL permissions for {old} and {new}. |
| 14 |
""" |
| 15 |
|
| 16 |
|
| 17 |
def update_proxy_model_permissions(apps, schema_editor, reverse=False): |
| 18 |
""" |
| 19 |
Update the content_type of proxy model permissions to use the ContentType |
| 20 |
of the proxy model. |
| 21 |
""" |
| 22 |
style = color_style() |
| 23 |
Permission = apps.get_model("auth", "Permission") |
| 24 |
ContentType = apps.get_model("contenttypes", "ContentType") |
| 25 |
alias = schema_editor.connection.alias |
| 26 |
for Model in apps.get_models(): |
| 27 |
opts = Model._meta |
| 28 |
if not opts.proxy: |
| 29 |
continue |
| 30 |
proxy_default_permissions_codenames = [ |
| 31 |
"%s_%s" % (action, opts.model_name) for action in opts.default_permissions |
| 32 |
] |
| 33 |
permissions_query = Q(codename__in=proxy_default_permissions_codenames) |
| 34 |
for codename, name in opts.permissions: |
| 35 |
permissions_query |= Q(codename=codename, name=name) |
| 36 |
content_type_manager = ContentType.objects.db_manager(alias) |
| 37 |
concrete_content_type = content_type_manager.get_for_model( |
| 38 |
Model, for_concrete_model=True |
| 39 |
) |
| 40 |
proxy_content_type = content_type_manager.get_for_model( |
| 41 |
Model, for_concrete_model=False |
| 42 |
) |
| 43 |
old_content_type = proxy_content_type if reverse else concrete_content_type |
| 44 |
new_content_type = concrete_content_type if reverse else proxy_content_type |
| 45 |
try: |
| 46 |
with transaction.atomic(using=alias): |
| 47 |
Permission.objects.using(alias).filter( |
| 48 |
permissions_query, |
| 49 |
content_type=old_content_type, |
| 50 |
).update(content_type=new_content_type) |
| 51 |
except IntegrityError: |
| 52 |
old = "{}_{}".format(old_content_type.app_label, old_content_type.model) |
| 53 |
new = "{}_{}".format(new_content_type.app_label, new_content_type.model) |
| 54 |
sys.stdout.write( |
| 55 |
style.WARNING(WARNING.format(old=old, new=new, query=permissions_query)) |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def revert_proxy_model_permissions(apps, schema_editor): |
| 60 |
""" |
| 61 |
Update the content_type of proxy model permissions to use the ContentType |
| 62 |
of the concrete model. |
| 63 |
""" |
| 64 |
update_proxy_model_permissions(apps, schema_editor, reverse=True) |
| 65 |
|
| 66 |
|
| 67 |
class Migration(migrations.Migration): |
| 68 |
dependencies = [ |
| 69 |
("auth", "0010_alter_group_name_max_length"), |
| 70 |
("contenttypes", "0002_remove_content_type_name"), |
| 71 |
] |
| 72 |
operations = [ |
| 73 |
migrations.RunPython( |
| 74 |
update_proxy_model_permissions, revert_proxy_model_permissions |
| 75 |
), |
| 76 |
] |