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.

61 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in root directory
  4. ##############################################################################
  5. from openerp import models, fields, api
  6. from openerp.tools.translate import _
  7. import collections
  8. def dict_recursive_update(d, u):
  9. for k, v in u.iteritems():
  10. if isinstance(v, collections.Mapping):
  11. r = dict_recursive_update(d.get(k, {}), v)
  12. d[k] = r
  13. else:
  14. d[k] = u[k]
  15. return d
  16. class ResPartner(models.Model):
  17. _inherit = 'res.partner'
  18. region = fields.Many2one(comodel_name='res.partner.nuts',
  19. string="Region")
  20. substate = fields.Many2one(comodel_name='res.partner.nuts',
  21. string="Substate")
  22. lbl_region = fields.Char(compute='_labels_get')
  23. lbl_substate = fields.Char(compute='_labels_get')
  24. @api.one
  25. @api.depends('country_id')
  26. def _labels_get(self):
  27. self.lbl_region = _('Region')
  28. self.lbl_substate = _('Substate')
  29. @api.multi
  30. def onchange_state(self, state_id):
  31. result = super(ResPartner, self).onchange_state(state_id)
  32. if not state_id:
  33. changes = {
  34. 'domain': {
  35. 'substate': [],
  36. 'region': [],
  37. },
  38. 'value': {
  39. 'substate': False,
  40. 'region': False,
  41. }
  42. }
  43. dict_recursive_update(result, changes)
  44. return result
  45. @api.onchange('substate', 'region')
  46. def onchange_substate_or_region(self):
  47. result = {'domain': {}}
  48. if not self.substate:
  49. result['domain']['substate'] = []
  50. if not self.region:
  51. result['domain']['region'] = []
  52. return result