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.

56 lines
1.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 ACSONE SA/NA (<http://acsone.eu>)
  3. # Copyright 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
  4. # Copyright 2018 Simone Rubino - Agile Business Group
  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. def _wrap_name_create():
  13. @api.model
  14. def wrapper(self, name):
  15. raise UserError(_(
  16. "Can't create %s with name %s quickly.\n"
  17. "Please contact your system administrator to disable "
  18. "this behaviour.") % (self._name, name))
  19. return wrapper
  20. method_name = 'name_create'
  21. for model in self:
  22. model_obj = self.env.get(model.model)
  23. if model.avoid_quick_create and model_obj is not None:
  24. model_obj._patch_method(method_name, _wrap_name_create())
  25. else:
  26. method = getattr(model_obj, method_name, None)
  27. if method and hasattr(method, 'origin'):
  28. model_obj._revert_method(method_name)
  29. return True
  30. def _register_hook(self):
  31. models = self.search([])
  32. models._patch_quick_create()
  33. return super(IrModel, self)._register_hook()
  34. @api.model
  35. @api.returns('self', lambda value: value.id)
  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. if 'avoid_quick_create' in vals:
  45. self.pool.signal_registry_change()
  46. return res