From 292726e8c311fa224b5403cb3f034dfb5e7bfbc5 Mon Sep 17 00:00:00 2001 From: Fredrik Ringqvist <35255659+fredrikphotowall@users.noreply.github.com> Date: Wed, 8 Jun 2022 14:34:11 +0200 Subject: [PATCH] Revert "Remove listprice code and read price adjustments from database (#56)" (#57) This reverts commit cdcefda954316c88b9947fc34b7ccc7b5998a07a. --- api/lib/prices.py | 15 ++++++-- api/models/price_adjustments.py | 34 ------------------ api/resources/price-adjustments.yaml | 20 +++++++++++ api/resources/prices.py | 53 ++++++++++++++++++++++++++-- 4 files changed, 83 insertions(+), 39 deletions(-) delete mode 100644 api/models/price_adjustments.py create mode 100644 api/resources/price-adjustments.yaml diff --git a/api/lib/prices.py b/api/lib/prices.py index 60bdd9b..530e558 100644 --- a/api/lib/prices.py +++ b/api/lib/prices.py @@ -1,10 +1,15 @@ # coding=UTF-8 """ Price formulas """ -from api.lib.utils import round_half_up +import yaml +from api.lib.utils import lru_with_ttl, round_half_up from api.models.external_print_product import get_external_print_product -from api.models.price_adjustments import get_price_adjustment +@lru_with_ttl(ttl_seconds=3600) +def get_adjustment_config(): + with open("api/resources/price-adjustments.yaml") as f: + return yaml.load(f, Loader=yaml.FullLoader) + def wallpaper_m2_price( material, market, @@ -29,8 +34,12 @@ def wallpaper_m2_price( price *= market.exchange_rate price *= market.price_adjustments + adjustment_config = get_adjustment_config() + inquiry_or_product_key = "product" if inquiry is None else "inquiry" - price *= get_price_adjustment(market.name, material.name, inquiry_or_product_key) + + adj_value = adjustment_config.get(material.name).get(inquiry_or_product_key).get(market.name, 1.0) + price *= adj_value if inc_vat: price *= market.vat diff --git a/api/models/price_adjustments.py b/api/models/price_adjustments.py deleted file mode 100644 index 5abfec1..0000000 --- a/api/models/price_adjustments.py +++ /dev/null @@ -1,34 +0,0 @@ -# coding=UTF-8 - -from api.extensions import db -from api.lib.utils import lru_with_ttl - -@lru_with_ttl(ttl_seconds=3600) -def get_adjustment_config(): - sql = """ - select - material, product_type, markets.name, price_adjustments.value - from - price_adjustments - join "product-materials" on material_id = materialid - join markets on market_id = markets.id - """ - - result = db.session.execute(sql) - rows = result.fetchall() - config = {} - for row in rows: - [material, product_type, market, value] = row - if material not in config: - config[material] = {} - if product_type not in config[material]: - config[material][product_type] = {} - config[material][product_type][market] = float(value) - - return config - - -def get_price_adjustment(market, product_type, material): - config = get_adjustment_config() - - return config.get(material, {}).get(product_type, {}).get(market, 1.0) diff --git a/api/resources/price-adjustments.yaml b/api/resources/price-adjustments.yaml new file mode 100644 index 0000000..fb9380c --- /dev/null +++ b/api/resources/price-adjustments.yaml @@ -0,0 +1,20 @@ +standard-wallpaper: + inquiry: + IT: 1.1034 + GB: 1.1154 + DE: 1.0625 + product: + GB: 1.1154 + DE: 1.0625 +premium-wallpaper: + inquiry: + IT: 1.1250 + SE: 1.0462 + GB: 1.1034 + DE: 1.1143 + NL: 1.0833 + product: + GB: 1.1034 + DE: 1.1143 + SE: 1.0462 + NL: 1.0833 diff --git a/api/resources/prices.py b/api/resources/prices.py index 3a5d564..2cb14cf 100644 --- a/api/resources/prices.py +++ b/api/resources/prices.py @@ -1,12 +1,13 @@ from api.models.external_print_product import get_cheapest_canvas_product, get_cheapest_poster_product -from flask import Blueprint, request, jsonify +from flask import Blueprint, request, jsonify, abort from api.helpers import check_api_key -from api.validators import validate_number, validate_exists +from api.validators import validate_number, validate_any_of, validate_exists from api.models import ( Inquiry, Market, Material, Product, + PrintProduct, ContractCustomer, ) from api.lib.prices import ( @@ -261,6 +262,54 @@ def inquiry(inquiry_id): return jsonify(data) +@mod.route("/list-prices//wallpaper") +def list_prices_wallpaper(market_id): + wallpapers = PrintProduct.query.wallpapers().all() + result = {} + material = Material.query.filter(Material.name == "standard-wallpaper").one() + market = Market.query.get_or_404(market_id) + + for wallpaper in wallpapers: + result[wallpaper.id] = wallpaper_m2_price( + material=material, market=market, designer=wallpaper.product.designer + ) + return jsonify(result) + + +@mod.route("/list-prices//canvas_external") +def list_prices_canvas_external(market_id): + canvases = PrintProduct.query.canvases().all() + market = Market.query.get_or_404(market_id) + external_product_id = get_cheapest_canvas_product() + + result = {} + for canvas in canvases: + result[canvas.id] = external_product_price( + id = external_product_id, + market=market, + designer=canvas.product.designer + ) + + return jsonify(result) + + +@mod.route("/list-prices//poster_external") +def list_prices_poster_external(market_id): + posters = PrintProduct.query.posters().all() + market = Market.query.get_or_404(market_id) + external_product_id = get_cheapest_poster_product() + + result = {} + for poster in posters: + result[poster.id] = external_product_price( + id = external_product_id, + market=market, + designer=poster.product.designer + ) + + return jsonify(result) + + @mod.route("/external-product", methods=["GET"]) @validate_exists("market") @validate_exists("id")