Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Get or create permissions in migrations (#9662) (#9663)

Marcin Gębalafa393b4659fd
diff --git a/saleor/account/migrations/0055_alter_user_options.py b/saleor/account/migrations/0055_alter_user_options.py
index 87496f81b6..1ada86b0a6 100644
--- a/saleor/account/migrations/0055_alter_user_options.py
+++ b/saleor/account/migrations/0055_alter_user_options.py
@@ -7,10 +7,13 @@ def assing_permissions(apps, schema_editor):
def on_migrations_complete(sender=None, **kwargs):
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
+ ContentType = apps.get_model("contenttypes", "ContentType")
+
+ ct, _ = ContentType.objects.get_or_create(app_label="account", model="user")
+ impersonate_user, _ = Permission.objects.get_or_create(
+ name="Impersonate user.", content_type=ct, codename="impersonate_user"
+ )
- impersonate_user = Permission.objects.filter(
- codename="impersonate_user", content_type__app_label="account"
- ).first()
manage_users = Permission.objects.filter(
codename="manage_users", content_type__app_label="account"
)
@@ -18,7 +21,7 @@ def assing_permissions(apps, schema_editor):
for group in Group.objects.filter(permissions__in=manage_users).iterator():
group.permissions.add(impersonate_user)
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
class Migration(migrations.Migration):
diff --git a/saleor/account/migrations/0058_update_user_search_document.py b/saleor/account/migrations/0058_update_user_search_document.py
index 4a297ba9d8..6c89c1216c 100644
--- a/saleor/account/migrations/0058_update_user_search_document.py
+++ b/saleor/account/migrations/0058_update_user_search_document.py
@@ -10,7 +10,7 @@ def update_user_search_document_values(apps, _schema_editor):
def on_migrations_complete(sender=None, **kwargs):
set_user_search_document_values.delay()
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
class Migration(migrations.Migration):
diff --git a/saleor/channel/migrations/0001_initial.py b/saleor/channel/migrations/0001_initial.py
index 9834475ff8..28d04a9d7f 100644
--- a/saleor/channel/migrations/0001_initial.py
+++ b/saleor/channel/migrations/0001_initial.py
@@ -9,13 +9,17 @@ def assing_permissions(apps, schema_editor):
def on_migrations_complete(sender=None, **kwargs):
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
- manage_channels = Permission.objects.filter(
- codename="manage_channels", content_type__app_label="channel"
- ).first()
+ ContentType = apps.get_model("contenttypes", "ContentType")
+
+ ct, _ = ContentType.objects.get_or_create(app_label="channel", model="channel")
+ manage_channels, _ = Permission.objects.get_or_create(
+ name="Manage channels.", content_type=ct, codename="manage_channels"
+ )
+
for group in Group.objects.iterator():
group.permissions.add(manage_channels)
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
def get_default_currency(Checkout, Order, Product, ShippingMethod, Voucher):
diff --git a/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py b/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py
index cd6f51fca2..8d04f3cfcd 100644
--- a/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py
+++ b/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py
@@ -9,27 +9,28 @@ def assign_permissions(apps, schema_editor):
Permission = apps.get_model("auth", "Permission")
App = apps.get_model("app", "App")
Group = apps.get_model("auth", "Group")
+ ContentType = apps.get_model("contenttypes", "ContentType")
+
+ ct, _ = ContentType.objects.get_or_create(
+ app_label="checkout", model="checkout"
+ )
+ handle_checkouts, _ = Permission.objects.get_or_create(
+ name="Handle checkouts", content_type=ct, codename="handle_checkouts"
+ )
- handle_checkouts = Permission.objects.filter(
- codename="handle_checkouts", content_type__app_label="checkout"
- ).first()
manage_checkouts = Permission.objects.filter(
codename="manage_checkouts", content_type__app_label="checkout"
).first()
- app_qs = App.objects.filter(
- permissions=manage_checkouts,
- )
+ app_qs = App.objects.filter(permissions=manage_checkouts)
for app in app_qs.iterator():
app.permissions.add(handle_checkouts)
- groups = Group.objects.filter(
- permissions=manage_checkouts,
- )
+ groups = Group.objects.filter(permissions=manage_checkouts)
for group in groups.iterator():
group.permissions.add(handle_checkouts)
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
class Migration(migrations.Migration):
diff --git a/saleor/order/migrations/0123_update_order_search_document.py b/saleor/order/migrations/0123_update_order_search_document.py
index ac0f3fee55..a365d46d8f 100644
--- a/saleor/order/migrations/0123_update_order_search_document.py
+++ b/saleor/order/migrations/0123_update_order_search_document.py
@@ -10,7 +10,7 @@ def update_order_search_document_values(apps, _schema_editor):
def on_migrations_complete(sender=None, **kwargs):
set_order_search_document_values.delay()
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
class Migration(migrations.Migration):
diff --git a/saleor/page/migrations/0017_pagetype.py b/saleor/page/migrations/0017_pagetype.py
index 1346d60a56..3c39b18c59 100644
--- a/saleor/page/migrations/0017_pagetype.py
+++ b/saleor/page/migrations/0017_pagetype.py
@@ -9,11 +9,14 @@ def update_groups_with_manage_pages_with_new_permission(apps, schema_editor):
def on_migrations_complete(sender=None, **kwargs):
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
+ ContentType = apps.get_model("contenttypes", "ContentType")
- manage_page_types_and_attributes_perm = Permission.objects.filter(
+ ct, _ = ContentType.objects.get_or_create(app_label="page", model="pagetype")
+ manage_page_types_and_attributes_perm, _ = Permission.objects.get_or_create(
codename="manage_page_types_and_attributes",
- content_type__app_label="page",
- ).first()
+ content_type=ct,
+ name="Manage page types and attributes.",
+ )
groups = Group.objects.filter(
permissions__content_type__app_label="page",
@@ -22,7 +25,7 @@ def update_groups_with_manage_pages_with_new_permission(apps, schema_editor):
for group in groups:
group.permissions.add(manage_page_types_and_attributes_perm)
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
def add_page_types_to_existing_pages(apps, schema_editor):
diff --git a/saleor/payment/migrations/0029_alter_payment_options.py b/saleor/payment/migrations/0029_alter_payment_options.py
index 5231e17e7b..043d88abab 100644
--- a/saleor/payment/migrations/0029_alter_payment_options.py
+++ b/saleor/payment/migrations/0029_alter_payment_options.py
@@ -8,13 +8,16 @@ def assing_permissions(apps, schema_editor):
def on_migrations_complete(sender=None, **kwargs):
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
- handle_payments = Permission.objects.filter(
- codename="handle_payments", content_type__app_label="payment"
- ).first()
+ ContentType = apps.get_model("contenttypes", "ContentType")
+
+ ct, _ = ContentType.objects.get_or_create(app_label="payment", model="payment")
+ handle_payments, _ = Permission.objects.get_or_create(
+ name="Handle payments", codename="handle_payments", content_type=ct
+ )
for group in Group.objects.iterator():
group.permissions.add(handle_payments)
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
class Migration(migrations.Migration):
diff --git a/saleor/product/migrations/0129_add_product_types_and_attributes_perm.py b/saleor/product/migrations/0129_add_product_types_and_attributes_perm.py
index 068516cc48..985b72e9fb 100644
--- a/saleor/product/migrations/0129_add_product_types_and_attributes_perm.py
+++ b/saleor/product/migrations/0129_add_product_types_and_attributes_perm.py
@@ -8,11 +8,16 @@ def update_groups_with_manage_products_with_new_permission(apps, schema_editor):
def on_migrations_complete(sender=None, **kwargs):
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
+ ContentType = apps.get_model("contenttypes", "ContentType")
- product_type_and_attribute_permission = Permission.objects.filter(
+ ct, _ = ContentType.objects.get_or_create(
+ app_label="product", model="producttype"
+ )
+ product_type_and_attribute_permission, _ = Permission.objects.get_or_create(
codename="manage_product_types_and_attributes",
- content_type__app_label="product",
- ).first()
+ content_type=ct,
+ name="Manage product types and attributes.",
+ )
groups = Group.objects.filter(
permissions__content_type__app_label="product",
@@ -21,7 +26,7 @@ def update_groups_with_manage_products_with_new_permission(apps, schema_editor):
for group in groups:
group.permissions.add(product_type_and_attribute_permission)
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
class Migration(migrations.Migration):
diff --git a/saleor/product/migrations/0157_update_product_search_document.py b/saleor/product/migrations/0157_update_product_search_document.py
index e1fb0a31f5..6e83fadbd5 100644
--- a/saleor/product/migrations/0157_update_product_search_document.py
+++ b/saleor/product/migrations/0157_update_product_search_document.py
@@ -10,7 +10,7 @@ def update_product_search_document_values(apps, _schema_editor):
def on_migrations_complete(sender=None, **kwargs):
set_product_search_document_values.delay()
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
class Migration(migrations.Migration):
diff --git a/saleor/product/migrations/0165_update_product_search_document.py b/saleor/product/migrations/0165_update_product_search_document.py
index 423f77727c..b03d60b4e6 100644
--- a/saleor/product/migrations/0165_update_product_search_document.py
+++ b/saleor/product/migrations/0165_update_product_search_document.py
@@ -8,7 +8,7 @@ def update_product_search_document_values(apps, _schema_editor):
def on_migrations_complete(sender=None, **kwargs):
set_product_search_document_values.delay()
- post_migrate.connect(on_migrations_complete)
+ post_migrate.connect(on_migrations_complete, weak=False)
class Migration(migrations.Migration):