mass_editing.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 IrModelFields(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(
  26. self, cr, uid, args, offset=0, limit=0, order=None, context=None,
  27. count=False):
  28. model_domain = []
  29. for domain in args:
  30. if (len(domain) > 2 and
  31. domain[0] == 'model_id' and
  32. isinstance(domain[2], basestring)):
  33. model_domain += [
  34. ('model_id', 'in', map(int, domain[2][1:-1].split(',')))
  35. ]
  36. else:
  37. model_domain.append(domain)
  38. return super(IrModelFields, self).search(
  39. cr, uid, model_domain, offset=offset, limit=limit, order=order,
  40. context=context, count=count
  41. )
  42. class MassObject(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. self.model_ids = [[6, 0, model_ids]]
  64. self.model_list = [self.model_ids.id]
  65. @api.multi
  66. def create_action(self):
  67. vals = {}
  68. action_obj = self.env['ir.actions.act_window']
  69. src_obj = self.model_id.model
  70. button_name = _('Mass Editing (%s)') % self.name
  71. vals['ref_ir_act_window'] = action_obj.create({
  72. 'name': button_name,
  73. 'type': 'ir.actions.act_window',
  74. 'res_model': 'mass.editing.wizard',
  75. 'src_model': src_obj,
  76. 'view_type': 'form',
  77. 'context': "{'mass_editing_object' : %d}" % (self.id),
  78. 'view_mode':'form,tree',
  79. 'target': 'new',
  80. 'auto_refresh':1
  81. }).id
  82. vals['ref_ir_value'] = self.env['ir.values'].create({
  83. 'name': button_name,
  84. 'model': src_obj,
  85. 'key2': 'client_action_multi',
  86. 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
  87. 'object': True,
  88. }).id
  89. self.write({
  90. 'ref_ir_act_window': vals.get('ref_ir_act_window', False),
  91. 'ref_ir_value': vals.get('ref_ir_value', False),
  92. })
  93. return True
  94. @api.one
  95. def unlink_action(self):
  96. if self.ref_ir_act_window:
  97. self.env['ir.actions.act_window'].search([('id', '=', self.ref_ir_act_window.id)]).unlink()
  98. if self.ref_ir_value:
  99. self.env['ir_values'].search([('id', '=', self.ref_ir_value.id)]).unlink()
  100. @api.one
  101. def unlink(self):
  102. self.unlink_action()
  103. return super(MassObject, self).unlink()
  104. @api.multi
  105. def copy(self, default):
  106. if default is None:
  107. default = {}
  108. default.update({'name':'', 'field_ids': []})
  109. return super(MassObject, self).copy(default)
  110. class IrModuleModule(models.Model):
  111. _inherit = 'ir.module.module'
  112. @api.multi
  113. def module_uninstall(self):
  114. cr, uid, context = self.env.args
  115. action_obj = self.pool.get('ir.actions.act_window')
  116. # search window actions of mass editing and delete it
  117. if self.name == 'mass_editing':
  118. action_ids = action_obj.search (cr, uid, [('res_model', '=', 'mass.editing.wizard')], context=context)
  119. action_obj.unlink(cr, uid, action_ids, context=context)
  120. return super(IrModuleModule, self).module_uninstall()