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.

104 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Serpent Consulting Services Pvt. Ltd. (support@serpentcs.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp.exceptions import UserError
  5. from openerp import api, fields, models, _
  6. class MassObject(models.Model):
  7. _name = "mass.object"
  8. _description = "Mass Editing Object"
  9. name = fields.Char('Name', required=True, select=1)
  10. model_id = fields.Many2one('ir.model', 'Model', required=True,
  11. help="Model is used for Selecting Fields. "
  12. "This is editable until Sidebar menu "
  13. "is not created.")
  14. field_ids = fields.Many2many('ir.model.fields', 'mass_field_rel',
  15. 'mass_id', 'field_id', 'Fields')
  16. ref_ir_act_window_id = fields.Many2one('ir.actions.act_window',
  17. 'Sidebar action',
  18. readonly=True,
  19. help="Sidebar action to make this "
  20. "template available on "
  21. "records of the related "
  22. "document model.")
  23. ref_ir_value_id = fields.Many2one('ir.values', 'Sidebar button',
  24. readonly=True,
  25. help="Sidebar button to open "
  26. "the sidebar action.")
  27. model_list = fields.Char('Model List')
  28. _sql_constraints = [
  29. ('name_uniq', 'unique (name)', _('Name must be unique!')),
  30. ]
  31. @api.onchange('model_id')
  32. def _onchange_model_id(self):
  33. self.field_ids = [(6, 0, [])]
  34. model_list = []
  35. if self.model_id:
  36. model_obj = self.env['ir.model']
  37. model_list = [self.model_id.id]
  38. active_model_obj = self.env[self.model_id.model]
  39. if active_model_obj._inherits:
  40. keys = active_model_obj._inherits.keys()
  41. inherits_model_list = model_obj.search([('model', 'in', keys)])
  42. model_list.extend((inherits_model_list and
  43. inherits_model_list.ids or []))
  44. self.model_list = model_list
  45. @api.multi
  46. def create_action(self):
  47. self.ensure_one()
  48. vals = {}
  49. action_obj = self.env['ir.actions.act_window']
  50. src_obj = self.model_id.model
  51. button_name = _('Mass Editing (%s)') % self.name
  52. vals['ref_ir_act_window_id'] = action_obj.create({
  53. 'name': button_name,
  54. 'type': 'ir.actions.act_window',
  55. 'res_model': 'mass.editing.wizard',
  56. 'src_model': src_obj,
  57. 'view_type': 'form',
  58. 'context': "{'mass_editing_object' : %d}" % (self.id),
  59. 'view_mode': 'form, tree',
  60. 'target': 'new',
  61. 'auto_refresh': 1,
  62. }).id
  63. # We make sudo as any user with rights in this model should be able
  64. # to create the action, not only admin
  65. vals['ref_ir_value_id'] = self.env['ir.values'].sudo().create({
  66. 'name': button_name,
  67. 'model': src_obj,
  68. 'key2': 'client_action_multi',
  69. 'value': "ir.actions.act_window," +
  70. str(vals['ref_ir_act_window_id']),
  71. }).id
  72. self.write(vals)
  73. return True
  74. @api.multi
  75. def unlink_action(self):
  76. for mass in self:
  77. try:
  78. if mass.ref_ir_act_window_id:
  79. mass.ref_ir_act_window_id.unlink()
  80. if mass.ref_ir_value_id:
  81. mass.ref_ir_value_id.unlink()
  82. except:
  83. raise UserError(_("Deletion of the action record failed."))
  84. return True
  85. @api.multi
  86. def unlink(self):
  87. self.unlink_action()
  88. return super(MassObject, self).unlink()
  89. @api.returns('self', lambda value: value.id)
  90. def copy(self, default=None):
  91. if default is None:
  92. default = {}
  93. default.update({'name': _("%s (copy)" % self.name), 'field_ids': []})
  94. return super(MassObject, self).copy(default)