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
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. @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. """Add new required value when missing.
  49. This is required because this model is created from a QWeb wizard view
  50. that does not populate ``model_id``, and it is easier to hack here than
  51. in the view.
  52. """
  53. vals.setdefault("model_id",
  54. self._get_model_id(vals.get("resource")).id)
  55. return super(IrExports, self).create(vals)