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.

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