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.

70 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Python source code encoding : https://www.python.org/dev/peps/pep-0263/
  3. ##############################################################################
  4. #
  5. # OpenERP, Open Source Management Solution
  6. # This module copyright :
  7. # (c) 2015 Antiun Ingenieria, SL (Madrid, Spain, http://www.antiun.com)
  8. # Antonio Espinosa <antonioea@antiun.com>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Affero General Public License as
  12. # published by the Free Software Foundation, either version 3 of the
  13. # License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. from openerp import models, fields, api, exceptions
  25. from openerp.tools.translate import _
  26. class IrExportsLine(models.Model):
  27. _inherit = 'ir.exports.line'
  28. _order = 'sequence,id'
  29. sequence = fields.Integer()
  30. label = fields.Char(string='Label', compute="_get_label")
  31. def _get_label_string(self):
  32. self.ensure_one()
  33. model_name = self.export_id.resource
  34. label = ''
  35. if not self.name:
  36. return False
  37. for field in self.name.split('/'):
  38. model = self.env['ir.model'].search([('model', '=', model_name)])
  39. field_obj = model.field_id.filtered(lambda r: r.name == field)
  40. if not field_obj:
  41. return False
  42. label = label + _(field_obj.field_description) + '/'
  43. model_name = field_obj.relation
  44. return label.rstrip('/') + ' (' + self.name + ')'
  45. @api.one
  46. @api.constrains('name')
  47. def _check_name(self):
  48. if not self._get_label_string():
  49. raise exceptions.ValidationError(
  50. _("Field '%s' does not exist") % self.name)
  51. lines = self.search([('export_id', '=', self.export_id.id),
  52. ('name', '=', self.name)])
  53. if len(lines) > 1:
  54. raise exceptions.ValidationError(
  55. _("Field '%s' already exists") % self.name)
  56. @api.one
  57. @api.depends('name')
  58. def _get_label(self):
  59. self.label = self._get_label_string()
  60. @api.onchange('name')
  61. def _onchange_name(self):
  62. self.label = self._get_label_string()