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.

54 lines
1.7 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 not isinstance(model_obj, models.BaseModel):
  23. continue
  24. if model.avoid_quick_create:
  25. model_obj._patch_method(method_name, _wrap_name_create())
  26. else:
  27. method = getattr(model_obj, method_name, None)
  28. if method and hasattr(method, 'origin'):
  29. model_obj._revert_method(method_name)
  30. return True
  31. def _register_hook(self):
  32. models = self.search([])
  33. models._patch_quick_create()
  34. return super(IrModel, self)._register_hook()
  35. @api.model
  36. def create(self, vals):
  37. ir_model = super(IrModel, self).create(vals)
  38. ir_model._patch_quick_create()
  39. return ir_model
  40. @api.multi
  41. def write(self, vals):
  42. res = super(IrModel, self).write(vals)
  43. self._patch_quick_create()
  44. return res