Skip to content

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

Add publication date to collections #3125

Konrad Borkowski0f7f8067585d
diff --git a/saleor/core/templatetags/status.py b/saleor/core/templatetags/status.py
index 24f87ad1ee..400bfe22e1 100644
--- a/saleor/core/templatetags/status.py
+++ b/saleor/core/templatetags/status.py
@@ -68,10 +68,10 @@ def render_page_availability(page):
@register.inclusion_tag('dashboard/includes/_collection_availability.html')
def render_collection_availability(collection):
- if collection.is_published:
+ if collection.is_available:
label_cls = LABEL_SUCCESS
else:
label_cls = LABEL_DANGER
- return {'is_published': collection.is_published,
+ return {'is_available': collection.is_available,
'collection': collection,
'label_cls': label_cls}
diff --git a/saleor/dashboard/collection/forms.py b/saleor/dashboard/collection/forms.py
index 67dd0893bd..d563d4c317 100644
--- a/saleor/dashboard/collection/forms.py
+++ b/saleor/dashboard/collection/forms.py
@@ -28,6 +28,9 @@ class CollectionForm(forms.ModelForm):
'is_published': pgettext_lazy(
'Collection published toggle',
'Published'),
+ 'published_at': pgettext_lazy(
+ 'Published at which date field',
+ 'Published at'),
'description': pgettext_lazy(
'Description field of a collection',
'Description')}
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 316232259b..2c758563d5 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -110,6 +110,7 @@ class CollectionInput(graphene.InputObjectType):
description = graphene.String(description='Description of the collection.')
background_image = Upload(description='Background image file.')
seo = SeoInput(description='Search engine optimization fields.')
+ published_at = graphene.types.datetime.Date(description='Publication date. ISO 8601 standard.')
class CollectionCreateInput(CollectionInput):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index e428f9a1f7..8aab8fc4d7 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -408,6 +408,7 @@ type Collection implements Node {
backgroundImage: Image
isPublished: Boolean!
description: String!
+ publishedAt: Date
}
type CollectionAddProducts {
diff --git a/saleor/menu/migrations/0011_auto_20181130_1540.py b/saleor/menu/migrations/0011_auto_20181130_1540.py
new file mode 100644
index 0000000000..1c86b541bc
--- /dev/null
+++ b/saleor/menu/migrations/0011_auto_20181130_1540.py
@@ -0,0 +1,17 @@
+# Generated by Django 2.1.3 on 2018-11-30 21:40
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('menu', '0010_auto_20180913_0841'),
+ ]
+
+ operations = [
+ migrations.AlterModelOptions(
+ name='menu',
+ options={'ordering': ['pk'], 'permissions': (('manage_menus', 'Manage navigation.'),)},
+ ),
+ ]
diff --git a/saleor/product/migrations/0080_collection_published_at.py b/saleor/product/migrations/0080_collection_published_at.py
new file mode 100644
index 0000000000..0e57d501bc
--- /dev/null
+++ b/saleor/product/migrations/0080_collection_published_at.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.1.3 on 2018-11-30 21:40
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0079_default_tax_rate_instead_of_empty_field'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='collection',
+ name='published_at',
+ field=models.DateField(blank=True, null=True),
+ ),
+ ]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 5391cbd34d..34858eb88c 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -435,7 +435,10 @@ class VariantImage(models.Model):
class CollectionQuerySet(models.QuerySet):
def public(self):
- return self.filter(is_published=True)
+ today = datetime.datetime.today()
+ return self.filter(
+ Q(is_published=True),
+ Q(published_at__lte=today) | Q(published_at__isnull=True))
def visible_to_user(self, user):
has_access_to_all = (
@@ -454,7 +457,7 @@ class Collection(SeoModel):
upload_to='collection-backgrounds', blank=True, null=True)
is_published = models.BooleanField(default=False)
description = models.TextField(blank=True)
-
+ published_at = models.DateField(blank=True, null=True);
objects = CollectionQuerySet.as_manager()
translated = TranslationProxy()
@@ -469,6 +472,12 @@ class Collection(SeoModel):
'product:collection',
kwargs={'pk': self.id, 'slug': self.slug})
+ @property
+ def is_available(self):
+ today = datetime.date.today()
+ return self.is_published and (self.published_at is None or self.published_at <= today)
+
+
class CollectionTranslation(SeoModelTranslation):
language_code = models.CharField(max_length=10)
diff --git a/saleor/product/views.py b/saleor/product/views.py
index ccecdcb1b3..fe8bb60124 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -136,10 +136,11 @@ def collection_index(request, slug, pk):
collection = get_object_or_404(collections, id=pk)
if collection.slug != slug:
return HttpResponsePermanentRedirect(collection.get_absolute_url())
+ is_available = collection.is_available
products = products_for_products_list(user=request.user).filter(
collections__id=collection.id).order_by('name')
product_filter = ProductCollectionFilter(
request.GET, queryset=products, collection=collection)
ctx = get_product_list_context(request, product_filter)
- ctx.update({'object': collection})
+ ctx.update({'object': collection, 'is_available': is_available})
return TemplateResponse(request, 'collection/index.html', ctx)
diff --git a/saleor/static/dashboard-next/collections/components/CollectionCreatePage/CollectionCreatePage.tsx b/saleor/static/dashboard-next/collections/components/CollectionCreatePage/CollectionCreatePage.tsx
index 765888ecb4..e24c834523 100644
--- a/saleor/static/dashboard-next/collections/components/CollectionCreatePage/CollectionCreatePage.tsx
+++ b/saleor/static/dashboard-next/collections/components/CollectionCreatePage/CollectionCreatePage.tsx
@@ -26,6 +26,7 @@ export interface CollectionCreatePageFormData {
isPublished: boolean;
seoDescription: string;
seoTitle: string;
+ publishedAt: string;
}
export interface CollectionCreatePageProps {
@@ -43,6 +44,7 @@ const initialForm: CollectionCreatePageFormData = {
},
isPublished: false,
name: "",
+ publishedAt: "",
seoDescription: "",
seoTitle: ""
};
diff --git a/templates/collection/index.html b/templates/collection/index.html
index 2bf55a9190..a58b6fcae2 100644
--- a/templates/collection/index.html
+++ b/templates/collection/index.html
@@ -17,6 +17,13 @@
You are previewing a collection that is not published.
{% endblocktrans %}
</div>
+ {% elif not is_available %}
+ <div class="alert alert-warning" role="alert">
+ {% blocktrans trimmed with date=object.published_at|date context "Unpublished collection details text" %}
+ <strong>Warning!</strong>
+ You are previewing a collection that will become visible on <strong>{{ date }}</strong>.
+ {% endblocktrans %}
+ </div>
{% endif %}
{% endblock %}
diff --git a/templates/dashboard/collection/detail.html b/templates/dashboard/collection/detail.html
index 771afcb1a8..0e6453d4ea 100644
--- a/templates/dashboard/collection/detail.html
+++ b/templates/dashboard/collection/detail.html
@@ -64,6 +64,9 @@
<div class="row">
{{ form.is_published|materializecss }}
</div>
+ <div class="row">
+ {{ form.published_at|materializecss }}
+ </div>
{% include "dashboard/includes/_google_preview.html" with object=collection form=form %}
</div>
<div class="card-action right-align">
diff --git a/templates/dashboard/includes/_collection_availability.html b/templates/dashboard/includes/_collection_availability.html
index 780bffc204..243c2ef2ba 100644
--- a/templates/dashboard/includes/_collection_availability.html
+++ b/templates/dashboard/includes/_collection_availability.html
@@ -1,9 +1,20 @@
{% load i18n %}
<div class="label label-{{ label_cls }}">
- {% if is_published %}
+ {% if is_available %}
{% trans "Published" context "Collection availability status" %}
- {% else %}
- {% trans "Not Published" context "Collection availability status" %}
- {% endif %}
+ {% elif not collection.is_published %}
+ <div class="label">
+ {% trans "Hidden" context "Collection availability status" %}
+ </div>
+ {% elif collection.published_at %}
+ <div class="label">
+ {% blocktrans with date=collection.published_at context "Collection availability status" %}
+ Hidden (will become visible on {{ date }})
+ {% endblocktrans %}
</div>
+ <div class="label">
+ {% trans "Hidden" context "Collection availability status" %}
+ </div>
+ </div>
+{% endif %}
diff --git a/tests/api/test_collection.py b/tests/api/test_collection.py
index 365320678e..d1b2e7a5ce 100644
--- a/tests/api/test_collection.py
+++ b/tests/api/test_collection.py
@@ -1,5 +1,6 @@
import graphene
import pytest
+from datetime import datetime
from django.utils.text import slugify
from graphql_relay import to_global_id
from unittest.mock import Mock
@@ -54,9 +55,9 @@ def test_create_collection(
monkeypatch, staff_api_client, product_list, permission_manage_products):
query = """
mutation createCollection(
- $name: String!, $slug: String!, $description: String, $products: [ID], $backgroundImage: Upload!, $isPublished: Boolean!) {
+ $name: String!, $slug: String!, $description: String, $products: [ID], $backgroundImage: Upload!, $isPublished: Boolean!, $publishedAt: Date) {
collectionCreate(
- input: {name: $name, slug: $slug, description: $description, products: $products, backgroundImage: $backgroundImage, isPublished: $isPublished}) {
+ input: {name: $name, slug: $slug, description: $description, products: $products, backgroundImage: $backgroundImage, isPublished: $isPublished, publishedAt: $publishedAt}) {
collection {
name
slug
@@ -81,10 +82,12 @@ def test_create_collection(
name = 'test-name'
slug = 'test-slug'
description = 'test-description'
+ published_at = datetime.today()
+
variables = {
'name': name, 'slug': slug, 'description': description,
'products': product_ids, 'backgroundImage': image_name,
- 'isPublished': True}
+ 'isPublished': True, 'publishedAt': published_at}
body = get_multipart_request_body(query, variables, image_file, image_name)
response = staff_api_client.post_multipart(
body, permissions=[permission_manage_products])
@@ -93,6 +96,7 @@ def test_create_collection(
assert data['name'] == name
assert data['slug'] == slug
assert data['description'] == description
+ assert data['publishedAt'] == published_at
assert data['products']['totalCount'] == len(product_ids)
collection = Collection.objects.get(slug=slug)
assert collection.background_image.file
diff --git a/tests/conftest.py b/tests/conftest.py
index 491ed17b25..bd9345d480 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,6 +1,6 @@
from io import BytesIO
from unittest.mock import MagicMock, Mock
-
+from datetime import datetime, timedelta
import pytest
from django.contrib.auth.models import Permission
@@ -598,6 +598,14 @@ def collection(db):
return collection
+def not_yet_available_collection(db):
+ collection = Collection.objects.create(
+ name='Collection', slug='collection', is_published=True, published_at=datetime.today() + timedelta(days=1),
+ description='Test description')
+ return collection
+
+
@pytest.fixture
def draft_collection(db):
collection = Collection.objects.create(
diff --git a/tests/test_collection.py b/tests/test_collection.py
index 0c1afb9b41..c674b27ce2 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -38,3 +38,11 @@ def test_collection_not_published_404(client, draft_collection):
url = reverse('product:collection', kwargs=url_kwargs)
response = client.get(url)
assert response.status_code == 404
+
+def test_collection_not_yet_published(admin_client, client, not_yet_available_collection):
+ url_kwargs = {'pk': not_yet_available_collection.pk, 'slug': not_yet_available_collection.slug}
+ url = reverse('product:collection', kwargs=url_kwargs)
+ response = client.get(url)
+ assert response.status_code == 404
+ response = admin_client.get(url)
+ assert response.status_code == 200