You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
5.4 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # This module uses OpenERP, Open Source Management Solution Framework.
  5. # Copyright (C):
  6. # 2012-Today Serpent Consulting Services (<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 SUPERUSER_ID
  23. from openerp.osv import orm, fields
  24. from openerp.tools.translate import _
  25. class MassObject(orm.Model):
  26. _name = "mass.object"
  27. _columns = {
  28. 'name': fields.char("Name", size=64, required=True, select=1),
  29. 'model_id': fields.many2one(
  30. 'ir.model', 'Model', required=True, select=1),
  31. 'field_ids': fields.many2many(
  32. 'ir.model.fields', 'mass_field_rel', 'mass_id', 'field_id',
  33. 'Fields'),
  34. 'ref_ir_act_window': fields.many2one(
  35. 'ir.actions.act_window', 'Sidebar Action', readonly=True,
  36. help="Sidebar action to make this template available on records \
  37. of the related document model"),
  38. 'ref_ir_value': fields.many2one(
  39. 'ir.values', 'Sidebar Button', readonly=True,
  40. help="Sidebar button to open the sidebar action"),
  41. 'model_ids': fields.many2many('ir.model', string='Model List')
  42. }
  43. _sql_constraints = [
  44. ('name_uniq', 'unique (name)', _('Name must be unique!')),
  45. ]
  46. def onchange_model_id(self, cr, uid, ids, model_id, context=None):
  47. if context is None:
  48. context = {}
  49. if not model_id:
  50. return {'value': {'model_ids': [(6, 0, [])]}}
  51. model_ids = [model_id]
  52. model_obj = self.pool['ir.model']
  53. active_model_obj = self.pool.get(model_obj.browse(
  54. cr, uid, model_id).model)
  55. if active_model_obj._inherits:
  56. for key, val in active_model_obj._inherits.items():
  57. found_model_ids = model_obj.search(
  58. cr, uid, [('model', '=', key)], context=context)
  59. model_ids += found_model_ids
  60. return {'value': {'model_ids': [(6, 0, model_ids)]}}
  61. def create_action(self, cr, uid, ids, context=None):
  62. vals = {}
  63. action_obj = self.pool['ir.actions.act_window']
  64. ir_values_obj = self.pool['ir.values']
  65. for data in self.browse(cr, uid, ids, context=context):
  66. src_obj = data.model_id.model
  67. button_name = _('Mass Editing (%s)') % data.name
  68. vals['ref_ir_act_window'] = action_obj.create(cr, SUPERUSER_ID, {
  69. 'name': button_name,
  70. 'type': 'ir.actions.act_window',
  71. 'res_model': 'mass.editing.wizard',
  72. 'src_model': src_obj,
  73. 'view_type': 'form',
  74. 'context': "{'mass_editing_object' : %d}" % (data.id),
  75. 'view_mode': 'form,tree',
  76. 'target': 'new',
  77. 'auto_refresh': 1,
  78. }, context)
  79. vals['ref_ir_value'] = ir_values_obj.create(cr, SUPERUSER_ID, {
  80. 'name': button_name,
  81. 'model': src_obj,
  82. 'key2': 'client_action_multi',
  83. 'value': (
  84. "ir.actions.act_window,"
  85. + str(vals['ref_ir_act_window'])),
  86. 'object': True,
  87. }, context)
  88. self.write(cr, uid, ids, {
  89. 'ref_ir_act_window': vals.get('ref_ir_act_window', False),
  90. 'ref_ir_value': vals.get('ref_ir_value', False),
  91. }, context)
  92. return True
  93. def unlink_action(self, cr, uid, ids, context=None):
  94. for template in self.browse(cr, uid, ids, context=context):
  95. try:
  96. if template.ref_ir_act_window:
  97. act_window_obj = self.pool['ir.actions.act_window']
  98. act_window_obj.unlink(
  99. cr, SUPERUSER_ID, [template.ref_ir_act_window.id],
  100. context=context)
  101. if template.ref_ir_value:
  102. ir_values_obj = self.pool['ir.values']
  103. ir_values_obj.unlink(
  104. cr, SUPERUSER_ID, template.ref_ir_value.id,
  105. context=context)
  106. except:
  107. raise orm.except_orm(
  108. _("Warning"),
  109. _("Deletion of the action record failed."))
  110. return True
  111. def unlink(self, cr, uid, ids, context=None):
  112. self.unlink_action(cr, uid, ids, context=context)
  113. return super(MassObject, self).unlink(cr, uid, ids, context=context)
  114. def copy(self, cr, uid, record_id, default=None, context=None):
  115. if default is None:
  116. default = {}
  117. default.update({'name': '', 'field_ids': []})
  118. return super(MassObject, self).copy(
  119. cr, uid, record_id, default, context=context)