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.

51 lines
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2013 Agile Business Group sagl (<http://www.agilebg.com>)
  3. # © 2016 ACSONE SA/NA (<http://acsone.eu>)
  4. # © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
  5. from odoo import api, fields, models, _
  6. from odoo.exceptions import UserError
  7. class IrModel(models.Model):
  8. _inherit = 'ir.model'
  9. avoid_quick_create = fields.Boolean()
  10. @api.multi
  11. def _patch_quick_create(self):
  12. @api.multi
  13. def _wrap_name_create():
  14. def wrapper(self):
  15. raise UserError(_("Can't create quickly. Opening create form"))
  16. return wrapper
  17. for model in self:
  18. if model.avoid_quick_create:
  19. model_name = model.model
  20. model_obj = self.env.get(model_name)
  21. if (
  22. not isinstance(model_obj, type(None)) and
  23. not hasattr(model_obj, 'check_quick_create')):
  24. model_obj._patch_method('name_create', _wrap_name_create())
  25. model_obj.check_quick_create = True
  26. return True
  27. def _register_hook(self):
  28. models = self.search([])
  29. models._patch_quick_create()
  30. return super(IrModel, self)._register_hook()
  31. @api.model
  32. @api.returns('self', lambda value: value.id)
  33. def create(self, vals):
  34. ir_model = super(IrModel, self).create(vals)
  35. ir_model._patch_quick_create()
  36. return ir_model
  37. @api.multi
  38. def write(self, vals):
  39. res = super(IrModel, self).write(vals)
  40. self._patch_quick_create()
  41. return res