mass_editing.py 5.9 KB

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