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.

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