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.

138 lines
5.6 KiB

10 years ago
10 years ago
10 years ago
  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(
  69. cr, SUPERUSER_ID,
  70. {
  71. 'name': button_name,
  72. 'type': 'ir.actions.act_window',
  73. 'res_model': 'mass.editing.wizard',
  74. 'src_model': src_obj,
  75. 'view_type': 'form',
  76. 'context': "{'mass_editing_object' : %d}" % (data.id),
  77. 'view_mode': 'form,tree',
  78. 'target': 'new',
  79. 'auto_refresh': 1,
  80. },
  81. context)
  82. vals['ref_ir_value'] = ir_values_obj.create(
  83. cr, SUPERUSER_ID,
  84. {
  85. 'name': button_name,
  86. 'model': src_obj,
  87. 'key2': 'client_action_multi',
  88. 'value': (
  89. "ir.actions.act_window," +
  90. str(vals['ref_ir_act_window'])),
  91. 'object': True,
  92. },
  93. context)
  94. self.write(
  95. cr, uid, ids,
  96. {
  97. 'ref_ir_act_window': vals.get('ref_ir_act_window', False),
  98. 'ref_ir_value': vals.get('ref_ir_value', False),
  99. },
  100. context)
  101. return True
  102. def unlink_action(self, cr, uid, ids, context=None):
  103. for template in self.browse(cr, uid, ids, context=context):
  104. try:
  105. if template.ref_ir_act_window:
  106. act_window_obj = self.pool['ir.actions.act_window']
  107. act_window_obj.unlink(
  108. cr, SUPERUSER_ID, [template.ref_ir_act_window.id],
  109. context=context)
  110. if template.ref_ir_value:
  111. ir_values_obj = self.pool['ir.values']
  112. ir_values_obj.unlink(
  113. cr, SUPERUSER_ID, template.ref_ir_value.id,
  114. context=context)
  115. except:
  116. raise orm.except_orm(
  117. _("Warning"),
  118. _("Deletion of the action record failed."))
  119. return True
  120. def unlink(self, cr, uid, ids, context=None):
  121. self.unlink_action(cr, uid, ids, context=context)
  122. return super(MassObject, self).unlink(cr, uid, ids, context=context)
  123. def copy(self, cr, uid, record_id, default=None, context=None):
  124. if default is None:
  125. default = {}
  126. default.update({'name': '', 'field_ids': []})
  127. return super(MassObject, self).copy(
  128. cr, uid, record_id, default, context=context)