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.

52 lines
1.6 KiB

  1. # Copyright 2016 ACSONE SA/NA (<http://acsone.eu>)
  2. # Copyright 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
  3. # Copyright 2018 Simone Rubino - Agile Business Group
  4. from odoo import api, fields, models, _
  5. from odoo.exceptions import UserError
  6. class IrModel(models.Model):
  7. _inherit = 'ir.model'
  8. avoid_quick_create = fields.Boolean()
  9. @api.multi
  10. def _patch_quick_create(self):
  11. def _wrap_name_create():
  12. @api.model
  13. def wrapper(self, name):
  14. raise UserError(_(
  15. "Can't create %s with name %s quickly.\n"
  16. "Please contact your system administrator to disable "
  17. "this behaviour.") % (self._name, name))
  18. return wrapper
  19. method_name = 'name_create'
  20. for model in self:
  21. model_obj = self.env.get(model.model)
  22. if model.avoid_quick_create:
  23. model_obj._patch_method(method_name, _wrap_name_create())
  24. else:
  25. method = getattr(model_obj, method_name, None)
  26. if method and hasattr(method, 'origin'):
  27. model_obj._revert_method(method_name)
  28. return True
  29. def _register_hook(self):
  30. models = self.search([])
  31. models._patch_quick_create()
  32. return super(IrModel, self)._register_hook()
  33. @api.model
  34. def create(self, vals):
  35. ir_model = super(IrModel, self).create(vals)
  36. ir_model._patch_quick_create()
  37. return ir_model
  38. @api.multi
  39. def write(self, vals):
  40. res = super(IrModel, self).write(vals)
  41. self._patch_quick_create()
  42. return res