mass_editing.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # This module uses OpenERP, Open Source Management Solution Framework.
  5. # Copyright (C) 2012-Today Serpent Consulting Services (<http://www.serpentcs.com>)
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>
  19. #
  20. ##############################################################################
  21. from openerp import models, fields, api, _
  22. class ir_model_fields(models.Model):
  23. _inherit = 'ir.model.fields'
  24. @api.returns('self') # If we keep @api.v7, we have to add @api.v8 code too.
  25. def search(self, cr, uid, args, offset=0, limit=0, order=None, context=None, count=False):
  26. model_domain = []
  27. for domain in args:
  28. if domain[0] == 'model_id' and domain[2] and type(domain[2]) != list:
  29. model_domain += [('model_id', 'in', map(int, domain[2][1:-1].split(',')))]
  30. else:
  31. model_domain.append(domain)
  32. return super(ir_model_fields, self).search(cr, uid, model_domain, offset=offset, limit=limit, order=order, context=context, count=count)
  33. # @api.v8
  34. # def search(self, args, offset=0, limit=0, order=None, count=False):
  35. # model_domain = []
  36. # for domain in args:
  37. # if domain[0] == 'model_id' and domain[2] and type(domain[2]) != list:
  38. # model_domain += [('model_id', 'in', map(int, domain[2][1:-1].split(',')))]
  39. # else:
  40. # model_domain.append(domain)
  41. # return super(ir_model_fields, self).search(model_domain, offset=offset, limit=limit, order=order, count=count)
  42. class mass_object(models.Model):
  43. _name = "mass.object"
  44. name = fields.Char(string="Name", required=True, select=1)
  45. model_id = fields.Many2one('ir.model', string='Model', required=False)
  46. field_ids = fields.Many2many('ir.model.fields', 'mass_field_rel', 'mass_id', 'field_id', string='Fields')
  47. ref_ir_act_window = fields.Many2one('ir.actions.act_window', string='Sidebar action', readonly=True,
  48. help="Sidebar action to make this template available on records "
  49. "of the related document model")
  50. ref_ir_value = fields.Many2one('ir.values', string='Sidebar button', readonly=True,
  51. help="Sidebar button to open the sidebar action")
  52. model_ids = fields.Many2many('ir.model', string='Model List')
  53. model_list = fields.Char(string='Model List', size=256)
  54. _sql_constraints = [
  55. ('name_uniq', 'unique (name)', _('Name must be unique!')), ]
  56. @api.multi
  57. @api.onchange('model_id')
  58. def onchange_model(self):
  59. if self._context is None: self._context = {}
  60. if not self.model_id:
  61. self.model_ids = [(6, 0, [])]
  62. model_ids = [self.model_id.id]
  63. model_obj = self.env['ir.model']
  64. # active_model_obj = self.env[model_obj.browse(self.model_id.id).model]
  65. self.model_ids = [[6, 0, model_ids]]
  66. self.model_list = [self.model_ids.id]
  67. @api.multi
  68. def create_action(self):
  69. vals = {}
  70. action_obj = self.env['ir.actions.act_window']
  71. data_obj = self.env['ir.model.data']
  72. src_obj = self.model_id.model
  73. button_name = _('Mass Editing (%s)') % self.name
  74. vals['ref_ir_act_window'] = action_obj.create({
  75. 'name': button_name,
  76. 'type': 'ir.actions.act_window',
  77. 'res_model': 'mass.editing.wizard',
  78. 'src_model': src_obj,
  79. 'view_type': 'form',
  80. 'context': "{'mass_editing_object' : %d}" % (self.id),
  81. 'view_mode':'form,tree',
  82. 'target': 'new',
  83. 'auto_refresh':1
  84. }).id
  85. vals['ref_ir_value'] = self.env['ir.values'].create({
  86. 'name': button_name,
  87. 'model': src_obj,
  88. 'key2': 'client_action_multi',
  89. 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
  90. 'object': True,
  91. }).id
  92. self.write({
  93. 'ref_ir_act_window': vals.get('ref_ir_act_window', False),
  94. 'ref_ir_value': vals.get('ref_ir_value', False),
  95. })
  96. return True
  97. @api.one
  98. def unlink_action(self):
  99. if self.ref_ir_act_window:
  100. winid = self.ref_ir_act_window.id
  101. self.env['ir.actions.act_window'].search([('id', '=', winid)]).unlink()
  102. if self.ref_ir_value:
  103. uinid = self.ref_ir_value.id
  104. self.env['ir_values'].search([('id', '=', uinid)]).unlink()
  105. @api.one
  106. def unlink(self):
  107. self.unlink_action()
  108. return super(mass_object, self).unlink()
  109. @api.multi
  110. def copy(self, default):
  111. if default is None:
  112. default = {}
  113. default.update({'name':'', 'field_ids': []})
  114. return super(mass_object, self).copy(default)
  115. class ir_module_module(models.Model):
  116. _inherit = 'ir.module.module'
  117. @api.multi
  118. def module_uninstall(self):
  119. cr, uid, context = self.env.args
  120. action_obj = self.pool.get('ir.actions.act_window')
  121. # search window actions of mass editing and delete it
  122. if self.name == 'mass_editing':
  123. action_ids = action_obj.search (cr, uid, [('res_model', '=', 'mass.editing.wizard')], context=context)
  124. action_obj.unlink(cr, uid, action_ids, context=context)
  125. return super(ir_module_module, self).module_uninstall()