Skip to content

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

Simplyfy paging logic

Tomasz Szymanski32cf174245b0
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 48dd379e03..c9f2193073 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -355,33 +355,27 @@ def _chunk_products(products, product_limit):
return chunks
-def _get_page_count(products_first_page, rest_of_products):
- """Calculate number of PDF pages.
+def _get_product_limit_first_page(products):
+ MAX_PRODUCTS_WITH_TABLE = 3
+ MAX_PRODUCTS_WITHOUT_TABLE = 4
- Keep track of summary table that might be displayed on last page
- if product count is too big to include table on that page.
- """
- if not rest_of_products and len(products_first_page) == 4:
- return 2
- try:
- append_last_page = 1 if len(rest_of_products[-1]) > 11 else 0
- return 1 + len(rest_of_products) + append_last_page
- except IndexError:
- return 1
+ if len(products) < MAX_PRODUCTS_WITHOUT_TABLE:
+ return MAX_PRODUCTS_WITH_TABLE
+
+ return MAX_PRODUCTS_WITHOUT_TABLE
def generate_invoice_pdf_for_order(invoice):
logo_path = static_finders.find("images/logo.svg")
- MAX_PRODUCTS_FIRST_PAGE = 4
MAX_PRODUCTS_PER_PAGE = 13
all_products = invoice.order.lines.all()
- _product_limit = (
- 3 if len(all_products) < MAX_PRODUCTS_FIRST_PAGE else MAX_PRODUCTS_FIRST_PAGE
- )
- products_first_page = all_products[:_product_limit]
+
+ product_limit_first_page = _get_product_limit_first_page(all_products)
+
+ products_first_page = all_products[:product_limit_first_page]
rest_of_products = _chunk_products(
- all_products[_product_limit:], MAX_PRODUCTS_PER_PAGE
+ all_products[product_limit_first_page:], MAX_PRODUCTS_PER_PAGE
)
rendered_template = get_template("invoice.html").render(
@@ -392,7 +386,6 @@ def generate_invoice_pdf_for_order(invoice):
"logo_path": f"file://{logo_path}",
"products_first_page": products_first_page,
"rest_of_products": rest_of_products,
- "page_count": _get_page_count(products_first_page, rest_of_products),
}
)
content_file = ContentFile(HTML(string=rendered_template).write_pdf())
diff --git a/templates/invoice.html b/templates/invoice.html
index 96c72b3b5f..9095cd62a9 100644
--- a/templates/invoice.html
+++ b/templates/invoice.html
@@ -8,6 +8,15 @@
@page {
background-color: #FAFAFA;
margin: 0.5cm;
+ @bottom-right {
+ content: counter(page) " of " counter(pages);
+ font-style: normal;
+ font-weight: normal;
+ font-size: 13px;
+ letter-spacing: 0.02em;
+ color: rgba(40, 35, 74, 0.6);
+ margin: -15px 28px 40px 0;
+ }
}
body {
@@ -33,7 +42,7 @@
.header-category {
font-style: normal;
font-weight: normal;
- font-size: 14px;
+ font-size: 13px;
letter-spacing: 0.02em;
color: rgba(40, 35, 74, 0.6);
@@ -46,7 +55,7 @@
font-style: normal;
font-weight: bold;
- font-size: 14px;
+ font-size: 13px;
letter-spacing: 0.02em;
color: #28234A;
}
@@ -55,7 +64,7 @@
display: block;
font-style: normal;
font-weight: 600;
- font-size: 19px;
+ font-size: 18px;
letter-spacing: 0.02em;
padding-bottom: 5px;
color: #28234A;
@@ -74,6 +83,7 @@
}
.normal-text {
+ font-size: 14px;
color: #28234A;
line-height: 1.4;
}
@@ -136,16 +146,6 @@
border-bottom: 2px solid #EFF5F8;
}
- .page-counter {
- float: right;
- }
-
- .page-footer {
- position: absolute;
- bottom: 20px;
- right: 20px;
- }
-
.page-break {
page-break-before: always;
}
@@ -286,32 +286,18 @@
<div class="content-padded">
<span class="header-title">{% trans "ITEMS ORDERED" context "Invoice PDF table header" %}</span>
{% include "invoice_products_table.html" with products=products_first_page %}
- <div class="page-footer">
- <span class="page-counter header-category">
- 1 / {{ page_count }}
- </span>
- </div>
{% if not rest_of_products|length %}
{% if products_first_page|length == 4 %}
<div class="page-break"></div>
- <div class="page-footer">
- <span class="page-counter header-category">2 / 2</span>
- </div>
{% endif %}
{% include "invoice_summary_table.html" with order=order %}
{% else %}
{% for product_set in rest_of_products %}
<div class="page-break"></div>
- <div class="page-footer">
- <span class="page-counter header-category">{{ forloop.counter|add:1 }} / {{ page_count }}</span>
- </div>
{% include "invoice_products_table.html" with products=product_set %}
{% if forloop.last %}
{% if product_set|length > 11 %}
<div class="page-break"></div>
- <div class="page-footer">
- <span class="page-counter header-category">{{ forloop.counter|add:2 }} / {{ page_count }}</span>
- </div>
{% endif %}
{% include "invoice_summary_table.html" with order=order %}
{% endif %}
diff --git a/templates/invoice_products_table.html b/templates/invoice_products_table.html
index 361e4a64bf..60ed0769da 100644
--- a/templates/invoice_products_table.html
+++ b/templates/invoice_products_table.html
@@ -20,7 +20,7 @@
<tr class="row-product">
<td class="cell-product">
<span class="normal-text">
- {{ product|truncatechars:30 }}
+ {{ product|truncatechars:35 }}
<br />
SKU: {{ product.product_sku }}
</span>