From 576877d61b34ad9048b71a13e6f0a3628d7650f3 Mon Sep 17 00:00:00 2001 From: Martin Carlsson Date: Wed, 11 Mar 2020 14:14:07 +0100 Subject: [PATCH] P5-4906 optimize framed print listprice --- api/resources/prices.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/api/resources/prices.py b/api/resources/prices.py index f5b62ba..2e2323c 100644 --- a/api/resources/prices.py +++ b/api/resources/prices.py @@ -597,26 +597,36 @@ def list_prices_posters(territory): @mod.route("/list-prices//framed-print") def list_prices_framed_prints(territory): - framed_prints = PrintProduct.query.framed_prints().all() + from api.extensions import db + from types import SimpleNamespace + + query = ( + "SELECT prints.printid, designers.pricepremium_framed_print, " + 'width.value AS width, height.value AS height FROM "product-printproducts" prints ' + 'JOIN "product-products" products ON prints.productid = products.productid ' + 'LEFT JOIN "product-products_fields" width ON width.fieldid = 5 and width.productid = products.productid ' + 'LEFT JOIN "product-products_fields" height ON height.fieldid = 3 and height.productid = products.productid ' + "LEFT JOIN designers ON designers.designerid = products.designerid " + "WHERE prints.groupid = :group" + ) + rows = db.session.execute(query, {"group": 8}) + market = market_model.from_territory(territory) result = {} - for framed_print in framed_prints: - product_fields = framed_print.product.fields - width = None - height = None - for product_field in product_fields: - if product_field.field.field == "width": - width = product_field.value - if product_field.field.field == "height": - height = product_field.value + for row in rows: + printid, pricepremium_framed_print, width, height = row + + if not pricepremium_framed_print: + pricepremium_framed_print = 0 + + designer = SimpleNamespace(pricepremium_framed_print=pricepremium_framed_print) if width == height: sku = pwinty.GLOBAL_CFP_12x12 else: sku = pwinty.GLOBAL_CFP_11x14 - result[framed_print.id] = framed_print_price( - sku=sku, market=market, designer=framed_print.product.designer - ) + result[printid] = framed_print_price(sku=sku, market=market, designer=designer) + return jsonify(result)