From 9863abb8a333ad2be9619b1571e62f823e199959 Mon Sep 17 00:00:00 2001 From: Jacob Rief Date: Mon, 27 Apr 2020 17:08:17 +0200 Subject: [PATCH] =?UTF-8?q?add=20context=20to=20templatetag=20=E2=80=98tra?= =?UTF-8?q?ns=E2=80=99=20for=20fine-grained=20translations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shop/search/documents.py | 67 +++++++++ shop/search/indexes.py | 136 ------------------ shop/templates/shop/breadcrumb/catalog.html | 2 +- shop/templates/shop/cart/editable.html | 10 +- shop/templates/shop/cart/static.html | 6 +- shop/templates/shop/cart/summary.html | 6 +- shop/templates/shop/cart/watch.html | 6 +- .../catalog/available-product-add2cart.html | 10 +- .../shop/catalog/bsp-scrollpanel.tmpl.html | 2 +- shop/templates/shop/catalog/list.html | 2 +- shop/templates/shop/catalog/pagination.html | 10 +- .../shop/catalog/product-add2cart.html | 24 ++-- .../shop/catalog/product-figure.html | 2 +- .../shop/checkout/billing-address-form.html | 6 +- .../shop/checkout/payment-method-form.html | 6 +- .../shop/checkout/purchase-button.html | 2 +- .../shop/checkout/required-form-fields.html | 2 +- .../shop/checkout/shipping-address-form.html | 6 +- .../shop/checkout/shipping-method-form.html | 6 +- shop/templates/shop/order/addendum-form.html | 2 +- shop/templates/shop/order/cancel-button.html | 2 +- shop/templates/shop/order/detail.html | 18 +-- 22 files changed, 132 insertions(+), 201 deletions(-) create mode 100644 shop/search/documents.py delete mode 100644 shop/search/indexes.py diff --git a/shop/search/documents.py b/shop/search/documents.py new file mode 100644 index 000000000..682890523 --- /dev/null +++ b/shop/search/documents.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.template.loader import select_template +from django.utils.html import strip_tags + +from django_elasticsearch_dsl import fields, Document + +from shop.models.product import ProductModel + + +# products = Index('products') +# products.settings( +# number_of_shards=1, +# number_of_replicas=0 +# ) + + +# @products.document +class ProductDocument(Document): + product_codes = fields.KeywordField( + multi=True, + ) + + product_type = fields.TextField( + attr='product_type', + ) + + body = fields.TextField() + + class Django: + model = ProductModel + fields = ['id', 'product_name'] + + def prepare(self, instance): + data = super().prepare(instance) + return data + + def get_queryset(self): + queryset = super().get_queryset() + return queryset.filter(active=True) + + def prepare_product_codes(self, instance): + variants = instance.get_product_variants() + product_codes = [ + v.product_code for v in variants if isinstance(getattr(v, 'product_code', None), str) + ] + if isinstance(getattr(instance, 'product_code', None), str): + product_codes.append(instance.product_code) + return product_codes + + def prepare_body(self, instance): + """ + Create a textual representation of the product's instance to be used by Elasticsearch for + creating a full text search index. + """ + app_label = instance._meta.app_label.lower() + params = [ + (app_label, instance.product_model), + (app_label, 'product'), + ('shop', 'product'), + ] + template = select_template(['{0}/search/indexes/{1}.txt'.format(*p) for p in params]) + body = template.render({'product': instance}) + print("------------") + print(strip_tags(body)) + return strip_tags(body) diff --git a/shop/search/indexes.py b/shop/search/indexes.py deleted file mode 100644 index 11d70f912..000000000 --- a/shop/search/indexes.py +++ /dev/null @@ -1,136 +0,0 @@ -from django.conf import settings -from django.template.loader import select_template -from django.utils import translation -from django.utils.html import strip_spaces_between_tags -from django.utils.safestring import mark_safe -# from haystack import indexes - -from elasticsearch_dsl import analyzer -from django_elasticsearch_dsl import Document, Index, fields -from django_elasticsearch_dsl.fields import TextField -from django_elasticsearch_dsl.registries import registry - -from shop.models.product import ProductModel - - -# class ProductIndex(indexes.SearchIndex): -# """ -# Abstract base class used to index all products for this shop -# """ -# text = indexes.CharField(document=True, use_template=True) -# autocomplete = indexes.EdgeNgramField(use_template=True) -# product_name = indexes.CharField(stored=True, indexed=False, model_attr='product_name') -# product_url = indexes.CharField(stored=True, indexed=False, model_attr='get_absolute_url') -# categories = indexes.MultiValueField(stored=True, indexed=False) -# -# def get_model(self): -# """ -# Hook to refer to the used Product model. Override this to create indices of -# specialized product models. -# """ -# return ProductModel -# -# def prepare(self, product): -# if hasattr(product, 'translations'): -# product.set_current_language(self.language) -# with translation.override(self.language): -# data = super(ProductIndex, self).prepare(product) -# return data -# -# def prepare_categories(self, product): -# category_fields = getattr(product, 'category_fields', []) -# category_ids = set(page.pk for field in category_fields for page in getattr(product, field).all()) -# return category_ids -# -# def render_html(self, prefix, product, postfix): -# """ -# Render a HTML snippet to be stored inside the index database, so that rendering of the -# product's list views can be performed without database queries. -# """ -# app_label = product._meta.app_label.lower() -# params = [ -# (app_label, prefix, product.product_model, postfix), -# (app_label, prefix, 'product', postfix), -# ('shop', prefix, 'product', postfix), -# ] -# template = select_template(['{0}/products/{1}-{2}-{3}.html'.format(*p) for p in params]) -# context = {'product': product} -# content = strip_spaces_between_tags(template.render(context).strip()) -# return mark_safe(content) -# -# def index_queryset(self, using=None): -# """Used when the entire index for model is updated.""" -# if using in dict(settings.LANGUAGES): -# self.language = using -# else: -# self.language = settings.LANGUAGE_CODE -# return self.get_model().objects.indexable() - - -html_strip = analyzer( - 'html_strip', - tokenizer="standard", - filter=["standard", "lowercase", "stop", "snowball"], - char_filter=["html_strip"] -) - - -class ProductDocument(Document): - caption = fields.TextField() - - catalog_media = fields.TextField( - index=False, - ) - - search_media = fields.TextField( - index=False, - ) - - product_url = fields.KeywordField( - index=False, - attr='get_absolute_url', - ) - - class Django: - model = ProductModel - fields = ['id', 'product_name'] - - # description = fields.TextField( - # analyzer=html_strip, - # fields={ - # 'raw': fields.TextField(analyzer='keyword'), - # } - # ) - # class Index: - # name = 'products' - # settings = {'number_of_shards': 1, 'number_of_replicas': 0} - - def get_queryset(self): - queryset = super().get_queryset() - return queryset.filter(active=True) - - def prepare_caption(self, instance): - return instance.caption - - def prepare_catalog_media(self, product): - return self.render_html('catalog', product, 'media') - - def prepare_search_media(self, product): - return self.render_html('search', product, 'media') - - def render_html(self, prefix, product, postfix): - """ - Render a HTML snippet to be stored inside the index database, so that rendering of the - product's list views can be performed without database queries. - """ - app_label = product._meta.app_label.lower() - params = [ - (app_label, prefix, product.product_model, postfix), - (app_label, prefix, 'product', postfix), - ('shop', prefix, 'product', postfix), - ] - template = select_template(['{0}/products/{1}-{2}-{3}.html'.format(*p) for p in params]) - context = {'product': product} - content = strip_spaces_between_tags(template.render(context).strip()) - return mark_safe(content) - diff --git a/shop/templates/shop/breadcrumb/catalog.html b/shop/templates/shop/breadcrumb/catalog.html index 8887edf34..532213fa4 100644 --- a/shop/templates/shop/breadcrumb/catalog.html +++ b/shop/templates/shop/breadcrumb/catalog.html @@ -9,7 +9,7 @@ diff --git a/shop/templates/shop/cart/editable.html b/shop/templates/shop/cart/editable.html index 8ae30c3f8..aa8c7b39f 100644 --- a/shop/templates/shop/cart/editable.html +++ b/shop/templates/shop/cart/editable.html @@ -16,7 +16,7 @@ {% block shop-cart-head %}
- {% trans "There are no items in the cart." %} + {% trans "There are no items in the cart." context "cart" %}
{% endblock shop-cart-head %} @@ -47,13 +47,13 @@
{% if shop_watch_list_url %}
{% endif %} @@ -70,7 +70,7 @@
- {% trans "Subtotal" %} + {% trans "Subtotal" context "cart" %}
@@ -81,7 +81,7 @@
-
{% trans "Total" %}
+
{% trans "Total" context "cart" %}
diff --git a/shop/templates/shop/cart/static.html b/shop/templates/shop/cart/static.html index 944f2da72..b1f598b4c 100644 --- a/shop/templates/shop/cart/static.html +++ b/shop/templates/shop/cart/static.html @@ -7,7 +7,7 @@ {% if cart.num_items == 0 %}
- {% trans "There are no items in the cart" %} + {% trans "There are no items in the cart." context "cart" %}
{% endif %} @@ -48,7 +48,7 @@
- {% trans "Subtotal" %} + {% trans "Subtotal" context "cart" %}
{{ cart.subtotal }} @@ -65,7 +65,7 @@
-
{% trans "Total" %}
+
{% trans "Total" context "cart" %}
{{ cart.total }}
diff --git a/shop/templates/shop/cart/summary.html b/shop/templates/shop/cart/summary.html index 72c6581cc..a6610a54e 100644 --- a/shop/templates/shop/cart/summary.html +++ b/shop/templates/shop/cart/summary.html @@ -2,11 +2,11 @@
  • - {% trans "Number of items" %}: + {% trans "Number of items" context "cart" %}: {{ cart.num_items }}
  • - {% trans "Subtotal" %}: + {% trans "Subtotal" context "cart" %}: {{ cart.subtotal }}
  • {% for extra_row in cart.extra_rows %} @@ -20,7 +20,7 @@
  • - {% trans "Total" %}: + {% trans "Total" context "cart" %}: {{ cart.total }}
diff --git a/shop/templates/shop/cart/watch.html b/shop/templates/shop/cart/watch.html index 5db96ce9b..cb4afbd2b 100644 --- a/shop/templates/shop/cart/watch.html +++ b/shop/templates/shop/cart/watch.html @@ -10,7 +10,7 @@ {% block shop-watch-head %}
- {% trans "You are perfectly happy!" %} + {% trans "You are perfectly happy!" context "cart" %}
{% endblock shop-watch-head %} @@ -26,10 +26,10 @@ {% block shop-watch-item-controls %}
{% endblock %} diff --git a/shop/templates/shop/catalog/available-product-add2cart.html b/shop/templates/shop/catalog/available-product-add2cart.html index 086777487..54cb03fea 100644 --- a/shop/templates/shop/catalog/available-product-add2cart.html +++ b/shop/templates/shop/catalog/available-product-add2cart.html @@ -3,7 +3,7 @@ {% block available-quantity-in-stock %}
- {% trans "Available" %} + {% trans "Available" context "catalog" %}
{% verbatim %}{{ context.availability.quantity }}{% endverbatim %}
@@ -12,21 +12,21 @@ {% block change-purchasing-quantity %}
- +
- {% trans "Currently not available." %} + {% trans "Currently not available." context "catalog" %}
{% endblock %} {% block limited-availability %}
- {% trans "Deliverable from {{ context.availability.earliest|date:'d. MMM.' }} onwards." %} + {% trans "Deliverable from {{ context.availability.earliest|date:'d. MMM.' }} onwards." context "catalog" %}
- {% trans "Available no later than {{ context.availability.latest|date:'d. MMM. H:mm' }}." %} + {% trans "Available no later than {{ context.availability.latest|date:'d. MMM. H:mm' }}." context "catalog" %}
{% endblock %} diff --git a/shop/templates/shop/catalog/bsp-scrollpanel.tmpl.html b/shop/templates/shop/catalog/bsp-scrollpanel.tmpl.html index f2b8f9d1d..be2751589 100644 --- a/shop/templates/shop/catalog/bsp-scrollpanel.tmpl.html +++ b/shop/templates/shop/catalog/bsp-scrollpanel.tmpl.html @@ -1,4 +1,4 @@ -{% load i18n static sekizai_tags sass_tags %} +{% load static sekizai_tags sass_tags %} {% addtoblock "css" %}{% endaddtoblock %} {% addtoblock "js" %}{% endaddtoblock %} {% add_data "ng-requires" "bs-plus.scrollpanel" %} diff --git a/shop/templates/shop/catalog/list.html b/shop/templates/shop/catalog/list.html index 0cdecb448..080842433 100644 --- a/shop/templates/shop/catalog/list.html +++ b/shop/templates/shop/catalog/list.html @@ -1,4 +1,4 @@ -{% load i18n static sekizai_tags sass_tags %} +{% load static sekizai_tags sass_tags %} {% addtoblock "js" %}{% endaddtoblock %} {% addtoblock "js" %}{% endaddtoblock %} diff --git a/shop/templates/shop/catalog/pagination.html b/shop/templates/shop/catalog/pagination.html index e58fb6709..286df1aed 100644 --- a/shop/templates/shop/catalog/pagination.html +++ b/shop/templates/shop/catalog/pagination.html @@ -4,7 +4,7 @@ {% addtoblock "css" %}{% endaddtoblock %} {% if paginator.display_page_controls %} -