From 62660be8d6724ae1bee1748407d8d5ac7fe854ae Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Wed, 28 Aug 2019 16:23:42 +0200 Subject: [PATCH] Add option to generate custom name format for cells Following the proposal of @damendieta, implement differently --- stock_vertical_lift/models/stock_location.py | 28 ++++++++++++++++++- .../views/stock_location_views.xml | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/stock_vertical_lift/models/stock_location.py b/stock_vertical_lift/models/stock_location.py index ea30e9d7e22a..e764683304d2 100644 --- a/stock_vertical_lift/models/stock_location.py +++ b/stock_vertical_lift/models/stock_location.py @@ -1,6 +1,7 @@ # Copyright 2019 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from collections import defaultdict from odoo import _, api, exceptions, fields, models from odoo.addons.base_sparse_field.models.fields import Serialized @@ -31,6 +32,19 @@ class StockLocation(models.Model): help="Used to know if a cell of a Tray location is empty.", ) tray_matrix = Serialized(string='Cells', compute='_compute_tray_matrix') + cell_name_format = fields.Char( + string='Name Format for Cells', + default=lambda self: self._default_cell_name_format(), + help="Cells sub-locations generated in a tray will be named" + " after this format. Replacement fields between curly braces are used" + " to inject positions. {x} will be replaced by the x position and" + " {y} by the y position. Complex formatting (such as padding, ...)" + " can be done using the format specification at " + " https://docs.python.org/2/library/string.html#formatstrings", + ) + + def _default_cell_name_format(self): + return 'x{x:0>2}y{y:0>2}' # TODO document hierarchy # Vertical Lift View @@ -120,6 +134,13 @@ def write(self, vals): trays_to_update = ( location.vertical_lift_tray_type_id.id != new_tray_type_id ) + # short-circuit this check if we already know that we have to + # update trays + if not trays_to_update and 'cell_name_format' in vals: + new_format = vals.get('cell_name_format') + trays_to_update = ( + location.cell_name_format != new_format + ) super(StockLocation, location).write(vals) if trays_to_update: self._update_tray_sublocations() @@ -168,6 +189,11 @@ def _tray_cell_matrix(self): cells[cell.posy - 1][cell.posx - 1] = 1 return cells + def _format_tray_sublocation_name(self, x, y): + template = self.cell_name_format or self._default_cell_name_format() + # using format_map allow to have missing replacement strings + return template.format_map(defaultdict(str, x=x, y=y)) + @api.multi def _update_tray_sublocations(self): values = [] @@ -195,7 +221,7 @@ def _update_tray_sublocations(self): for row in range(1, tray_type.rows + 1): for col in range(1, tray_type.cols + 1): subloc_values = { - 'name': _('x{}y{}').format(col, row), + 'name': location._format_tray_sublocation_name(col, row), 'posx': col, 'posy': row, 'location_id': location.id, diff --git a/stock_vertical_lift/views/stock_location_views.xml b/stock_vertical_lift/views/stock_location_views.xml index 5ca0f6e4e505..18016973e3f9 100644 --- a/stock_vertical_lift/views/stock_location_views.xml +++ b/stock_vertical_lift/views/stock_location_views.xml @@ -15,6 +15,7 @@ +