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.

101 lines
4.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 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. 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_ids = fields.Many2many('ir.model', 'mass_model_rel',
  27. 'mass_id', 'model_id', '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_ids = []
  35. if self.model_id:
  36. model_obj = self.env['ir.model']
  37. model_ids = [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_ids = model_obj.search([('model', 'in', keys)])
  42. model_ids.extend((inherits_model_ids and
  43. inherits_model_ids.ids or []))
  44. self.model_ids = [(6, 0, model_ids)]
  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. # We make sudo as any user with rights in this model should be able
  77. # to delete the action, not only admin
  78. self.mapped('ref_ir_act_window_id').sudo().unlink()
  79. self.mapped('ref_ir_value_id').sudo().unlink()
  80. return True
  81. @api.multi
  82. def unlink(self):
  83. self.unlink_action()
  84. return super(MassObject, self).unlink()
  85. @api.multi
  86. @api.returns('self', lambda value: value.id)
  87. def copy(self, default=None):
  88. if default is None:
  89. default = {}
  90. default.update({'name': _("%s (copy)" % self.name), 'field_ids': []})
  91. return super(MassObject, self).copy(default)