You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB

  1. # Copyright 2017 LasLabs Inc.
  2. # Copyright 2019 Rubén Bravo <rubenred18@gmail.com>
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. from odoo import api, fields, models, _
  5. from odoo.exceptions import ValidationError
  6. class ResLang(models.Model):
  7. _inherit = 'res.lang'
  8. default_uom_ids = fields.Many2many(
  9. string='Default Units',
  10. comodel_name='uom.uom',
  11. )
  12. @api.multi
  13. @api.constrains('default_uom_ids')
  14. def _check_default_uom_ids(self):
  15. for record in self:
  16. categories = set(record.default_uom_ids.mapped('category_id'))
  17. if len(categories) != len(record.default_uom_ids):
  18. raise ValidationError(_(
  19. 'Only one default unit of measure per category may '
  20. 'be selected.',
  21. ))
  22. @api.model
  23. def default_uom_by_category(self, category_name, lang=None):
  24. """Return the default UoM for language for the input UoM Category.
  25. Args:
  26. category_name (str): Name of the UoM category to get the default
  27. for.
  28. lang (ResLang or str, optional): Recordset or code of the language
  29. to get the default for. Will use the current user language if
  30. omitted.
  31. Returns:
  32. ProductUom: Unit of measure representing the default, if set.
  33. Empty recordset otherwise.
  34. """
  35. if lang is None:
  36. lang = self.env.user.lang
  37. if isinstance(lang, str):
  38. lang = self.env['res.lang'].search([
  39. ('code', '=', lang),
  40. ],
  41. limit=1,
  42. )
  43. results = lang.default_uom_ids.filtered(
  44. lambda r: r.category_id.name == category_name,
  45. )
  46. return results[:1]