From dee7344ba5c276b62513cf168d3f8c61fbec07a2 Mon Sep 17 00:00:00 2001 From: Martin Carlsson Date: Tue, 19 Jan 2021 14:32:06 +0100 Subject: [PATCH] P5-6370 inquires should use markets instead of territores for calculating prices #36 --- api/models/__init__.py | 2 +- api/models/inquiry.py | 11 +++-------- api/models/market.py | 18 +++++++++++------- tests/lib/test_prices.py | 5 ----- tests/models/test_market.py | 10 ---------- tests/resources/test_prices.py | 18 ++++++++++-------- 6 files changed, 25 insertions(+), 39 deletions(-) delete mode 100644 tests/models/test_market.py diff --git a/api/models/__init__.py b/api/models/__init__.py index e3fc12d..b1f1111 100644 --- a/api/models/__init__.py +++ b/api/models/__init__.py @@ -4,7 +4,7 @@ from .designer import Designer from .category import Category from .contract_customer import ContractCustomer from .inquiry import Inquiry -from .product import Material, Product +from .product import Material, Product, Currencies from .order import ( Order, OrderField, diff --git a/api/models/inquiry.py b/api/models/inquiry.py index 08c7163..345b329 100644 --- a/api/models/inquiry.py +++ b/api/models/inquiry.py @@ -9,7 +9,9 @@ class Inquiry(db.Model): __tablename__ = "inquiries" id = db.Column("inquiryid", db.Integer, primary_key=True) - territory = db.Column(db.String(2)) + market_id = db.Column(db.Integer, db.ForeignKey("markets.id")) + market = db.relationship("Market", backref="inquiries") + product_group = db.Column(db.String(100)) price_m2 = db.Column(db.Numeric) price_effect = db.Column(db.Numeric) @@ -36,13 +38,6 @@ class Inquiry(db.Model): product = db.relationship("Product", uselist=False) automated = db.Column(db.Integer, nullable=False, default=0) - @property - def market(self): - market = market_model.from_territory(self.territory) - if not market: - raise ValueError("Invalid territory: {}".format(self.territory)) - return market - def local_price_retouch(self, rounded=True, inc_vat=False): """ returns the retouch price in the inquiry local currency """ if not self.price_retouch: diff --git a/api/models/market.py b/api/models/market.py index 9e41155..0871676 100644 --- a/api/models/market.py +++ b/api/models/market.py @@ -12,25 +12,29 @@ class Market(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255)) vat = db.Column(db.Float, nullable=False) - price_adjustment = db.Column(db.Float, nullable=False) + price_adjustments = db.Column("price_adjustment", db.Float, nullable=False) currency = db.Column(db.String(3), db.ForeignKey("product-currencies.iso3char")) _pc = db.relationship(Currencies, uselist=False) - exchange_rate = association_proxy("_pc", "exchange_rate", creator=lambda v: Currencies(exchange_rate=v)) - - @validates("name") - def validate_name(self, key, value): - raise ValueError("Name can not be modified") + exchange_rate = association_proxy("_pc", "exchange_rate") def to_json(self): return { "id": self.id, "name": self.name, - "price_adjustment": str(self.price_adjustment), + "price_adjustment": str(self.price_adjustments), "vat": str(self.vat), "currency": self.currency, "exchange_rate": str(self.exchange_rate), } + # hack: market need a territory to make it compatible with current price calculations code + # this will be removed in P5-6491 + @property + def territory(self): + if self.name == 'GLOBAL': + return 'EU' + return self.name + class PriceAdjustments(db.Model): diff --git a/tests/lib/test_prices.py b/tests/lib/test_prices.py index 44825cc..80b6ed2 100644 --- a/tests/lib/test_prices.py +++ b/tests/lib/test_prices.py @@ -340,7 +340,6 @@ class TestPrice(unittest2.TestCase): with mock.patch.object(Inquiry, "market", nl): inquiry = Inquiry( product_group="photo-wallpaper", - territory="NL", pricepremium=0, width=350, height=270, @@ -373,7 +372,6 @@ class TestPrice(unittest2.TestCase): with mock.patch.object(Inquiry, "market", sweden): inquiry = Inquiry( product_group="photo-wallpaper", - territory="SE", pricepremium=0, width=350, height=280, @@ -393,7 +391,6 @@ class TestPrice(unittest2.TestCase): with mock.patch.object(Inquiry, "market", sweden): inquiry = Inquiry( product_group="photo-wallpaper", - territory="SE", pricepremium=25, width=250, height=400, @@ -412,7 +409,6 @@ class TestPrice(unittest2.TestCase): with mock.patch.object(Inquiry, "market", sweden): inquiry = Inquiry( product_group="canvas", - territory="SE", width=100, height=100, framed=True, @@ -472,7 +468,6 @@ class TestPrice(unittest2.TestCase): with mock.patch.object(Inquiry, "market", sweden): inquiry = Inquiry( product_group="poster", - territory="SE", width=30, height=40, hanger=True, diff --git a/tests/models/test_market.py b/tests/models/test_market.py deleted file mode 100644 index 7e20961..0000000 --- a/tests/models/test_market.py +++ /dev/null @@ -1,10 +0,0 @@ -# coding=UTF-8 - -import unittest2 -from api.models import Market - - -class TestMarket(unittest2.TestCase): - def test_exchange_rate(self): - market = Market(exchange_rate=0.123) - self.assertEqual(0.123, market.exchange_rate) diff --git a/tests/resources/test_prices.py b/tests/resources/test_prices.py index 9473c93..c5ff15f 100644 --- a/tests/resources/test_prices.py +++ b/tests/resources/test_prices.py @@ -1,9 +1,8 @@ import json import flask_testing from api import create_app, db -from api.models.product import Material, Product -from api.models.inquiry import Inquiry -from api.models.designer import Designer +from api.models.product import Material, Product, Currencies +from api.models import Inquiry, Designer, Market from api.lib import pwinty @@ -24,9 +23,12 @@ class TestEndpoints(flask_testing.TestCase): db.session.commit() return material - def _add_inquiry(self, territory, product_group, price_retouch=100): + def _add_inquiry(self, product_group, price_retouch=100): + currency = Currencies(iso3char="SEK", exchange_rate=1) + db.session.add(currency) + market = Market(name="SE", vat=1.25, currency='SEK', price_adjustments=1) inquiry = Inquiry( - territory=territory, + market=market, product_group=product_group, price_retouch=price_retouch, ) @@ -192,20 +194,20 @@ class TestEndpoints(flask_testing.TestCase): def test_inquiry_wallpaper(self): self._add_material("standard-wallpaper", 1, 236) self._add_material("premium-wallpaper", 1, 260) - self._add_inquiry(territory="SE", product_group="photo-wallpaper") + self._add_inquiry(product_group="photo-wallpaper") response = self.client.get("/prices/inquiry/1?width=100&height=100") self.assert200(response) self.assertEqual("wallpaper", response.json["group"]) def test_inquiry_canvas(self): self._add_material("standard-canvas", 2, 799.2) - self._add_inquiry(territory="SE", product_group="canvas") + self._add_inquiry(product_group="canvas") response = self.client.get("/prices/inquiry/1?width=100&height=100") self.assert200(response) self.assertEqual("canvas", response.json["group"]) def test_inquiry_poster(self): - self._add_inquiry(territory="SE", product_group="poster") + self._add_inquiry(product_group="poster") response = self.client.get("/prices/inquiry/1?width=100&height=100") self.assert200(response) self.assertEqual("poster", response.json["group"])