From 2de207e51a0d1d321adc858bc447cf424f9a5b3f Mon Sep 17 00:00:00 2001 From: "robin.keunen" Date: Sun, 29 Mar 2020 12:03:44 +0200 Subject: [PATCH] [REF] blacken stock coverage --- stock_coverage/__openerp__.py | 26 +++---- stock_coverage/models/product_template.py | 73 ++++++++++--------- stock_coverage/tests/test_stock_coverage.py | 81 ++++++++++++--------- 3 files changed, 91 insertions(+), 89 deletions(-) diff --git a/stock_coverage/__openerp__.py b/stock_coverage/__openerp__.py index a7640b8..4f07560 100644 --- a/stock_coverage/__openerp__.py +++ b/stock_coverage/__openerp__.py @@ -1,24 +1,16 @@ # -*- encoding: utf-8 -*- { - 'name': 'Product - Stock Coverage', - 'version': '9.0.1', - 'category': 'Product', - 'description': """ + "name": "Product - Stock Coverage", + "version": "9.0.1", + "category": "Product", + "description": """ Shows figures in the product form related to stock coverage There are settings in Inventory/settings to define the calculation range and the display range. """, - 'author': 'coop it easy', - 'website': 'coopiteasy.be', - 'license': 'AGPL-3', - 'depends': [ - 'product', - 'purchase', - 'point_of_sale', - 'stock' - ], - 'data': [ - 'views/product_template_view.xml', - 'data/cron.xml', - ], + "author": "coop it easy", + "website": "coopiteasy.be", + "license": "AGPL-3", + "depends": ["product", "purchase", "point_of_sale", "stock"], + "data": ["views/product_template_view.xml", "data/cron.xml"], } diff --git a/stock_coverage/models/product_template.py b/stock_coverage/models/product_template.py index a4aa50b..342f3a7 100644 --- a/stock_coverage/models/product_template.py +++ b/stock_coverage/models/product_template.py @@ -7,41 +7,38 @@ class ProductTemplate(models.Model): _inherit = "product.template" consumption_calculation_method = fields.Selection( - selection=[('sales_history', 'Sales History')], - string='Consumption Calculation Method', - default='sales_history', - ) - calculation_range = fields.Integer( - 'Calculation range (days)', - default=14, + selection=[("sales_history", "Sales History")], + string="Consumption Calculation Method", + default="sales_history", ) + calculation_range = fields.Integer("Calculation range (days)", default=14) average_consumption = fields.Float( - string='Average Consumption', - compute='_compute_average_daily_consumption', + string="Average Consumption", + compute="_compute_average_daily_consumption", readonly=True, digits=(100, 2), ) total_consumption = fields.Float( - string='Total Consumption', + string="Total Consumption", default=0, - compute='_compute_total_consumption', + compute="_compute_total_consumption", store=True, readonly=True, digits=(100, 2), ) estimated_stock_coverage = fields.Float( - string='Estimated Stock Coverage (days)', - compute='_compute_estimated_stock_coverage', + string="Estimated Stock Coverage (days)", + compute="_compute_estimated_stock_coverage", default=0, digits=(100, 2), readonly=True, ) @api.multi - @api.depends('total_consumption') + @api.depends("total_consumption") def _compute_average_daily_consumption(self): for template in self: if template.calculation_range > 0: @@ -53,35 +50,41 @@ class ProductTemplate(models.Model): return True @api.multi - @api.depends('calculation_range') + @api.depends("calculation_range") def _compute_total_consumption(self): for template in self: - products = ( - self.env['product.product'] - .search([('product_tmpl_id', '=', template.id)])) + products = self.env["product.product"].search( + [("product_tmpl_id", "=", template.id)] + ) today = dt.date.today() - pol_date_limit = ( - today - dt.timedelta(days=template.calculation_range)) - - order_lines = ( - self.env['pos.order.line'] - .search([ - ('product_id', 'in', products.ids), - ('create_date', '>', fields.Datetime.to_string(pol_date_limit)) # noqa - ]) + pol_date_limit = today - dt.timedelta( + days=template.calculation_range + ) + + order_lines = self.env["pos.order.line"].search( + [ + ("product_id", "in", products.ids), + ( + "create_date", + ">", + fields.Datetime.to_string(pol_date_limit), + ), # noqa + ] ) if order_lines: order_lines = order_lines.filtered( - lambda ol: ol.order_id.state in ['done', 'invoiced', 'paid']) # noqa - template.total_consumption = sum(order_lines.mapped('qty')) + lambda ol: ol.order_id.state + in ["done", "invoiced", "paid"] + ) # noqa + template.total_consumption = sum(order_lines.mapped("qty")) else: template.total_consumption = 0 return True @api.multi - @api.depends('total_consumption') + @api.depends("total_consumption") def _compute_estimated_stock_coverage(self): for product_template in self: qty = product_template.qty_available @@ -96,10 +99,7 @@ class ProductTemplate(models.Model): @api.model def _batch_compute_total_consumption(self): - products = ( - self.env['product.template'] - .search([('active', '=', True)]) - ) + products = self.env["product.template"].search([("active", "=", True)]) query = """ select @@ -121,5 +121,6 @@ class ProductTemplate(models.Model): results = {pid: qty for pid, qty in self.env.cr.fetchall()} for product in products: - product.total_consumption = results.get(product.id, - product.total_consumption) + product.total_consumption = results.get( + product.id, product.total_consumption + ) diff --git a/stock_coverage/tests/test_stock_coverage.py b/stock_coverage/tests/test_stock_coverage.py index 290b6af..49849b5 100644 --- a/stock_coverage/tests/test_stock_coverage.py +++ b/stock_coverage/tests/test_stock_coverage.py @@ -4,53 +4,62 @@ import datetime as dt from openerp.tests.common import TransactionCase _datetimes = map( - lambda d: d.strftime('%Y-%m-%d %H:%M:%S'), - (dt.datetime.now() - dt.timedelta(days=d) for d in range(0, 24, 2))) - -_quantities = [0.64, 6.45, 9.65, 1.76, 9.14, 3.99, - 6.92, 2.25, 6.91, 1.44, 6.52, 1.44] + lambda d: d.strftime("%Y-%m-%d %H:%M:%S"), + (dt.datetime.now() - dt.timedelta(days=d) for d in range(0, 24, 2)), +) + +_quantities = [ + 0.64, + 6.45, + 9.65, + 1.76, + 9.14, + 3.99, + 6.92, + 2.25, + 6.91, + 1.44, + 6.52, + 1.44, +] class TestProductTemplate(TransactionCase): - def setUp(self, *args, **kwargs): result = super(TestProductTemplate, self).setUp(*args, **kwargs) - test_product_template = ( - self.env['product.template'] - .create({'name': 'test product template', - 'calculation_range': 14, - 'consumption_calculation_method': 'sales_history', - 'product_template_id': 0, - }) + test_product_template = self.env["product.template"].create( + { + "name": "test product template", + "calculation_range": 14, + "consumption_calculation_method": "sales_history", + "product_template_id": 0, + } ) pid = ( - self.env['product.product'] - .search([('product_tmpl_id', '=', test_product_template.id)]) - .ids + self.env["product.product"] + .search([("product_tmpl_id", "=", test_product_template.id)]) + .ids ).pop() for date, qty in zip(_datetimes, _quantities): - (self.env['pos.order.line'] - .create({'create_date': date, - 'qty': qty, - 'product_id': pid, - }) - ) + ( + self.env["pos.order.line"].create( + {"create_date": date, "qty": qty, "product_id": pid} + ) + ) def _product_available(*args, **kwargs): - products = ( - self.env['product.product'] - .search([ - ('product_tmpl_id', '=', test_product_template.id)]) + products = self.env["product.product"].search( + [("product_tmpl_id", "=", test_product_template.id)] ) mock_data = { - 'qty_available': 53.2, - 'incoming_qty': 14, - 'outgoing_qty': 4.1, - 'virtual_available': 53.2 + 14 - 4.1, - } + "qty_available": 53.2, + "incoming_qty": 14, + "outgoing_qty": 4.1, + "virtual_available": 53.2 + 14 - 4.1, + } return {pid: mock_data for pid in products.ids} # mock area fixme @@ -66,13 +75,13 @@ class TestProductTemplate(TransactionCase): def test_create(self): """Create a simple product template""" - Template = self.env['product.template'] - product = Template.create({'name': 'Test create product'}) - self.assertEqual(product.name, 'Test create product') + Template = self.env["product.template"] + product = Template.create({"name": "Test create product"}) + self.assertEqual(product.name, "Test create product") def test_compute_average_daily_consumption(self): """Test computed field average_daily_consumption""" - ProductTemplate = self.env['product.template'] + ProductTemplate = self.env["product.template"] product_template = ProductTemplate.browse(self.product_template_id) computed_value = product_template.average_consumption @@ -81,7 +90,7 @@ class TestProductTemplate(TransactionCase): def test_compute_total_consumption(self): """Test total consumption was computed in setup""" - ProductTemplate = self.env['product.template'] + ProductTemplate = self.env["product.template"] product_template = ProductTemplate.browse(self.product_template_id) computed_value = product_template.total_consumption expected_value = 57.11