From 2f4275e66a162cbce73d3028d9f3e03fe189fd64 Mon Sep 17 00:00:00 2001 From: Martin Carlsson Date: Thu, 30 Jan 2020 13:59:21 +0100 Subject: [PATCH] P5-4777 add listprice resources --- api/lib/prices.py | 2 +- api/models/product.py | 65 ++++++++++++++++++++++++++++++++ api/resources/prices.py | 81 +++++++++++++++++++++++++++++++++++++++- tests/lib/test_prices.py | 2 +- 4 files changed, 147 insertions(+), 3 deletions(-) diff --git a/api/lib/prices.py b/api/lib/prices.py index 731869f..7d351d3 100644 --- a/api/lib/prices.py +++ b/api/lib/prices.py @@ -51,7 +51,7 @@ def wallpaper_price( inc_vat=True, ): price = m2_price( - material, market, designer, reseller, inquiry, rounded=rounded, inc_vat=False + material, market, designer, reseller, inquiry, rounded=False, inc_vat=False ) sqm = (width / 100) * (height / 100) diff --git a/api/models/product.py b/api/models/product.py index c7cc2a7..dfa5e26 100644 --- a/api/models/product.py +++ b/api/models/product.py @@ -1,7 +1,10 @@ # coding=UTF-8 +from datetime import datetime from api.extensions import db from flask_sqlalchemy import BaseQuery +from sqlalchemy import or_ +from sqlalchemy.orm import joinedload class Product(db.Model): @@ -13,6 +16,36 @@ class Product(db.Model): designer = db.relationship("Designer", backref="products") +class ProductField(db.Model): + + __tablename__ = "product-fields" + + id = db.Column("fieldid", db.Integer, primary_key=True) + field = db.Column(db.String()) + inserted = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + updated = db.Column(db.DateTime) + + +class ProductProductFields(db.Model): + + __tablename__ = "product-products_fields" + + productid = db.Column( + db.Integer, db.ForeignKey("product-products.productid"), nullable=False + ) + fieldid = db.Column( + db.Integer, db.ForeignKey("product-fields.fieldid"), nullable=False + ) + value = db.Column(db.String()) + inserted = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + updated = db.Column(db.DateTime) + + product = db.relationship("Product", backref="fields") + field = db.relationship("ProductField") + + __table_args__ = (db.PrimaryKeyConstraint(productid, fieldid),) + + class Currencies(db.Model): __tablename__ = "product-currencies" @@ -51,3 +84,35 @@ class Material(db.Model): def __repr__(self): return self.name + + +class PrintProductQuery(BaseQuery): + def wallpapers(self): + return self.filter(or_(PrintProduct.groupid == 1, PrintProduct.groupid == 3)) + + def canvases(self): + return self.filter(PrintProduct.groupid == 2) + + def posters(self): + return self.filter(PrintProduct.groupid == 7) + + def framed_prints(self): + return self.filter(PrintProduct.groupid == 8) + + def with_relations(self): + return self.options(joinedload("*")) + + +class PrintProduct(db.Model): + + __tablename__ = "product-printproducts" + + query_class = PrintProductQuery + + id = db.Column("printid", db.Integer, primary_key=True) + productid = db.Column( + db.Integer, db.ForeignKey("product-products.productid"), nullable=False + ) + groupid = db.Column(db.Integer, nullable=False) + + product = db.relationship("Product", backref="printproducts") diff --git a/api/resources/prices.py b/api/resources/prices.py index da2dd5c..54552f0 100644 --- a/api/resources/prices.py +++ b/api/resources/prices.py @@ -2,7 +2,7 @@ from flask import Blueprint, request, jsonify, abort from api.helpers import check_api_key from api.validators import validate_territory, validate_number, validate_any_of from api.models.inquiry import Inquiry -from api.models.product import Material, Product +from api.models.product import Material, Product, PrintProduct from api.models.contract_customer import ContractCustomer from api.models import market as market_model from api.lib import pwinty @@ -508,3 +508,82 @@ def inquiry(inquiry_id): raise ValueError("Invalid group {}".format(inquiry.product_group)) return jsonify(data) + + +@mod.route("/list-prices//wallpaper") +def list_prices_wallpaper(territory): + wallpapers = PrintProduct.query.wallpapers().with_relations().all() + result = {} + material = Material.query.filter(Material.name == "standard-wallpaper").one() + market = market_model.from_territory(territory) + + for wallpaper in wallpapers: + result[wallpaper.id] = wallpaper_price( + width=100, + height=100, + material=material, + market=market, + designer=wallpaper.product.designer, + ) + return jsonify(result) + + +@mod.route("/list-prices//canvas") +def list_prices_canvas(territory): + canvases = PrintProduct.query.canvases().with_relations().all() + market = market_model.from_territory(territory) + + result = {} + for canvas in canvases: + result[canvas.id] = canvas_price( + width=40, + height=40, + market=market, + framed=True, + designer=canvas.product.designer, + ) + return jsonify(result) + + +@mod.route("/list-prices//poster") +def list_prices_posters(territory): + posters = PrintProduct.query.posters().with_relations().all() + market = market_model.from_territory(territory) + + result = {} + for poster in posters: + result[poster.id] = poster_price( + width=30, + height=30, + market=market, + hanger=True, + designer=poster.product.designer, + ) + return jsonify(result) + + +@mod.route("/list-prices//framed-print") +def list_prices_framed_prints(territory): + framed_prints = PrintProduct.query.framed_prints().with_relations().all() + 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 + + 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 + ) + return jsonify(result) diff --git a/tests/lib/test_prices.py b/tests/lib/test_prices.py index f321e05..e70d25e 100644 --- a/tests/lib/test_prices.py +++ b/tests/lib/test_prices.py @@ -147,7 +147,7 @@ class TestPrice(unittest2.TestCase): # designer self.assertEqual( - 348, + 347, wallpaper_price( width=100, height=100,