From 0b141fddda67a0e13d54106eb3a3f901312c7aa6 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Wed, 11 Sep 2019 13:14:56 +0200 Subject: [PATCH 1/5] 12.0 Add stock_location_bin_name (split from stock_location_zone) --- stock_location_bin_name/__init__.py | 1 + stock_location_bin_name/__manifest__.py | 22 ++++++++ stock_location_bin_name/models/__init__.py | 1 + .../models/stock_location.py | 52 +++++++++++++++++++ .../readme/CONTRIBUTORS.rst | 4 ++ .../readme/DESCRIPTION.rst | 2 + .../views/stock_location.xml | 13 +++++ 7 files changed, 95 insertions(+) create mode 100644 stock_location_bin_name/__init__.py create mode 100644 stock_location_bin_name/__manifest__.py create mode 100644 stock_location_bin_name/models/__init__.py create mode 100644 stock_location_bin_name/models/stock_location.py create mode 100644 stock_location_bin_name/readme/CONTRIBUTORS.rst create mode 100644 stock_location_bin_name/readme/DESCRIPTION.rst create mode 100644 stock_location_bin_name/views/stock_location.xml diff --git a/stock_location_bin_name/__init__.py b/stock_location_bin_name/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/stock_location_bin_name/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_location_bin_name/__manifest__.py b/stock_location_bin_name/__manifest__.py new file mode 100644 index 000000000..8b7a3f3b7 --- /dev/null +++ b/stock_location_bin_name/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2017 Syvain Van Hoof (Okia sprl) +# Copyright 2016-2019 Jacques-Etienne Baudoux (BCIM) +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +{ + 'name': 'Stock Location Bin Name', + 'version': '12.0.1.0.0', + 'author': "BCIM, Okia, Camptocamp, Odoo Community Association (OCA)", + 'website': "https://github.com/OCA/stock-logistics-warehouse", + 'summary': "Compute bin stock location name automatically", + 'category': 'Stock Management', + 'depends': [ + 'stock_location_zone', + 'stock_location_attribute', + ], + 'data': [ + 'views/stock_location.xml', + ], + 'installable': True, + 'development_status': 'Alpha', + 'license': 'AGPL-3', +} diff --git a/stock_location_bin_name/models/__init__.py b/stock_location_bin_name/models/__init__.py new file mode 100644 index 000000000..88493e35d --- /dev/null +++ b/stock_location_bin_name/models/__init__.py @@ -0,0 +1 @@ +from . import stock_location diff --git a/stock_location_bin_name/models/stock_location.py b/stock_location_bin_name/models/stock_location.py new file mode 100644 index 000000000..d746b765c --- /dev/null +++ b/stock_location_bin_name/models/stock_location.py @@ -0,0 +1,52 @@ +# Copyright 2017 Syvain Van Hoof (Okia sprl) +# Copyright 2016-2019 Jacques-Etienne Baudoux (BCIM) +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from odoo import api, fields, models + + +class StockLocation(models.Model): + + _inherit = 'stock.location' + + location_name_format = fields.Char( + 'Location Name Format', + help="Format string that will compute the name of the location. " + "Use location fields. Example: " + "'{area}-{corridor:0>2}.{rack:0>3}" + ".{level:0>2}'") + + area = fields.Char( + 'Area', + compute='_compute_area', + store=True, + ) + + @api.depends('name', 'location_kind', 'location_id.area') + def _compute_area(self): + for location in self: + if location.location_kind == 'area': + location.area = location.name + else: + location.area = location.location_id.area + + @api.multi + @api.onchange('corridor', 'row', 'rack', 'level', + 'posx', 'posy', 'posz') + def _onchange_attribute_compute_name(self): + for location in self: + if not location.location_kind == 'bin': + continue + area = location + while area and not area.location_name_format: + area = area.location_id + if not area: + continue + template = area.location_name_format + # We don't want to use the full browse record as it would + # give too much access to internals for the users. + # We cannot use location.read() as we may have a NewId. + # We should have the record's values in the cache at this + # point. We must be cautious not to leak an environment through + # relational fields. + location.name = template.format(**location._cache) diff --git a/stock_location_bin_name/readme/CONTRIBUTORS.rst b/stock_location_bin_name/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..0425dda61 --- /dev/null +++ b/stock_location_bin_name/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* Syvain Van Hoof (Okia sprl) +* Jacques-Etienne Baudoux (BCIM) +* Guewen Baconnier (Camptocamp) +* Akim Juillerat diff --git a/stock_location_bin_name/readme/DESCRIPTION.rst b/stock_location_bin_name/readme/DESCRIPTION.rst new file mode 100644 index 000000000..d868c822f --- /dev/null +++ b/stock_location_bin_name/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module allows to compute automatically Bin location names based on +locations attributes. diff --git a/stock_location_bin_name/views/stock_location.xml b/stock_location_bin_name/views/stock_location.xml new file mode 100644 index 000000000..7bec3b6bd --- /dev/null +++ b/stock_location_bin_name/views/stock_location.xml @@ -0,0 +1,13 @@ + + + + stock.location.name.format + stock.location + + + + + + + + From 7f8102ad03ec4d27d2995f33bd63a13b41aff646 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Fri, 13 Sep 2019 11:35:31 +0200 Subject: [PATCH 2/5] Change the area field as a related field It is not stored because we only need it for the generation of the name. It slows creation of locations for no value. If we need to search on it, we can use a search method. --- .../models/stock_location.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/stock_location_bin_name/models/stock_location.py b/stock_location_bin_name/models/stock_location.py index d746b765c..3fa503d79 100644 --- a/stock_location_bin_name/models/stock_location.py +++ b/stock_location_bin_name/models/stock_location.py @@ -17,19 +17,14 @@ class StockLocation(models.Model): ".{level:0>2}'") area = fields.Char( - 'Area', - compute='_compute_area', - store=True, + string='Area', + # Field used for _onchange_attribute_compute_name, so we + # have the name in the record's cache. Does not need to be + # stored as we already have 'area_location_id' + related='area_location_id.name', + readonly=True, ) - @api.depends('name', 'location_kind', 'location_id.area') - def _compute_area(self): - for location in self: - if location.location_kind == 'area': - location.area = location.name - else: - location.area = location.location_id.area - @api.multi @api.onchange('corridor', 'row', 'rack', 'level', 'posx', 'posy', 'posz') From 8ed44d013f9f2df1a8903d75210b42864a9f6042 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Thu, 19 Dec 2019 11:48:41 +0100 Subject: [PATCH 3/5] [IMP] stock_location_bin_name: black, isort --- stock_location_bin_name/__manifest__.py | 27 ++++++++----------- .../models/stock_location.py | 20 +++++++------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/stock_location_bin_name/__manifest__.py b/stock_location_bin_name/__manifest__.py index 8b7a3f3b7..23898c0c3 100644 --- a/stock_location_bin_name/__manifest__.py +++ b/stock_location_bin_name/__manifest__.py @@ -3,20 +3,15 @@ # Copyright 2019 Camptocamp SA # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) { - 'name': 'Stock Location Bin Name', - 'version': '12.0.1.0.0', - 'author': "BCIM, Okia, Camptocamp, Odoo Community Association (OCA)", - 'website': "https://github.com/OCA/stock-logistics-warehouse", - 'summary': "Compute bin stock location name automatically", - 'category': 'Stock Management', - 'depends': [ - 'stock_location_zone', - 'stock_location_attribute', - ], - 'data': [ - 'views/stock_location.xml', - ], - 'installable': True, - 'development_status': 'Alpha', - 'license': 'AGPL-3', + "name": "Stock Location Bin Name", + "version": "12.0.1.0.0", + "author": "BCIM, Okia, Camptocamp, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/stock-logistics-warehouse", + "summary": "Compute bin stock location name automatically", + "category": "Stock Management", + "depends": ["stock_location_zone", "stock_location_attribute"], + "data": ["views/stock_location.xml"], + "installable": True, + "development_status": "Alpha", + "license": "AGPL-3", } diff --git a/stock_location_bin_name/models/stock_location.py b/stock_location_bin_name/models/stock_location.py index 3fa503d79..80593687e 100644 --- a/stock_location_bin_name/models/stock_location.py +++ b/stock_location_bin_name/models/stock_location.py @@ -7,30 +7,30 @@ from odoo import api, fields, models class StockLocation(models.Model): - _inherit = 'stock.location' + _inherit = "stock.location" location_name_format = fields.Char( - 'Location Name Format', + "Location Name Format", help="Format string that will compute the name of the location. " - "Use location fields. Example: " - "'{area}-{corridor:0>2}.{rack:0>3}" - ".{level:0>2}'") + "Use location fields. Example: " + "'{area}-{corridor:0>2}.{rack:0>3}" + ".{level:0>2}'", + ) area = fields.Char( - string='Area', + string="Area", # Field used for _onchange_attribute_compute_name, so we # have the name in the record's cache. Does not need to be # stored as we already have 'area_location_id' - related='area_location_id.name', + related="area_location_id.name", readonly=True, ) @api.multi - @api.onchange('corridor', 'row', 'rack', 'level', - 'posx', 'posy', 'posz') + @api.onchange("corridor", "row", "rack", "level", "posx", "posy", "posz") def _onchange_attribute_compute_name(self): for location in self: - if not location.location_kind == 'bin': + if not location.location_kind == "bin": continue area = location while area and not area.location_name_format: From 284633cfd278fcae5832552d9e50c347a1ef98d4 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Thu, 19 Dec 2019 13:13:20 +0100 Subject: [PATCH 4/5] [MIG] stock_location_bin_name: Migration to 13.0 --- .../odoo/addons/stock_location_bin_name | 1 + setup/stock_location_bin_name/setup.py | 6 +++ stock_location_bin_name/__manifest__.py | 4 +- .../models/stock_location.py | 46 +++++++++++++------ 4 files changed, 42 insertions(+), 15 deletions(-) create mode 120000 setup/stock_location_bin_name/odoo/addons/stock_location_bin_name create mode 100644 setup/stock_location_bin_name/setup.py diff --git a/setup/stock_location_bin_name/odoo/addons/stock_location_bin_name b/setup/stock_location_bin_name/odoo/addons/stock_location_bin_name new file mode 120000 index 000000000..cb6599b26 --- /dev/null +++ b/setup/stock_location_bin_name/odoo/addons/stock_location_bin_name @@ -0,0 +1 @@ +../../../../stock_location_bin_name \ No newline at end of file diff --git a/setup/stock_location_bin_name/setup.py b/setup/stock_location_bin_name/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/stock_location_bin_name/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_location_bin_name/__manifest__.py b/stock_location_bin_name/__manifest__.py index 23898c0c3..9d81d6f3c 100644 --- a/stock_location_bin_name/__manifest__.py +++ b/stock_location_bin_name/__manifest__.py @@ -4,12 +4,12 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) { "name": "Stock Location Bin Name", - "version": "12.0.1.0.0", + "version": "13.0.1.0.0", "author": "BCIM, Okia, Camptocamp, Odoo Community Association (OCA)", "website": "https://github.com/OCA/stock-logistics-warehouse", "summary": "Compute bin stock location name automatically", "category": "Stock Management", - "depends": ["stock_location_zone", "stock_location_attribute"], + "depends": ["stock_location_zone", "stock_location_position"], "data": ["views/stock_location.xml"], "installable": True, "development_status": "Alpha", diff --git a/stock_location_bin_name/models/stock_location.py b/stock_location_bin_name/models/stock_location.py index 80593687e..5a4c38b10 100644 --- a/stock_location_bin_name/models/stock_location.py +++ b/stock_location_bin_name/models/stock_location.py @@ -2,11 +2,38 @@ # Copyright 2016-2019 Jacques-Etienne Baudoux (BCIM) # Copyright 2019 Camptocamp SA # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +import string + from odoo import api, fields, models -class StockLocation(models.Model): +class PartialFormatter(string.Formatter): + def __init__(self, missing="~", bad_fmt="!"): + self.missing = missing + self.bad_fmt = bad_fmt + def get_field(self, field_name, args, kwargs): + # Handle a key not found + try: + val = super().get_field(field_name, args, kwargs) + except (KeyError, AttributeError): + val = None, field_name + return val + + def format_field(self, value, spec): + # handle an invalid format + if value is None: + return self.missing + try: + return super().format_field(value, spec) + except ValueError: + if self.bad_fmt is not None: + return self.bad_fmt + else: + raise + + +class StockLocation(models.Model): _inherit = "stock.location" location_name_format = fields.Char( @@ -14,19 +41,10 @@ class StockLocation(models.Model): help="Format string that will compute the name of the location. " "Use location fields. Example: " "'{area}-{corridor:0>2}.{rack:0>3}" - ".{level:0>2}'", + ".{level:0>2}'\n" + "Missing fields are replaced by '~' and formatting errors by '!'.", ) - area = fields.Char( - string="Area", - # Field used for _onchange_attribute_compute_name, so we - # have the name in the record's cache. Does not need to be - # stored as we already have 'area_location_id' - related="area_location_id.name", - readonly=True, - ) - - @api.multi @api.onchange("corridor", "row", "rack", "level", "posx", "posy", "posz") def _onchange_attribute_compute_name(self): for location in self: @@ -44,4 +62,6 @@ class StockLocation(models.Model): # We should have the record's values in the cache at this # point. We must be cautious not to leak an environment through # relational fields. - location.name = template.format(**location._cache) + values = dict(location._cache) + values["area"] = area.name + location.name = PartialFormatter().format(template, **values) From 393941547000fd8c7321530940bf3e89da640d80 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Tue, 17 Mar 2020 11:09:39 +0100 Subject: [PATCH 5/5] run pre-commit with new prettiers --- stock_location_bin_name/views/stock_location.xml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/stock_location_bin_name/views/stock_location.xml b/stock_location_bin_name/views/stock_location.xml index 7bec3b6bd..9cbcdc769 100644 --- a/stock_location_bin_name/views/stock_location.xml +++ b/stock_location_bin_name/views/stock_location.xml @@ -1,12 +1,15 @@ - + stock.location.name.format stock.location - + - +