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.

102 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.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. vals['ref_ir_value_id'] = self.env['ir.values'].create({
  64. 'name': button_name,
  65. 'model': src_obj,
  66. 'key2': 'client_action_multi',
  67. 'value': "ir.actions.act_window," +
  68. str(vals['ref_ir_act_window_id']),
  69. }).id
  70. self.write(vals)
  71. return True
  72. @api.multi
  73. def unlink_action(self):
  74. for mass in self:
  75. try:
  76. if mass.ref_ir_act_window_id:
  77. mass.ref_ir_act_window_id.unlink()
  78. if mass.ref_ir_value_id:
  79. mass.ref_ir_value_id.unlink()
  80. except:
  81. raise UserError(_("Deletion of the action record failed."))
  82. return True
  83. @api.multi
  84. def unlink(self):
  85. self.unlink_action()
  86. return super(MassObject, self).unlink()
  87. @api.returns('self', lambda value: value.id)
  88. def copy(self, default=None):
  89. if default is None:
  90. default = {}
  91. default.update({'name': _("%s (copy)" % self.name), 'field_ids': []})
  92. return super(MassObject, self).copy(default)