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.

65 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Antiun Ingeniería S.L. - Jairo Llopis
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import api, fields, models
  5. class IrExports(models.Model):
  6. _inherit = 'ir.exports'
  7. name = fields.Char(required=True)
  8. resource = fields.Char(
  9. required=True,
  10. readonly=True,
  11. help="Model's technical name.")
  12. model_id = fields.Many2one(
  13. "ir.model",
  14. "Model",
  15. required=True,
  16. store=True,
  17. domain=[("osv_memory", "=", 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. 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. """Add new required value when missing.
  48. This is required because this model is created from a QWeb wizard view
  49. that does not populate ``model_id``, and it is easier to hack here than
  50. in the view.
  51. """
  52. vals.setdefault("model_id",
  53. self._get_model_id(vals.get("resource")).id)
  54. return super(IrExports, self).create(vals)