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.

66 lines
2.1 KiB

  1. # Copyright 2015-2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. class IrExports(models.Model):
  6. _inherit = 'ir.exports'
  7. name = fields.Char(required=True)
  8. resource = fields.Char(
  9. required=False,
  10. readonly=True,
  11. help="Model's technical name.")
  12. model_id = fields.Many2one(
  13. "ir.model",
  14. "Model",
  15. store=True,
  16. domain=[("transient", "=", False)],
  17. compute="_compute_model_id",
  18. inverse="_inverse_model_id",
  19. help="Database model to export.")
  20. @api.multi
  21. @api.depends("resource")
  22. def _compute_model_id(self):
  23. """Get the model from the resource."""
  24. for s in self:
  25. s.model_id = self._get_model_id(s.resource)
  26. @api.multi
  27. @api.onchange("model_id")
  28. def _inverse_model_id(self):
  29. """Get the resource from the model."""
  30. for s in self:
  31. s.resource = s.model_id.model
  32. @api.multi
  33. @api.onchange("resource")
  34. def _onchange_resource(self):
  35. """Void fields if model is changed in a view."""
  36. for s in self:
  37. s.export_fields = False
  38. @api.model
  39. def _get_model_id(self, resource):
  40. """Return a model object from its technical name.
  41. :param str resource:
  42. Technical name of the model, like ``ir.model``.
  43. """
  44. return self.env["ir.model"].search([("model", "=", resource)])
  45. @api.model
  46. def create(self, vals):
  47. """Check required values when creating the record.
  48. Odoo's export dialog populates ``resource``, while this module's new
  49. form populates ``model_id``. At least one of them is required to
  50. trigger the methods that fill up the other, so this should fail if
  51. one is missing.
  52. """
  53. if not any(f in vals for f in {"model_id", "resource"}):
  54. raise ValidationError(_("You must supply a model or resource."))
  55. return super(IrExports, self).create(vals)