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.

100 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 odoo 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, index=1)
  9. model_id = fields.Many2one('ir.model', 'Model', required=True,
  10. ondelete="cascade",
  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. }).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. @api.returns('self', lambda value: value.id)
  86. def copy(self, default=None):
  87. if default is None:
  88. default = {}
  89. default.update({'name': _("%s (copy)" % self.name), 'field_ids': []})
  90. return super(MassObject, self).copy(default)