P5-4777 add listprice resources

This commit is contained in:
Martin Carlsson
2020-01-30 13:59:21 +01:00
committed by GitHub
parent 1d214566ec
commit 2f4275e66a
4 changed files with 147 additions and 3 deletions
+1 -1
View File
@@ -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)
+65
View File
@@ -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")
+80 -1
View File
@@ -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/<string:territory>/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/<string:territory>/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/<string:territory>/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/<string:territory>/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)
+1 -1
View File
@@ -147,7 +147,7 @@ class TestPrice(unittest2.TestCase):
# designer
self.assertEqual(
348,
347,
wallpaper_price(
width=100,
height=100,