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.

46 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. from openerp import api, fields, models, _
  5. from openerp.exceptions import UserError
  6. from openerp import SUPERUSER_ID
  7. class IrModel(models.Model):
  8. _inherit = 'ir.model'
  9. avoid_quick_create = fields.Boolean()
  10. def _patch_quick_create(self, cr, ids):
  11. def _wrap_name_create():
  12. def wrapper(self, cr, uid, name, context=None):
  13. raise UserError(_("Can't create quickly. Opening create form"))
  14. return wrapper
  15. for model in self.browse(cr, SUPERUSER_ID, ids):
  16. if model.avoid_quick_create:
  17. model_name = model.model
  18. model_obj = self.pool.get(model_name)
  19. if model_obj and not hasattr(model_obj, 'check_quick_create'):
  20. model_obj._patch_method('name_create', _wrap_name_create())
  21. model_obj.check_quick_create = True
  22. return True
  23. def _register_hook(self, cr):
  24. self._patch_quick_create(cr, self.search(cr, SUPERUSER_ID, []))
  25. return super(IrModel, self)._register_hook(cr)
  26. @api.model
  27. @api.returns('self', lambda value: value.id)
  28. def create(self, vals):
  29. ir_model = super(IrModel, self).create(vals)
  30. self.pool[self._name]._patch_quick_create(self.env.cr, [ir_model.id])
  31. return ir_model
  32. @api.multi
  33. def write(self, vals):
  34. res = super(IrModel, self).write(vals)
  35. self.pool[self._name]._patch_quick_create(self.env.cr, self.ids)
  36. return res