Skip to content

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

Add json fields to models for content/description

Michał Jelonekff34b6e82164
diff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.py
index af6c222645..999f5c31b3 100644
--- a/saleor/dashboard/category/forms.py
+++ b/saleor/dashboard/category/forms.py
@@ -20,7 +20,7 @@ class CategoryForm(forms.ModelForm):
class Meta:
model = Category
- exclude = ['slug']
+ exclude = ['slug', 'description_json']
labels = {
'name': pgettext_lazy('Item name', 'Name'),
'description': pgettext_lazy('Description', 'Description'),
diff --git a/saleor/dashboard/collection/forms.py b/saleor/dashboard/collection/forms.py
index 634a1fe28a..58917d943e 100644
--- a/saleor/dashboard/collection/forms.py
+++ b/saleor/dashboard/collection/forms.py
@@ -19,7 +19,7 @@ class CollectionForm(forms.ModelForm):
class Meta:
model = Collection
- exclude = ['slug']
+ exclude = ['slug', 'description_json']
labels = {
'name': pgettext_lazy('Item name', 'Name'),
'background_image': pgettext_lazy(
diff --git a/saleor/dashboard/page/forms.py b/saleor/dashboard/page/forms.py
index d894df995f..c19c639f55 100644
--- a/saleor/dashboard/page/forms.py
+++ b/saleor/dashboard/page/forms.py
@@ -9,11 +9,12 @@ from ..seo.utils import prepare_seo_description
class PageForm(forms.ModelForm):
content = RichTextField(
- label=pgettext_lazy('Page form: page content field', 'Content'))
+ label=pgettext_lazy('Page form: page content field', 'Content'),
+ required=True)
class Meta:
model = Page
- exclude = ['created']
+ exclude = ['created', 'content_json']
widgets = {
'slug': forms.TextInput(attrs={'placeholder': 'example-slug'})}
labels = {
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index abbad40d1a..701828b352 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -232,7 +232,7 @@ class ProductForm(forms.ModelForm, AttributesMixin):
required=False, queryset=Collection.objects.all(),
label=pgettext_lazy('Add to collection select', 'Collections'))
description = RichTextField(
- label=pgettext_lazy('Description', 'Description'))
+ label=pgettext_lazy('Description', 'Description'), required=True)
weight = WeightField(
required=False, label=pgettext_lazy('ProductType weight', 'Weight'),
help_text=pgettext_lazy(
@@ -244,7 +244,8 @@ class ProductForm(forms.ModelForm, AttributesMixin):
class Meta:
model = Product
- exclude = ['attributes', 'product_type', 'updated_at']
+ exclude = [
+ 'attributes', 'product_type', 'updated_at', 'description_json']
labels = {
'name': pgettext_lazy('Item name', 'Name'),
'price': pgettext_lazy('Currency amount', 'Price'),
diff --git a/saleor/graphql/page/mutations.py b/saleor/graphql/page/mutations.py
index 6ef1997da0..1562417871 100644
--- a/saleor/graphql/page/mutations.py
+++ b/saleor/graphql/page/mutations.py
@@ -1,5 +1,3 @@
-from textwrap import dedent
-
import graphene
from django.utils.text import slugify
@@ -13,8 +11,10 @@ class PageInput(graphene.InputObjectType):
slug = graphene.String(description='Page internal name.')
title = graphene.String(description='Page title.')
content = graphene.String(
- description=dedent("""Page content.
- May consists of ordinary text, HTML and images."""))
+ description=(
+ 'Page content. May consists of ordinary text, HTML and images.'))
+ content_json = graphene.JSONString(
+ description='Page content in JSON format.')
is_published = graphene.Boolean(
description='Determines if page is visible in the storefront')
publication_date = graphene.String(
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index d1be645695..7d5d6d8c76 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -22,9 +22,12 @@ from ..utils import attributes_to_hstore, validate_image_file
class CategoryInput(graphene.InputObjectType):
- description = graphene.String(description='Category description')
- name = graphene.String(description='Category name')
- slug = graphene.String(description='Category slug')
+ description = graphene.String(
+ description='Category description (HTML/text).')
+ description_json = graphene.JSONString(
+ description='Category description (JSON).')
+ name = graphene.String(description='Category name.')
+ slug = graphene.String(description='Category slug.')
seo = SeoInput(description='Search engine optimization fields.')
background_image = Upload(description='Background image file.')
background_image_alt = graphene.String(
@@ -114,7 +117,10 @@ class CollectionInput(graphene.InputObjectType):
description='Informs whether a collection is published.')
name = graphene.String(description='Name of the collection.')
slug = graphene.String(description='Slug of the collection.')
- description = graphene.String(description='Description of the collection.')
+ description = graphene.String(
+ description='Description of the collection (HTML/text).')
+ description_json = graphene.JSONString(
+ description='Description of the collection (JSON).')
background_image = Upload(description='Background image file.')
background_image_alt = graphene.String(
description='Alt text for an image.')
@@ -270,7 +276,10 @@ class ProductInput(graphene.InputObjectType):
graphene.ID,
description='List of IDs of collections that the product belongs to.',
name='collections')
- description = graphene.String(description='Product description.')
+ description = graphene.String(
+ description='Product description (HTML/text).')
+ description_json = graphene.JSONString(
+ description='Product description (JSON).')
is_published = graphene.Boolean(
description='Determines if product is visible to customers.')
name = graphene.String(description='Product name.')
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index ecd5aa7b71..58eb42042c 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -231,6 +231,7 @@ type Category implements Node {
name: String!
slug: String!
description: String!
+ descriptionJson: JSONString!
parent: Category
backgroundImage(size: Int): Image
level: Int!
@@ -263,6 +264,7 @@ type CategoryDelete {
input CategoryInput {
description: String
+ descriptionJson: JSONString
name: String
slug: String
seo: SeoInput
@@ -424,6 +426,7 @@ type Collection implements Node {
products(before: String, after: String, first: Int, last: Int): ProductCountableConnection
backgroundImage(size: Int): Image
description: String!
+ descriptionJson: JSONString!
publishedDate: Date @deprecated(reason: "publishedDate is deprecated, use publicationDate instead")
}
@@ -453,6 +456,7 @@ input CollectionCreateInput {
name: String
slug: String
description: String
+ descriptionJson: JSONString
backgroundImage: Upload
backgroundImageAlt: String
seo: SeoInput
@@ -470,6 +474,7 @@ input CollectionInput {
name: String
slug: String
description: String
+ descriptionJson: JSONString
backgroundImage: Upload
backgroundImageAlt: String
seo: SeoInput
@@ -1185,6 +1190,7 @@ type Page implements Node {
slug: String!
title: String!
content: String!
+ contentJson: JSONString!
created: DateTime!
availableOn: Date @deprecated(reason: "availableOn is deprecated, use publicationDate instead")
isVisible: Boolean @deprecated(reason: "isVisible is deprecated, use isPublished instead")
@@ -1222,6 +1228,7 @@ input PageInput {
slug: String
title: String
content: String
+ contentJson: JSONString
isPublished: Boolean
publicationDate: String
seo: SeoInput
@@ -1330,6 +1337,7 @@ type Product implements Node {
productType: ProductType!
name: String!
description: String!
+ descriptionJson: JSONString!
category: Category!
price: Money
attributes: [SelectedAttribute!]!
@@ -1383,6 +1391,7 @@ input ProductCreateInput {
chargeTaxes: Boolean
collections: [ID]
description: String
+ descriptionJson: JSONString
isPublished: Boolean
name: String
price: Decimal
@@ -1448,6 +1457,7 @@ input ProductInput {
chargeTaxes: Boolean
collections: [ID]
description: String
+ descriptionJson: JSONString
isPublished: Boolean
name: String
price: Decimal
diff --git a/saleor/page/migrations/0007_auto_20190225_0252.py b/saleor/page/migrations/0007_auto_20190225_0252.py
new file mode 100644
index 0000000000..27881f2db4
--- /dev/null
+++ b/saleor/page/migrations/0007_auto_20190225_0252.py
@@ -0,0 +1,34 @@
+# Generated by Django 2.1.5 on 2019-02-25 08:52
+
+import django.contrib.postgres.fields.jsonb
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('page', '0006_auto_20190220_1928'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='page',
+ name='content_json',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
+ ),
+ migrations.AddField(
+ model_name='pagetranslation',
+ name='content_json',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
+ ),
+ migrations.AlterField(
+ model_name='page',
+ name='content',
+ field=models.TextField(blank=True),
+ ),
+ migrations.AlterField(
+ model_name='pagetranslation',
+ name='content',
+ field=models.TextField(blank=True),
+ ),
+ ]
diff --git a/saleor/page/models.py b/saleor/page/models.py
index ac3c421162..7a5a40a91a 100644
--- a/saleor/page/models.py
+++ b/saleor/page/models.py
@@ -1,3 +1,4 @@
+from django.contrib.postgres.fields import JSONField
from django.db import models
from django.urls import reverse
from django.utils.translation import pgettext_lazy
@@ -18,7 +19,8 @@ class PagePublishedQuerySet(PublishedQuerySet):
class Page(SeoModel, PublishableModel):
slug = models.SlugField(unique=True, max_length=100)
title = models.CharField(max_length=200)
- content = models.TextField()
+ content = models.TextField(blank=True)
+ content_json = JSONField(blank=True, default=dict)
created = models.DateTimeField(auto_now_add=True)
objects = PagePublishedQuerySet.as_manager()
@@ -45,7 +47,8 @@ class PageTranslation(SeoModelTranslation):
page = models.ForeignKey(
Page, related_name='translations', on_delete=models.CASCADE)
title = models.CharField(max_length=255, blank=True)
- content = models.TextField()
+ content = models.TextField(blank=True)
+ content_json = JSONField(blank=True, default=dict)
class Meta:
unique_together = (('language_code', 'page'),)
diff --git a/saleor/product/migrations/0089_auto_20190225_0252.py b/saleor/product/migrations/0089_auto_20190225_0252.py
new file mode 100644
index 0000000000..f0dca59f7a
--- /dev/null
+++ b/saleor/product/migrations/0089_auto_20190225_0252.py
@@ -0,0 +1,54 @@
+# Generated by Django 2.1.5 on 2019-02-25 08:52
+
+import django.contrib.postgres.fields.jsonb
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0088_auto_20190220_1928'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='category',
+ name='description_json',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
+ ),
+ migrations.AddField(
+ model_name='categorytranslation',
+ name='description_json',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
+ ),
+ migrations.AddField(
+ model_name='collection',
+ name='description_json',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
+ ),
+ migrations.AddField(
+ model_name='collectiontranslation',
+ name='description_json',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
+ ),
+ migrations.AddField(
+ model_name='product',
+ name='description_json',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
+ ),
+ migrations.AddField(
+ model_name='producttranslation',
+ name='description_json',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
+ ),
+ migrations.AlterField(
+ model_name='product',
+ name='description',
+ field=models.TextField(blank=True),
+ ),
+ migrations.AlterField(
+ model_name='producttranslation',
+ name='description',
+ field=models.TextField(blank=True),
+ ),
+ ]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index bd5c44fd3e..10d855c77c 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -1,7 +1,7 @@
from decimal import Decimal
from django.conf import settings
-from django.contrib.postgres.fields import HStoreField
+from django.contrib.postgres.fields import HStoreField, JSONField
from django.core.validators import MinValueValidator
from django.db import models
from django.urls import reverse
@@ -32,6 +32,7 @@ class Category(MPTTModel, SeoModel):
name = models.CharField(max_length=128)
slug = models.SlugField(max_length=128)
description = models.TextField(blank=True)
+ description_json = JSONField(blank=True, default=dict)
parent = models.ForeignKey(
'self', null=True, blank=True, related_name='children',
on_delete=models.CASCADE)
@@ -58,6 +59,7 @@ class CategoryTranslation(SeoModelTranslation):
Category, related_name='translations', on_delete=models.CASCADE)
name = models.CharField(max_length=128)
description = models.TextField(blank=True)
+ description_json = JSONField(blank=True, default=dict)
class Meta:
unique_together = (('language_code', 'category'),)
@@ -98,7 +100,8 @@ class Product(SeoModel, PublishableModel):
product_type = models.ForeignKey(
ProductType, related_name='products', on_delete=models.CASCADE)
name = models.CharField(max_length=128)
- description = models.TextField()
+ description = models.TextField(blank=True)
+ description_json = JSONField(blank=True, default=dict)
category = models.ForeignKey(
Category, related_name='products', on_delete=models.CASCADE)
price = MoneyField(
@@ -172,7 +175,8 @@ class ProductTranslation(SeoModelTranslation):
product = models.ForeignKey(
Product, related_name='translations', on_delete=models.CASCADE)
name = models.CharField(max_length=128)
- description = models.TextField()
+ description = models.TextField(blank=True)
+ description_json = JSONField(blank=True, default=dict)
class Meta:
unique_together = (('language_code', 'product'),)
@@ -418,6 +422,7 @@ class Collection(SeoModel, PublishableModel):
upload_to='collection-backgrounds', blank=True, null=True)
background_image_alt = models.CharField(max_length=128, blank=True)
description = models.TextField(blank=True)
+ description_json = JSONField(blank=True, default=dict)
translated = TranslationProxy()
@@ -440,6 +445,7 @@ class CollectionTranslation(SeoModelTranslation):
on_delete=models.CASCADE)
name = models.CharField(max_length=128)
description = models.TextField(blank=True)
+ description_json = JSONField(blank=True, default=dict)
class Meta:
unique_together = (('language_code', 'collection'),)
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
index 0ea8680c25..3756fcd4df 100644
--- a/tests/api/test_category.py
+++ b/tests/api/test_category.py
@@ -1,3 +1,4 @@
+import json
from unittest.mock import Mock
import graphene
@@ -50,12 +51,16 @@ def test_category_query(user_api_client, product):
def test_category_create_mutation(
monkeypatch, staff_api_client, permission_manage_products):
query = """
- mutation($name: String, $slug: String, $description: String, $backgroundImage: Upload, $backgroundImageAlt: String, $parentId: ID) {
+ mutation(
+ $name: String, $slug: String, $description: String,
+ $descriptionJson: JSONString, $backgroundImage: Upload,
+ $backgroundImageAlt: String, $parentId: ID) {
categoryCreate(
input: {
name: $name
slug: $slug
description: $description
+ descriptionJson: $descriptionJson
backgroundImage: $backgroundImage
backgroundImageAlt: $backgroundImageAlt
},
@@ -66,6 +71,7 @@ def test_category_create_mutation(
name
slug
description
+ descriptionJson
parent {
name
id
@@ -91,12 +97,14 @@ def test_category_create_mutation(
category_name = 'Test category'
category_slug = slugify(category_name)
category_description = 'Test description'
+ category_description_json = json.dumps({'content': 'description'})
image_file, image_name = create_image()
image_alt = 'Alt text for an image.'
# test creating root category
variables = {
'name': category_name, 'description': category_description,
+ 'descriptionJson': category_description_json,
'backgroundImage': image_name, 'backgroundImageAlt': image_alt,
'slug': category_slug}
body = get_multipart_request_body(query, variables, image_file, image_name)
@@ -107,6 +115,7 @@ def test_category_create_mutation(
assert data['errors'] == []
assert data['category']['name'] == category_name
assert data['category']['description'] == category_description
+ assert data['category']['descriptionJson'] == category_description_json
assert not data['category']['parent']
category = Category.objects.get(name=category_name)
assert category.background_image.file
diff --git a/tests/api/test_collection.py b/tests/api/test_collection.py
index 23f8ed74c3..cace77d7c7 100644
--- a/tests/api/test_collection.py
+++ b/tests/api/test_collection.py
@@ -1,3 +1,4 @@
+import json
from datetime import date
from unittest.mock import Mock
@@ -57,13 +58,26 @@ 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!, $backgroundImageAlt: String, $isPublished: Boolean!, $publicationDate: Date) {
+ $name: String!, $slug: String!, $description: String,
+ $descriptionJson: JSONString, $products: [ID],
+ $backgroundImage: Upload!, $backgroundImageAlt: String,
+ $isPublished: Boolean!, $publicationDate: Date) {
collectionCreate(
- input: {name: $name, slug: $slug, description: $description, products: $products, backgroundImage: $backgroundImage, backgroundImageAlt: $backgroundImageAlt, isPublished: $isPublished, publicationDate: $publicationDate}) {
+ input: {
+ name: $name,
+ slug: $slug,
+ description: $description,
+ descriptionJson: $descriptionJson,
+ products: $products,
+ backgroundImage: $backgroundImage,
+ backgroundImageAlt: $backgroundImageAlt,
+ isPublished: $isPublished,
+ publicationDate: $publicationDate}) {
collection {
name
slug
description
+ descriptionJson
products {
totalCount
}
@@ -89,12 +103,13 @@ def test_create_collection(
name = 'test-name'
slug = 'test-slug'
description = 'test-description'
+ description_json = json.dumps({'content': 'description'})
publication_date = date.today()
variables = {
'name': name, 'slug': slug, 'description': description,
- 'products': product_ids, 'backgroundImage': image_name,
- 'backgroundImageAlt': image_alt, 'isPublished': True,
- 'publicationDate': publication_date}
+ 'descriptionJson': description_json, 'products': product_ids,
+ 'backgroundImage': image_name, 'backgroundImageAlt': image_alt,
+ 'isPublished': True, 'publicationDate': publication_date}
body = get_multipart_request_body(query, variables, image_file, image_name)
response = staff_api_client.post_multipart(
body, permissions=[permission_manage_products])
@@ -103,6 +118,7 @@ def test_create_collection(
assert data['name'] == name
assert data['slug'] == slug
assert data['description'] == description
+ assert data['descriptionJson'] == description_json
assert data['publicationDate'] == publication_date.isoformat()
assert data['products']['totalCount'] == len(product_ids)
collection = Collection.objects.get(slug=slug)
diff --git a/tests/api/test_page.py b/tests/api/test_page.py
index ba23ad3312..b746c4cfa2 100644
--- a/tests/api/test_page.py
+++ b/tests/api/test_page.py
@@ -1,3 +1,5 @@
+import json
+
import graphene
import pytest
@@ -86,15 +88,17 @@ def test_page_create_mutation(staff_api_client, permission_manage_pages):
query = """
mutation CreatePage(
$slug: String!, $title: String!, $content: String!,
- $isPublished: Boolean!) {
+ $contentJson: JSONString!, $isPublished: Boolean!) {
pageCreate(
input: {
slug: $slug, title: $title,
- content: $content, isPublished: $isPublished}) {
+ content: $content, contentJson: $contentJson
+ isPublished: $isPublished}) {
page {
id
title
content
+ contentJson
slug
isPublished
}
@@ -107,13 +111,15 @@ def test_page_create_mutation(staff_api_client, permission_manage_pages):
"""
page_slug = 'test-slug'
page_content = 'test content'
+ page_content_json = json.dumps({'content': 'test content'})
page_title = 'test title'
- page_isPublished = True
+ page_is_published = True
# test creating root page
variables = {
'title': page_title, 'content': page_content,
- 'isPublished': page_isPublished, 'slug': page_slug}
+ 'contentJson': page_content_json, 'isPublished': page_is_published,
+ 'slug': page_slug}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_pages])
content = get_graphql_content(response)
@@ -121,8 +127,9 @@ def test_page_create_mutation(staff_api_client, permission_manage_pages):
assert data['errors'] == []
assert data['page']['title'] == page_title
assert data['page']['content'] == page_content
+ assert data['page']['contentJson'] == page_content_json
assert data['page']['slug'] == page_slug
- assert data['page']['isPublished'] == page_isPublished
+ assert data['page']['isPublished'] == page_is_published
def test_page_delete_mutation(staff_api_client, page, permission_manage_pages):
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 0b755530a6..c9b4102776 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1,3 +1,4 @@
+import json
from datetime import datetime
from unittest.mock import Mock, patch
@@ -440,9 +441,10 @@ def test_create_product(
query = """
mutation createProduct(
$productTypeId: ID!,
- $categoryId: ID!
+ $categoryId: ID!,
$name: String!,
$description: String!,
+ $descriptionJson: JSONString!,
$isPublished: Boolean!,
$chargeTaxes: Boolean!,
$taxRate: TaxRateType!,
@@ -454,6 +456,7 @@ def test_create_product(
productType: $productTypeId,
name: $name,
description: $description,
+ descriptionJson: $descriptionJson,
isPublished: $isPublished,
chargeTaxes: $chargeTaxes,
taxRate: $taxRate,
@@ -465,6 +468,7 @@ def test_create_product(
name
}
description
+ descriptionJson
isPublished
chargeTaxes
taxRate
@@ -497,6 +501,7 @@ def test_create_product(
category_id = graphene.Node.to_global_id(
'Category', category.pk)
product_description = 'test description'
+ product_description_json = json.dumps({'content': 'description'})
product_name = 'test name'
product_is_published = True
product_charge_taxes = True
@@ -519,6 +524,7 @@ def test_create_product(
'categoryId': category_id,
'name': product_name,
'description': product_description,
+ 'descriptionJson': product_description_json,
'isPublished': product_is_published,
'chargeTaxes': product_charge_taxes,
'taxRate': product_tax_rate,
@@ -534,6 +540,7 @@ def test_create_product(
assert data['errors'] == []
assert data['product']['name'] == product_name
assert data['product']['description'] == product_description
+ assert data['product']['descriptionJson'] == product_description_json
assert data['product']['isPublished'] == product_is_published
assert data['product']['chargeTaxes'] == product_charge_taxes
assert data['product']['taxRate'] == product_tax_rate