Skip to content

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

Add homepage collection to site settings

oldPadavanff7d975e59ee
diff --git a/saleor/site/migrations/0018_sitesettings_homepage_collection.py b/saleor/site/migrations/0018_sitesettings_homepage_collection.py
new file mode 100644
index 0000000000..29d00baeb1
--- /dev/null
+++ b/saleor/site/migrations/0018_sitesettings_homepage_collection.py
@@ -0,0 +1,48 @@
+# Generated by Django 2.0.3 on 2018-08-07 14:01
+
+import datetime
+
+from django.db import migrations, models
+from django.db.models import Q
+from django.utils.text import slugify
+import django.db.models.deletion
+
+
+def copy_featured_products_to_homepade_collection(apps, schema_editor):
+ SiteSettings = apps.get_model('site', 'SiteSettings')
+ Collection = apps.get_model('product', 'Collection')
+ Product = apps.get_model('product', 'Product')
+
+ homepage_collection = Collection(name='Homepage collection for migration')
+ homepage_collection.slug = slugify(homepage_collection.name)
+ homepage_collection.save()
+ today = datetime.date.today()
+ homepage_collection.products.add(
+ *list(
+ Product.objects.filter(
+ Q(available_on__lte=today) | Q(available_on__isnull=True),
+ Q(is_published=True, is_featured=True))))
+
+ for site_setting in SiteSettings.objects.all():
+ site_setting.homepage_collection = homepage_collection
+ site_setting.save()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0066_auto_20180803_0528'),
+ ('site', '0017_auto_20180803_0528'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='sitesettings',
+ name='homepage_collection',
+ field=models.ForeignKey(
+ blank=True, null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ related_name='+', to='product.Collection'),),
+ migrations.RunPython(
+ copy_featured_products_to_homepade_collection,
+ lambda app, schema_editor: None)]
diff --git a/saleor/site/models.py b/saleor/site/models.py
index 69f7b6ba85..0768aa13ec 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -24,6 +24,9 @@ class SiteSettings(models.Model):
display_gross_prices = models.BooleanField(default=True)
charge_taxes_on_shipping = models.BooleanField(default=True)
track_inventory_by_default = models.BooleanField(default=True)
+ homepage_collection = models.ForeignKey(
+ 'product.Collection', on_delete=models.SET_NULL, related_name='+',
+ blank=True, null=True)
translated = TranslationProxy()