Skip to content

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

Change format_address to use template and support inline rendering, update templates to use tag instead of template inclusion

Rafał Pitoń060c7573d650
diff --git a/saleor/userprofile/templatetags/i18n_address_tags.py b/saleor/userprofile/templatetags/i18n_address_tags.py
index 060d2f16df..46bc9c1c6d 100644
--- a/saleor/userprofile/templatetags/i18n_address_tags.py
+++ b/saleor/userprofile/templatetags/i18n_address_tags.py
@@ -1,5 +1,4 @@
from django import template
-from django.utils.safestring import mark_safe
from django.utils.translation import pgettext
import i18naddress
@@ -8,8 +7,8 @@ from ...userprofile.models import Address
register = template.Library()
-def format_address(address, latin=False):
[email protected]_tag('formatted_address.html')
+def format_address(address, include_phone=True, inline=False, latin=False):
address_data = Address.objects.as_data(address)
address_data['name'] = pgettext(
'Address data', '%(first_name)s %(last_name)s') % address_data
@@ -18,5 +17,7 @@ def format_address(address, latin=False):
'Address data',
'%(street_address_1)s\n'
'%(street_address_2)s' % address_data)
- formatted_address = i18naddress.format_address(address_data, latin)
- return mark_safe(formatted_address)
+ address_lines = i18naddress.format_address(address_data, latin).split('\n')
+ if include_phone and address.phone:
+ address_lines.append(address.phone)
+ return {'address_lines': address_lines, 'inline': inline}
diff --git a/templates/checkout/shipping_method.html b/templates/checkout/shipping_method.html
index 3583a5b0ea..dad53853c4 100644
--- a/templates/checkout/shipping_method.html
+++ b/templates/checkout/shipping_method.html
@@ -1,11 +1,12 @@
{% extends "checkout/details.html" %}
{% load i18n %}
+{% load i18n_address_tags %}
{% load gross from prices_i18n %}
{% load bootstrap_form from bootstrap4 %}
{% block forms %}
<h3>{% trans "Shipping address" context "Checkout shipping address title" %}</h3>
- {% include "userprofile/snippets/address_short.html" with address=checkout.shipping_address only %}
+ {% format_address checkout.shipping_address %}
<p><a href="{% url 'checkout:shipping-address' %}">{% trans "Select other address" %}</a></p>
<hr>
<form method="post" novalidate>
diff --git a/templates/checkout/snippets/addresses_form_panel.html b/templates/checkout/snippets/addresses_form_panel.html
index ef3bbb42a9..a70d29fc3c 100644
--- a/templates/checkout/snippets/addresses_form_panel.html
+++ b/templates/checkout/snippets/addresses_form_panel.html
@@ -1,4 +1,5 @@
-<div class="radio {% if address or label == _('Same as shipping') %} address_hide{% else %} address_show{% endif %}">
+ {% load i18n_address_tags %}
+ <div class="radio {% if address or label == _('Same as shipping') %} address_hide{% else %} address_show{% endif %}">
<label>
<input type="radio"
name="{{ radio_name }}"
@@ -6,7 +7,7 @@
id="address_{{ radio_value }}"
{% if radio_value == value %}checked{% endif %}>
{% if address %}
- {% include "userprofile/snippets/address_short.html" with address=address only %}
+ {% format_address address %}
{% else %}
{{ label }}
{% endif %}
diff --git a/templates/dashboard/includes/_address.html b/templates/dashboard/includes/_address.html
index d9c55335e8..b67036bb6b 100644
--- a/templates/dashboard/includes/_address.html
+++ b/templates/dashboard/includes/_address.html
@@ -1,8 +1,8 @@
-{% load format_address from i18n_address_tags %}
+{% load i18n_address_tags %}
{% load staticfiles %}
<div class="address">
- <div class="formatted-address">{% format_address address latin=True %}</div>
+ {% format_address address include_phone=False latin=True %}
{% if address.phone %}
<div>
<svg data-src="{% static "dashboard/images/phone.svg" %}" width="16" height="16" class="left" />
diff --git a/templates/formatted_address.html b/templates/formatted_address.html
new file mode 100644
index 0000000000..0001cb4dc1
--- /dev/null
+++ b/templates/formatted_address.html
@@ -0,0 +1,3 @@
+<div class="address-details" data-content="{{ address_lines|slice:'1:'|join:'<br>' }}" data-html="true" title="{{ address_lines.0 }}">
+ {% for line in address_lines %} {{ line }}{% if not forloop.last %}{% if inline %}, {% else %}<br>{% endif %}{% endif %} {% endfor %}
+</div>
\ No newline at end of file
diff --git a/templates/order/_ordered_items_table.html b/templates/order/_ordered_items_table.html
index 66f67c7842..ccbbf6d2b9 100644
--- a/templates/order/_ordered_items_table.html
+++ b/templates/order/_ordered_items_table.html
@@ -1,4 +1,5 @@
{% load i18n %}
+{% load i18n_address_tags %}
{% load gross from prices_i18n %}
{% load discount_amount_for from prices %}
{% load product_first_image from product_images %}
@@ -13,14 +14,14 @@
<h3>
{% trans "Shipping address" context "Order details address title" %}
</h3>
- <address>{% include "userprofile/snippets/address_short.html" with address=order.shipping_address %}</address>
+ {% format_address order.shipping_address %}
</div>
{% endif %}
<div class="col-6">
<h3>
{% trans "Billing address" context "Order details address title" %}
</h3>
- <address>{% include "userprofile/snippets/address_short.html" with address=order.billing_address %}</address>
+ {% format_address order.billing_address %}
</div>
</div>
</div>
diff --git a/templates/userprofile/details.html b/templates/userprofile/details.html
index 5c2f789893..f93d32421f 100644
--- a/templates/userprofile/details.html
+++ b/templates/userprofile/details.html
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
+{% load i18n_address_tags %}
{% load staticfiles %}
{% load gross from prices_i18n %}
{% load discount_amount_for from prices %}
@@ -118,7 +119,7 @@
{% for address in addresses %}
<div class="col-md-6">
<div class="card card-block">
- {% include "userprofile/snippets/address_short.html" %}
+ {% format_address address %}
<ul class="icons">
<li><a href="{% url "profile:address-edit" pk=address.pk %}">
{% trans "Edit" context "User address action" %}