Skip to content

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

Add product creation prerequisites (#10244)

Jakub6189878f6696
diff --git a/saleor/channel/migrations/0004_create_default_channel.py b/saleor/channel/migrations/0004_create_default_channel.py
new file mode 100644
index 0000000000..264cd79d89
--- /dev/null
+++ b/saleor/channel/migrations/0004_create_default_channel.py
@@ -0,0 +1,23 @@
+from django.db import migrations
+from django.conf import settings
+
+
+def create_default_channel(apps, schema_editor):
+ Channel = apps.get_model("channel", "Channel")
+ if not Channel.objects.all().exists() and settings.POPULATE_DEFAULTS:
+ Channel.objects.create(
+ name="Default Channel",
+ slug="default-channel",
+ currency_code="USD",
+ default_country="US",
+ is_active=True,
+ )
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("channel", "0003_alter_channel_default_country"),
+ ]
+ operations = [
+ migrations.RunPython(create_default_channel, migrations.RunPython.noop)
+ ]
diff --git a/saleor/product/migrations/0173_create_default_category_and_product_type.py b/saleor/product/migrations/0173_create_default_category_and_product_type.py
new file mode 100644
index 0000000000..57fb1a21ac
--- /dev/null
+++ b/saleor/product/migrations/0173_create_default_category_and_product_type.py
@@ -0,0 +1,42 @@
+from django.db import migrations
+from django.conf import settings
+
+from ...product import ProductTypeKind
+
+
+def create_default_category(apps, schema_editor):
+ Category = apps.get_model("product", "Category")
+ if not Category.objects.all().exists() and settings.POPULATE_DEFAULTS:
+ Category.objects.create(
+ name="Default Category",
+ slug="default-category",
+ lft=0,
+ rght=0,
+ tree_id=0,
+ level=0,
+ )
+
+
+def create_default_product_type(apps, schema_editor):
+ ProductType = apps.get_model("product", "ProductType")
+ if not ProductType.objects.all().exists() and settings.POPULATE_DEFAULTS:
+ ProductType.objects.create(
+ name="Default Type",
+ slug="default-type",
+ kind=ProductTypeKind.NORMAL,
+ has_variants=False,
+ is_shipping_required=True,
+ )
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("product", "0172_merge_20220802_0817"),
+ ("warehouse", "0031_create_default_warehouse"),
+ ]
+
+ operations = [
+ migrations.RunPython(create_default_product_type, migrations.RunPython.noop),
+ migrations.RunPython(create_default_category, migrations.RunPython.noop),
+ ]
diff --git a/saleor/settings.py b/saleor/settings.py
index a86ebbe24d..e5572b2ac4 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -604,6 +604,11 @@ DEFAULT_MENUS = {"top_menu_name": "navbar", "bottom_menu_name": "footer"}
# Slug for channel precreated in Django migrations
DEFAULT_CHANNEL_SLUG = os.environ.get("DEFAULT_CHANNEL_SLUG", "default-channel")
+# Set this to `True` if you want to create default channel, warehouse, product type and
+# category during migrations. It makes it easier for the users to create their first
+# product.
+POPULATE_DEFAULTS = get_bool_from_env("POPULATE_DEFAULTS", True)
+
# Sentry
sentry_sdk.utils.MAX_STRING_LENGTH = 4096
diff --git a/saleor/tests/settings.py b/saleor/tests/settings.py
index 3a5167c1dc..b986e20893 100644
--- a/saleor/tests/settings.py
+++ b/saleor/tests/settings.py
@@ -20,6 +20,8 @@ def lazy_re_compile(regex, flags=0):
return SimpleLazyObject(_compile)
+POPULATE_DEFAULTS = False
+
CELERY_TASK_ALWAYS_EAGER = True
SECRET_KEY = "NOTREALLY"
diff --git a/saleor/warehouse/migrations/0031_create_default_warehouse.py b/saleor/warehouse/migrations/0031_create_default_warehouse.py
new file mode 100644
index 0000000000..5455c37020
--- /dev/null
+++ b/saleor/warehouse/migrations/0031_create_default_warehouse.py
@@ -0,0 +1,26 @@
+from django.db import migrations
+from django.conf import settings
+
+
+def create_default_warehouse(apps, schema_editor):
+ if settings.POPULATE_DEFAULTS:
+ address = apps.get_model("account", "Address").objects.create(country="US")
+ Warehouse = apps.get_model("warehouse", "Warehouse")
+ if not Warehouse.objects.all().exists():
+ Warehouse.objects.create(
+ address=address,
+ name="Default Warehouse",
+ slug="default-warehouse",
+ )
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("channel", "0004_create_default_channel"),
+ ("warehouse", "0030_add_channels_to_warehouses"),
+ ]
+
+ operations = [
+ migrations.RunPython(create_default_warehouse, migrations.RunPython.noop),
+ ]