mass_editing.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, tools, _
  22. from lxml import etree
  23. class ir_model_fields(models.Model):
  24. _inherit = 'ir.model.fields'
  25. @api.returns('self') # If we keep @api.v7, we have to add @api.v8 code too.
  26. def search(self, cr, uid, args, offset=0, limit=0, order=None, context=None, count=False):
  27. model_domain = []
  28. for domain in args:
  29. if domain[0] == 'model_id' and domain[2] and type(domain[2]) != list:
  30. model_domain += [('model_id', 'in', map(int, domain[2][1:-1].split(',')))]
  31. else:
  32. model_domain.append(domain)
  33. return super(ir_model_fields, self).search(cr, uid, model_domain, offset=offset, limit=limit, order=order, context=context, count=count)
  34. # @api.v8
  35. # def search(self, args, offset=0, limit=0, order=None, count=False):
  36. # model_domain = []
  37. # for domain in args:
  38. # if domain[0] == 'model_id' and domain[2] and type(domain[2]) != list:
  39. # model_domain += [('model_id', 'in', map(int, domain[2][1:-1].split(',')))]
  40. # else:
  41. # model_domain.append(domain)
  42. # return super(ir_model_fields, self).search(model_domain, offset=offset, limit=limit, order=order, count=count)
  43. class mass_object(models.Model):
  44. _name = "mass.object"
  45. name = fields.Char(string="Name", required=True, select=1)
  46. model_id = fields.Many2one('ir.model', string='Model', required=False)
  47. field_ids = fields.Many2many('ir.model.fields', 'mass_field_rel', 'mass_id', 'field_id', string='Fields')
  48. ref_ir_act_window = fields.Many2one('ir.actions.act_window', string='Sidebar action', readonly=True,
  49. help="Sidebar action to make this template available on records "
  50. "of the related document model")
  51. ref_ir_value = fields.Many2one('ir.values', string='Sidebar button', readonly=True,
  52. help="Sidebar button to open the sidebar action")
  53. model_ids = fields.Many2many('ir.model', string='Model List')
  54. model_list = fields.Char(string='Model List', size=256)
  55. _sql_constraints = [
  56. ('name_uniq', 'unique (name)', _('Name must be unique!')), ]
  57. @api.multi
  58. @api.onchange('model_id')
  59. def onchange_model(self):
  60. if self._context is None: self._context = {}
  61. if not self.model_id:
  62. self.model_ids = [(6, 0, [])]
  63. model_ids = [self.model_id.id]
  64. model_obj = self.env['ir.model']
  65. # active_model_obj = self.env[model_obj.browse(self.model_id.id).model]
  66. self.model_ids = [[6, 0, model_ids]]
  67. self.model_list = [self.model_ids.id]
  68. @api.multi
  69. def create_action(self):
  70. vals = {}
  71. action_obj = self.env['ir.actions.act_window']
  72. data_obj = self.env['ir.model.data']
  73. src_obj = self.model_id.model
  74. button_name = _('Mass Editing (%s)') % self.name
  75. vals['ref_ir_act_window'] = action_obj.create({
  76. 'name': button_name,
  77. 'type': 'ir.actions.act_window',
  78. 'res_model': 'mass.editing.wizard',
  79. 'src_model': src_obj,
  80. 'view_type': 'form',
  81. 'context': "{'mass_editing_object' : %d}" % (self.id),
  82. 'view_mode':'form,tree',
  83. 'target': 'new',
  84. 'auto_refresh':1
  85. }).id
  86. vals['ref_ir_value'] = self.env['ir.values'].create({
  87. 'name': button_name,
  88. 'model': src_obj,
  89. 'key2': 'client_action_multi',
  90. 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
  91. 'object': True,
  92. }).id
  93. self.write({
  94. 'ref_ir_act_window': vals.get('ref_ir_act_window', False),
  95. 'ref_ir_value': vals.get('ref_ir_value', False),
  96. })
  97. return True
  98. @api.one
  99. def unlink_action(self):
  100. if self.ref_ir_act_window:
  101. winid = self.ref_ir_act_window.id
  102. self.env['ir.actions.act_window'].search([('id', '=', winid)]).unlink()
  103. if self.ref_ir_value:
  104. uinid = self.ref_ir_value.id
  105. self.env['ir_values'].search([('id', '=', uinid)]).unlink()
  106. @api.one
  107. def unlink(self):
  108. self.unlink_action()
  109. return super(mass_object, self).unlink()
  110. @api.multi
  111. def copy(self, default):
  112. if default is None:
  113. default = {}
  114. default.update({'name':'', 'field_ids': []})
  115. return super(mass_object, self).copy(default)
  116. class ir_module_module(models.Model):
  117. _inherit = 'ir.module.module'
  118. @api.multi
  119. def module_uninstall(self):
  120. cr, uid, context = self.env.args
  121. action_obj = self.pool.get('ir.actions.act_window')
  122. # search window actions of mass editing and delete it
  123. if self.name == 'mass_editing':
  124. action_ids = action_obj.search (cr, uid, [('res_model', '=', 'mass.editing.wizard')], context=context)
  125. action_obj.unlink(cr, uid, action_ids, context=context)
  126. return super(ir_module_module, self).module_uninstall()