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.

67 lines
2.1 KiB

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