summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2019-04-26 08:51:58 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-04-27 21:08:46 +0200
commit4f8ebdd09835ad5c010c122ec669c1b224801302 (patch)
treeaf0a0e958b213babf77a7fc621e7886d340009b3 /django
parente45763193fe496cefc6749c45e6a31e62f987d63 (diff)
[2.2.x] Fixed #30351 -- Handled pre-existing permissions in proxy model permissions data migration.
Regression in 181fb60159e54d442d3610f4afba6f066a6dac05. Backport of 98296f86b340c8c9c968375d59f1d3a3479e60c2 from master
Diffstat (limited to 'django')
-rw-r--r--django/contrib/auth/migrations/0011_update_proxy_permissions.py30
1 files changed, 25 insertions, 5 deletions
diff --git a/django/contrib/auth/migrations/0011_update_proxy_permissions.py b/django/contrib/auth/migrations/0011_update_proxy_permissions.py
index 0e66649695..c3f617f438 100644
--- a/django/contrib/auth/migrations/0011_update_proxy_permissions.py
+++ b/django/contrib/auth/migrations/0011_update_proxy_permissions.py
@@ -1,5 +1,18 @@
-from django.db import migrations
+import sys
+
+from django.core.management.color import color_style
+from django.db import migrations, transaction
from django.db.models import Q
+from django.db.utils import IntegrityError
+
+WARNING = """
+ A problem arose migrating proxy model permissions for {old} to {new}.
+
+ Permission(s) for {new} already existed.
+ Codenames Q: {query}
+
+ Ensure to audit ALL permissions for {old} and {new}.
+"""
def update_proxy_model_permissions(apps, schema_editor, reverse=False):
@@ -7,6 +20,7 @@ def update_proxy_model_permissions(apps, schema_editor, reverse=False):
Update the content_type of proxy model permissions to use the ContentType
of the proxy model.
"""
+ style = color_style()
Permission = apps.get_model('auth', 'Permission')
ContentType = apps.get_model('contenttypes', 'ContentType')
for Model in apps.get_models():
@@ -24,10 +38,16 @@ def update_proxy_model_permissions(apps, schema_editor, reverse=False):
proxy_content_type = ContentType.objects.get_for_model(Model, for_concrete_model=False)
old_content_type = proxy_content_type if reverse else concrete_content_type
new_content_type = concrete_content_type if reverse else proxy_content_type
- Permission.objects.filter(
- permissions_query,
- content_type=old_content_type,
- ).update(content_type=new_content_type)
+ try:
+ with transaction.atomic():
+ Permission.objects.filter(
+ permissions_query,
+ content_type=old_content_type,
+ ).update(content_type=new_content_type)
+ except IntegrityError:
+ old = '{}_{}'.format(old_content_type.app_label, old_content_type.model)
+ new = '{}_{}'.format(new_content_type.app_label, new_content_type.model)
+ sys.stdout.write(style.WARNING(WARNING.format(old=old, new=new, query=permissions_query)))
def revert_proxy_model_permissions(apps, schema_editor):