[MIG] stock_location_bin_name: Migration to 13.0

This commit is contained in:
Guewen Baconnier
2019-12-19 13:13:20 +01:00
committed by Hai Lang
parent 1d2f33d966
commit 28e8b83563
2 changed files with 35 additions and 15 deletions

View File

@@ -4,12 +4,12 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
{ {
"name": "Stock Location Bin Name", "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)", "author": "BCIM, Okia, Camptocamp, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-warehouse", "website": "https://github.com/OCA/stock-logistics-warehouse",
"summary": "Compute bin stock location name automatically", "summary": "Compute bin stock location name automatically",
"category": "Stock Management", "category": "Stock Management",
"depends": ["stock_location_zone", "stock_location_attribute"], "depends": ["stock_location_zone", "stock_location_position"],
"data": ["views/stock_location.xml"], "data": ["views/stock_location.xml"],
"installable": True, "installable": True,
"development_status": "Alpha", "development_status": "Alpha",

View File

@@ -2,11 +2,38 @@
# Copyright 2016-2019 Jacques-Etienne Baudoux (BCIM) <je@bcim.be> # Copyright 2016-2019 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# Copyright 2019 Camptocamp SA # Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
import string
from odoo import api, fields, models 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" _inherit = "stock.location"
location_name_format = fields.Char( location_name_format = fields.Char(
@@ -14,19 +41,10 @@ class StockLocation(models.Model):
help="Format string that will compute the name of the location. " help="Format string that will compute the name of the location. "
"Use location fields. Example: " "Use location fields. Example: "
"'{area}-{corridor:0>2}.{rack:0>3}" "'{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") @api.onchange("corridor", "row", "rack", "level", "posx", "posy", "posz")
def _onchange_attribute_compute_name(self): def _onchange_attribute_compute_name(self):
for location in 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 # We should have the record's values in the cache at this
# point. We must be cautious not to leak an environment through # point. We must be cautious not to leak an environment through
# relational fields. # relational fields.
location.name = template.format(**location._cache) values = dict(location._cache)
values["area"] = area.name
location.name = PartialFormatter().format(template, **values)