Skip to content

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

Move logic out of models

Pacu25919a9e65649
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 899a1de89e..c3b4f28273 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -196,7 +196,7 @@ class ProductForm(forms.ModelForm, AttributesMixin):
'category': pgettext_lazy('Category', 'Category'),
'price': pgettext_lazy('Currency amount', 'Price'),
'available_on': pgettext_lazy(
- 'Availability date', 'Availability date'),
+ 'Availability date', 'Publish product on'),
'is_published': pgettext_lazy(
'Product published toggle', 'Published'),
'is_featured': pgettext_lazy(
diff --git a/saleor/dashboard/seo/fields.py b/saleor/dashboard/seo/fields.py
index aa8cc98eb3..3561f0ed87 100644
--- a/saleor/dashboard/seo/fields.py
+++ b/saleor/dashboard/seo/fields.py
@@ -14,38 +14,42 @@ TITLE_MAX_LENGTH = SeoModel._meta.get_field('seo_title').max_length
class SeoTitleField(forms.CharField):
- help_text = SEO_FIELD_HELP_TEXT
- label = pgettext_lazy(
- (
- 'Field name, ',
- 'Title that will be used to describe page in Search Engines'),
- 'SEO Title')
widget = CharsLeftWidget(
attrs={
'data-min-recommended-length': MIN_TITLE_LENGTH,
'maxlength': TITLE_MAX_LENGTH})
- def __init__(self, extra_attrs=None, *args, **kwargs):
+ def __init__(self, extra_attrs=None, required=False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.max_length = TITLE_MAX_LENGTH
if extra_attrs:
self.widget.attrs.update(extra_attrs)
+ self.required = required
+ self.help_text = SEO_FIELD_HELP_TEXT
+ self.label = pgettext_lazy(
+ (
+ 'Field name, ',
+ 'Title that will be used to describe page in Search Engines'),
+ 'SEO Title')
class SeoDescriptionField(forms.CharField):
help_text = SEO_FIELD_HELP_TEXT
- label = pgettext_lazy(
- (
- 'Field name, Meta Description is page summary '
- 'used by Search Engines'),
- 'Meta Description'),
+
widget = CharsLeftWidget(
attrs={
+ 'help_text': SEO_FIELD_HELP_TEXT,
'data-min-recommended-length': MIN_DESCRIPTION_LENGTH,
'maxlength': DESCRIPTION_MAX_LENGTH})
- def __init__(self, extra_attrs=None, *args, **kwargs):
+ def __init__(self, extra_attrs=None, required=False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.max_length = DESCRIPTION_MAX_LENGTH
if extra_attrs:
self.widget.attrs.update(extra_attrs)
+ self.required = required
+ self.help_text = SEO_FIELD_HELP_TEXT
+ self.label = pgettext_lazy(
+ ('Field name, Meta Description is page '
+ 'summary used by Search Engines'),
+ 'Meta Description')
diff --git a/saleor/page/models.py b/saleor/page/models.py
index 9d335fb8d4..212f4356da 100644
--- a/saleor/page/models.py
+++ b/saleor/page/models.py
@@ -43,9 +43,3 @@ class Page(SeoModel):
def get_full_url(self):
return build_absolute_uri(self.get_absolute_url())
-
- def get_seo_title(self):
- return self.seo_title or self.title
-
- def get_seo_description(self):
- return self.seo_description or ''
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 1fc7d8e522..e4475cd307 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -60,12 +60,6 @@ class Category(MPTTModel, SeoModel):
nodes = [node for node in ancestors] + [self]
return '/'.join([node.slug for node in nodes])
- def get_seo_title(self):
- return self.seo_title or self.name
-
- def get_seo_description(self):
- return self.seo_description or self.description or ''
-
class ProductType(models.Model):
name = models.CharField(max_length=128)
@@ -182,12 +176,6 @@ class Product(SeoModel):
grosses = sorted(grosses, key=lambda x: x.tax)
return TaxedMoneyRange(min(grosses), max(grosses))
- def get_seo_title(self):
- return self.seo_title or self.name
-
- def get_seo_description(self):
- return self.seo_description or ''
-
class ProductVariant(models.Model):
sku = models.CharField(max_length=32, unique=True)
@@ -415,9 +403,3 @@ class Collection(SeoModel):
return reverse(
'product:collection',
kwargs={'pk': self.id, 'slug': self.slug})
-
- def get_seo_title(self):
- return self.seo_title or self.name
-
- def get_seo_description(self):
- return self.seo_description or ''
diff --git a/saleor/static/dashboard/scss/components/_google-preview.scss b/saleor/static/dashboard/scss/components/_google-preview.scss
index a9d5f66507..58e987ae0d 100644
--- a/saleor/static/dashboard/scss/components/_google-preview.scss
+++ b/saleor/static/dashboard/scss/components/_google-preview.scss
@@ -1,9 +1,7 @@
.google-preview {
text-align: left;
letter-spacing: initial;
- padding-top: 1rem;
&__container {
- margin: 12px;
background-color: #fff;
max-width: 600px;
}
diff --git a/templates/dashboard/includes/_google_preview.html b/templates/dashboard/includes/_google_preview.html
index 445b82e1f3..8ea2fe0b6a 100644
--- a/templates/dashboard/includes/_google_preview.html
+++ b/templates/dashboard/includes/_google_preview.html
@@ -1,10 +1,9 @@
{% load build_absolute_uri from urls %}
{% load materializecss %}
{% load i18n %}
-
+<span class="card-title">{% trans "Search engine listing preview" context "Title of the box showing preview from the search engine perspective" %}</span>
<div class='google-preview'>
<div class='google-preview__container'>
- <p class="flow-text">Search engine listing preview</p>
<br>
<div id="preview-error"></div>
<div id="google-preview">
diff --git a/templates/dashboard/product/form.html b/templates/dashboard/product/form.html
index fe7c36dcfe..8f43ec6c42 100644
--- a/templates/dashboard/product/form.html
+++ b/templates/dashboard/product/form.html
@@ -64,25 +64,24 @@
<form method="post" class="card" id="form-product" novalidate>
{% csrf_token %}
<div class="card-content">
- <span class="card-title">
- {{ product.product_type }}
- </span>
<div class="row">
<div class="col s12 l8" >
<div class="row">
{{ product_form.name|materializecss }}
</div>
- {% for attribute_field in product_form.iter_attribute_fields %}
- <div class="row attribute-select-or-create">
- {{ attribute_field|materializecss:"input-field s12" }}
- </div>
- {% endfor %}
<div class="row">
{{ product_form.description|materializecss }}
</div>
+ <span class="card-title">Availability</span>
<div class="row">
- {% include "dashboard/includes/_google_preview.html" with object=product form=product_form %}
+ <div class="col s12 m3">
+ {{ product_form.is_published|materializecss }}
+ </div>
+ <div class="col s12 m5">
+ {{ product_form.available_on|materializecss }}
+ </div>
</div>
+ {% include "dashboard/includes/_google_preview.html" with object=product form=product_form %}
</div>
<div class="col s12 l4">
<div class="row">
@@ -96,18 +95,27 @@
<div class="row">
{{ product_form.category|materializecss }}
</div>
- <div class="row">
- {{ product_form.available_on|materializecss }}
- </div>
<div class="row">
{{ product_form.collections|materializecss }}
</div>
<div class="row">
- {{ product_form.is_featured|materializecss }}
+ {{ product_form.is_featured|materializecss }}
</div>
<div class="row">
- {{ product_form.is_published|materializecss }}
+ <span class="card-title">{% trans "Attributes" %}</span>
</div>
+ <div class="row">
+ <h6>
+ {% blocktrans with product_type_name=product.product_type %}
+ Product Type: {{ product_type_name }}
+ {% endblocktrans %}
+ </h6>
+ </div>
+ {% for attribute_field in product_form.iter_attribute_fields %}
+ <div class="row attribute-select-or-create">
+ {{ attribute_field|materializecss:"input-field s12" }}
+ </div>
+ {% endfor %}
</div>
</div>
</div>
diff --git a/templates/page/details.html b/templates/page/details.html
index 18884900ec..160a1fefb8 100644
--- a/templates/page/details.html
+++ b/templates/page/details.html
@@ -12,9 +12,9 @@
{% endblock %}
{% block meta_tags %}
- <meta property="og:title" content="{{ page.get_seo_title }}">
- <meta property="og:description" content="{{ page.get_seo_description }}">
- <meta name="description" content="{{ page.get_seo_description }}">
+ <meta property="og:title" content="{{ page.seo_title|default:page.title }}">
+ <meta property="og:description" content="{{ page.seo_description|default:"" }}">
+ <meta name="description" content="{{ page.seo_description|default:"" }}">
{% build_absolute_uri request=request location=page.get_absolute_url as page_url %}
<meta property="og:url" content="{{ page_url }}">
diff --git a/templates/product/details.html b/templates/product/details.html
index b81ccd018e..5dad950f16 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -19,9 +19,9 @@
{% endblock %}
{% block meta_tags %}
- <meta property="og:title" content="{{ product.get_seo_title }}">
- <meta property="og:description" content="{{ product.get_seo_description }}">
- <meta name="description" content="{{ product.get_seo_description }}">
+ <meta property="og:title" content="{{ product.seo_title|default:product.name }}">
+ <meta property="og:description" content="{{ product.seo_description|default:product.description }}">
+ <meta name="description" content="{{ product.seo_description|default:product.description }}">
{% build_absolute_uri request=request location=product.get_absolute_url as product_url %}
<meta property="og:url" content="{{ product_url }}">
diff --git a/templates/product/product_list_base.html b/templates/product/product_list_base.html
index 0125dc0981..9bdcf23b0c 100644
--- a/templates/product/product_list_base.html
+++ b/templates/product/product_list_base.html
@@ -19,10 +19,16 @@
{% endblock %}
{% block meta_tags %}
- <meta property="og:title" content="{{ object.get_seo_title }}">
- <meta property="og:description" content="{{ object.get_seo_description }}">
+ <meta property="og:title" content="{{ object.seo_title|default:object.name }}">
+
+ <meta name="description" content="
+ {% if object.seo_description %}
+ {{ object.seo_description }}
+ {% elif object.description %}
+ {{ object.description }}
+ {% endif %}
+ ">
<meta property="og:image" content="{% static "images/block1.png" %}">
- <meta name="description" content="{{ object.get_seo_description }}">
{% build_absolute_uri request=request location=object.get_absolute_url as object_url %}
<meta property="og:url" content="{{ object_url }}">
diff --git a/tests/test_collection.py b/tests/test_collection.py
index abfc188788..fbd31e631e 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -1,8 +1,6 @@
-import pytest
-
from django.urls import reverse
-from .utils import get_redirect_location, seo_field_test_helper
+from .utils import get_redirect_location
def test_collection_index(client, collection):
@@ -33,29 +31,3 @@ def test_collection_not_exists(client):
url = reverse('product:collection', kwargs=url_kwargs)
response = client.get(url)
assert response.status_code == 404
-
-
- 'title, seo_title, expected_result',
- (
- ('Title', 'Seo Title', 'Seo Title'),
- ('Title', None, 'Title')))
-def test_page_get_seo_title(
- admin_client, collection, title, seo_title, expected_result):
- seo_field_test_helper(
- collection, 'get_seo_title',
- {'name': title, 'seo_title': seo_title},
- expected_result)
-
-
- 'seo_description, expected_result',
- (
- ('Seo', 'Seo'),
- (None, '')))
-def test_page_get_seo_description(
- admin_client, collection, seo_description, expected_result):
- seo_field_test_helper(
- collection, 'get_seo_description',
- {'seo_description': seo_description},
- expected_result)
diff --git a/tests/test_page.py b/tests/test_page.py
index b00dfc75c5..f89ff57979 100644
--- a/tests/test_page.py
+++ b/tests/test_page.py
@@ -1,8 +1,3 @@
-import pytest
-
-from .utils import seo_field_test_helper
-
-
def test_draft_page_detail(client, admin_client, page):
page_url = page.get_absolute_url()
response = client.get(page_url)
@@ -19,29 +14,3 @@ def test_public_page_detail(client, admin_client, page):
assert response.status_code == 200
response = admin_client.get(page_url)
assert response.status_code == 200
-
-
- 'title, seo_title, expected_result',
- (
- ('Title', 'Seo Title', 'Seo Title'),
- ('Title', None, 'Title')))
-def test_page_get_seo_title(
- admin_client, page, title, seo_title, expected_result):
- seo_field_test_helper(
- page, 'get_seo_title',
- {'title': title, 'seo_title': seo_title},
- expected_result)
-
-
- 'seo_description, expected_result',
- (
- ('Seo', 'Seo'),
- (None, '')))
-def test_page_get_seo_description(
- admin_client, page, seo_description, expected_result):
- seo_field_test_helper(
- page, 'get_seo_description',
- {'seo_description': seo_description},
- expected_result)
diff --git a/tests/test_product.py b/tests/test_product.py
index a115e85481..cfc83a7712 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -5,7 +5,7 @@ from unittest.mock import Mock
import pytest
from django.urls import reverse
from django.utils.encoding import smart_text
-from tests.utils import filter_products_by_attribute, seo_field_test_helper
+from tests.utils import filter_products_by_attribute
from saleor.cart import CartStatus, utils
from saleor.cart.models import Cart
@@ -532,57 +532,3 @@ def test_include_products_from_subcategories_in_main_view(
'path': path, 'category_id': default_category.pk})
response = authorized_client.get(url)
assert product in response.context_data['products'][0]
-
-
- 'title, seo_title, expected_result',
- (
- ('Title', 'Seo Title', 'Seo Title'),
- ('Title', None, 'Title')))
-def test_product_get_seo_title(
- admin_client, product_in_stock, title, seo_title, expected_result):
- seo_field_test_helper(
- product_in_stock, 'get_seo_title',
- {'name': title, 'seo_title': seo_title},
- expected_result)
-
-
- 'description, seo_description, expected_result',
- (
- ('Description', 'Seo', 'Seo'),
- ('Description', None, '')))
-def test_product_get_seo_description(
- admin_client, product_in_stock, description, seo_description,
- expected_result):
- seo_field_test_helper(
- product_in_stock, 'get_seo_description',
- {'description': description, 'seo_description': seo_description},
- expected_result)
-
-
- 'title, seo_title, expected_result',
- (
- ('Title', 'Seo Title', 'Seo Title'),
- ('Title', None, 'Title')))
-def test_category_get_seo_title(
- admin_client, default_category, title, seo_title, expected_result):
- seo_field_test_helper(
- default_category, 'get_seo_title',
- {'name': title, 'seo_title': seo_title},
- expected_result)
-
-
- 'description, seo_description, expected_result',
- (
- ('Description', 'Seo', 'Seo'),
- ('Description', None, 'Description')))
-def test_category_get_seo_description(
- admin_client, default_category, description, seo_description,
- expected_result):
- seo_field_test_helper(
- default_category, 'get_seo_description',
- {'description': description, 'seo_description': seo_description},
- expected_result)
diff --git a/tests/utils.py b/tests/utils.py
index a00c813ead..f79b74b5f5 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -25,13 +25,3 @@ def filter_products_by_attribute(queryset, attribute_id, value):
def get_graphql_content(response):
return json.loads(response.content.decode('utf8'))
-
-
-def seo_field_test_helper(object, function_name, field_values, expected_result):
- """Helper for testing seo fields."""
- for field_name, field_value in field_values.items():
- setattr(object, field_name, field_value)
- object.save()
- get_seo_field_function = getattr(object, function_name)
- result = get_seo_field_function()
- assert expected_result == result