فهرست منبع

Optimization of checkout page to decrease loading times.

template.py - passing products list to get_product_by_product_id

db.py - removed extra read_folder call in the function using already existing product list from previous call.
slow_tiger 1 سال پیش
والد
کامیت
a8e53fe426
2فایلهای تغییر یافته به همراه14 افزوده شده و 11 حذف شده
  1. 12 10
      app/db.py
  2. 2 1
      app/template.py

+ 12 - 10
app/db.py

@@ -195,24 +195,26 @@ def get_products_by_category(settings):
 
 
 def get_product_by_product_id(
-    settings: Settings, product_id: str
+    settings: Settings, 
+    product_id: str,
+    products = []
 ) -> DocumentProduct:
     """Fetch a product by its product-id."""
-
-    products = read_dir(
-        settings.git_repo,
-        settings.document_match,
-        settings.document_exclude,
-        settings.block_types,
-        exclude_doc="products",
-        tree="products",
+    if not products:
+        products = read_dir(
+            settings.git_repo,
+            settings.document_match,
+            settings.document_exclude,
+            settings.block_types,
+            exclude_doc="products",
+            tree="products",
     )
 
     product = None
 
     for p in products:
         if p.meta.product_id == product_id:
-            product = p
+            return p
 
     return product
 

+ 2 - 1
app/template.py

@@ -1,6 +1,7 @@
 import os
 import random
 import hashlib
+import time
 from pathlib import Path
 from typing import Literal
 
@@ -115,7 +116,7 @@ def prepare_checkout(
     # map over each item in the cart and create a new item object
     checkout_items = []
     for item in cart.items.values():
-        product = get_product_by_product_id(settings, item.product_id)
+        product = get_product_by_product_id(settings, item.product_id, products)
         
         checkout_item = {
             "path": product.meta.path,