PW-622 Use column in materials table for getting prices

Squashed commit of the following:

commit 801333b25a635c5fce12d937d6808894ef2475d3
Author: Martin <me.carlsson@gmail.com>
Date:   Tue Jan 3 10:59:45 2017 +0100

    PW-622 divide by float

commit 9e2c696751c28c017bcb452ea4349509ca787fd9
Author: Martin <me.carlsson@gmail.com>
Date:   Mon Jan 2 13:46:57 2017 +0100

    PW-622 use new material_price column
This commit is contained in:
Rikard Bartholf
2017-01-17 11:42:26 +01:00
parent 5ad053a09b
commit 617c2daa06
4 changed files with 15 additions and 43 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ from api.lib.limits import calculate_canvas_limits
def m2_price(material, market, designer=None, reseller=None, inquiry=None, rounded=True, inc_vat=True): def m2_price(material, market, designer=None, reseller=None, inquiry=None, rounded=True, inc_vat=True):
""" calculates price per square meter """ """ calculates price per square meter """
price = material.price price = material.price / 100.0
if designer: if designer:
price *= (float(designer.pricepremium) / 100) + 1 price *= (float(designer.pricepremium) / 100) + 1
+2 -24
View File
@@ -43,18 +43,8 @@ class Material(db.Model):
id = db.Column('materialid', db.Integer, primary_key=True) id = db.Column('materialid', db.Integer, primary_key=True)
name = db.Column('material', db.String(255), nullable=False) name = db.Column('material', db.String(255), nullable=False)
material_price = db.relationship('MaterialPrice', uselist=False) price = db.Column('price', db.Integer)
@property
def price(self):
if self.material_price is not None:
return float(self.material_price.price)
return None
@price.setter
def price(self, value):
if self.material_price:
self.material_price.price = value
def to_json(self): def to_json(self):
return { return {
@@ -64,16 +54,4 @@ class Material(db.Model):
} }
def __repr__(self): def __repr__(self):
return self.name return self.name
class MaterialPrice(db.Model):
__tablename__ = 'v_materialprice'
groupid = db.Column(db.Integer)
materialid = db.Column(db.Integer, db.ForeignKey(
'product-materials.materialid'))
price = db.Column(db.Numeric)
__table_args__ = (db.PrimaryKeyConstraint(groupid, materialid),)
+4 -9
View File
@@ -6,7 +6,7 @@ from tests.factories import DesignerFactory, ContractCustomerFactory, InquiryFac
from api.models import market from api.models import market
from api.models import Inquiry from api.models import Inquiry
from api.models import Product from api.models import Product
from api.models.product import Material, MaterialPrice from api.models.product import Material
sweden = market.from_territory('SE') sweden = market.from_territory('SE')
@@ -37,14 +37,9 @@ piterra = ContractCustomerFactory(pricepremium=36.029411)
designer_moomin = DesignerFactory() designer_moomin = DesignerFactory()
designer_moomin.pricepremium = 2 designer_moomin.pricepremium = 2
standard_wallpaper = Material(name='standard-wallpaper') standard_wallpaper = Material(name='standard-wallpaper', price=23600)
standard_wallpaper.material_price = MaterialPrice(price=236) premium_wallpaper = Material(name='premium-wallpaper', price=26000)
standard_canvas = Material(name='standard-canvas', price=79920)
premium_wallpaper = Material(name='premium-wallpaper')
premium_wallpaper.material_price = MaterialPrice(price=260)
standard_canvas = Material(name='standard-canvas')
standard_canvas.material_price = MaterialPrice(price=799.2)
class TestPrice(unittest2.TestCase): class TestPrice(unittest2.TestCase):
+8 -9
View File
@@ -1,7 +1,7 @@
import json import json
import flask_testing import flask_testing
from api import create_app, db from api import create_app, db
from api.models.product import Material, MaterialPrice, Product from api.models.product import Material, Product
from api.models.inquiry import Inquiry from api.models.inquiry import Inquiry
from api.models.designer import Designer from api.models.designer import Designer
@@ -19,8 +19,7 @@ class TestEndpoints(flask_testing.TestCase):
db.drop_all() db.drop_all()
def _add_material(self, name, groupid, price): def _add_material(self, name, groupid, price):
material = Material(name=name) material = Material(name=name, price=price)
material.material_price = MaterialPrice(price=price, groupid=groupid)
db.session.add(material) db.session.add(material)
db.session.commit() db.session.commit()
return material return material
@@ -46,8 +45,8 @@ class TestEndpoints(flask_testing.TestCase):
return product return product
def test_wallpaper(self): def test_wallpaper(self):
self._add_material('standard-wallpaper', 1, 236) self._add_material('standard-wallpaper', 1, 23600)
self._add_material('premium-wallpaper', 1, 260) self._add_material('premium-wallpaper', 1, 26000)
response = self.client.get( response = self.client.get(
'/prices/wallpaper?width=200&height=10&territory=SE') '/prices/wallpaper?width=200&height=10&territory=SE')
self.assert200(response) self.assert200(response)
@@ -72,8 +71,8 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(expected, response.json['data']) self.assertEqual(expected, response.json['data'])
def test_wallpaper_with_product(self): def test_wallpaper_with_product(self):
self._add_material('standard-wallpaper', 1, 236) self._add_material('standard-wallpaper', 1, 23600)
self._add_material('premium-wallpaper', 1, 260) self._add_material('premium-wallpaper', 1, 26000)
designer = self._add_designer(4) designer = self._add_designer(4)
product = self._add_product(designer) product = self._add_product(designer)
response = self.client.get( response = self.client.get(
@@ -99,7 +98,7 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(expected, response.json['data']) self.assertEqual(expected, response.json['data'])
def test_canvas(self): def test_canvas(self):
self._add_material('standard-canvas', 2, 799.2) self._add_material('standard-canvas', 2, 79920)
response = self.client.get( response = self.client.get(
'/prices/canvas?width=200&height=10&territory=SE') '/prices/canvas?width=200&height=10&territory=SE')
self.assert200(response) self.assert200(response)
@@ -126,7 +125,7 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(expected, response.json['data']) self.assertEqual(expected, response.json['data'])
def test_canvas_with_product(self): def test_canvas_with_product(self):
self._add_material('standard-canvas', 2, 799.2) self._add_material('standard-canvas', 2, 79920)
designer = self._add_designer(4) designer = self._add_designer(4)
product = self._add_product(designer) product = self._add_product(designer)
response = self.client.get( response = self.client.get(