mass_editing.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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"
  50. " created")
  51. field_ids = fields.Many2many('ir.model.fields', 'mass_field_rel',
  52. 'mass_id', 'field_id', string='Fields')
  53. ref_ir_act_window = fields.Many2one('ir.actions.act_window',
  54. string='Sidebar action',
  55. readonly=True,
  56. help="Sidebar action to make this "
  57. "template available on records of"
  58. "the related document model")
  59. ref_ir_value = fields.Many2one('ir.values', string='Sidebar button',
  60. readonly=True,
  61. help="Sidebar button to open"
  62. "the sidebar action")
  63. model_list = fields.Char(string='Model List', size=256)
  64. _sql_constraints = [
  65. ('name_uniq', 'unique (name)', _('Name must be unique!')), ]
  66. @api.multi
  67. @api.onchange('model_id')
  68. def onchange_model(self):
  69. self.field_ids = [(6, 0, [])]
  70. model_list = []
  71. if self.model_id:
  72. model_obj = self.env['ir.model']
  73. model_list = [self.model_id.id]
  74. active_model_obj = self.env[self.model_id.model]
  75. if active_model_obj._inherits:
  76. for key, val in active_model_obj._inherits.items():
  77. inherits_model_list = model_obj.search(
  78. [('model', '=', key)])
  79. model_list.extend((inherits_model_list and \
  80. inherits_model_list.ids or []))
  81. self.model_list = model_list
  82. @api.multi
  83. def create_action(self):
  84. vals = {}
  85. action_obj = self.env['ir.actions.act_window']
  86. src_obj = self.model_id.model
  87. button_name = _('Mass Editing (%s)') % self.name
  88. vals['ref_ir_act_window'] = action_obj.create({
  89. 'name': button_name,
  90. 'type': 'ir.actions.act_window',
  91. 'res_model': 'mass.editing.wizard',
  92. 'src_model': src_obj,
  93. 'view_type': 'form',
  94. 'context': "{'mass_editing_object' : %d}" % (self.id),
  95. 'view_mode': 'form, tree',
  96. 'target': 'new',
  97. 'auto_refresh': 1
  98. }).id
  99. vals['ref_ir_value'] = self.env['ir.values'].create({
  100. 'name': button_name,
  101. 'model': src_obj,
  102. 'key2': 'client_action_multi',
  103. 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
  104. 'object': True,
  105. }).id
  106. self.write(vals)
  107. return True
  108. @api.one
  109. def unlink_action(self):
  110. if self.ref_ir_act_window:
  111. self.ref_ir_act_window.unlink()
  112. if self.ref_ir_value:
  113. self.ref_ir_value.unlink()
  114. @api.one
  115. def unlink(self):
  116. self.unlink_action()
  117. return super(MassObject, self).unlink()
  118. @api.multi
  119. def copy(self, default):
  120. if default is None:
  121. default = {}
  122. default.update({'name': _("%s (copy)" % self.name), 'field_ids': []})
  123. return super(MassObject, self).copy(default)
  124. class IrModuleModule(models.Model):
  125. _inherit = 'ir.module.module'
  126. @api.multi
  127. def module_uninstall(self):
  128. # search window actions of mass editing and delete it
  129. if self.name == 'mass_editing':
  130. values_obj = self.env['ir.values']
  131. actions = self.env['ir.actions.act_window'].search(
  132. [('res_model', '=', 'mass.editing.wizard')])
  133. for action in actions:
  134. values_obj.search(
  135. [('value', '=', 'ir.actions.act_window,%s' % action.id)]
  136. ).unlink()
  137. actions.unlink()
  138. return super(IrModuleModule, self).module_uninstall()