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.

148 lines
6.1 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 IrModelFields(orm.Model):
  26. _inherit = 'ir.model.fields'
  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 domain[0] == 'model_id' and domain[2]\
  33. and type(domain[2]) != list:
  34. model_domain += [(
  35. 'model_id', 'in', map(int, domain[2][1:-1].split(',')))]
  36. else:
  37. model_domain.append(domain)
  38. return super(IrModelFields, self).search(
  39. cr, uid, model_domain, offset=offset, limit=limit, order=order,
  40. context=context, count=count)
  41. class MassObject(orm.Model):
  42. _name = "mass.object"
  43. _columns = {
  44. 'name': fields.char("Name", size=64, required=True, select=1),
  45. 'model_id': fields.many2one(
  46. 'ir.model', 'Model', required=True, select=1),
  47. 'field_ids': fields.many2many(
  48. 'ir.model.fields', 'mass_field_rel', 'mass_id', 'field_id',
  49. 'Fields'),
  50. 'ref_ir_act_window': fields.many2one(
  51. 'ir.actions.act_window', 'Sidebar Action', readonly=True,
  52. help="Sidebar action to make this template available on records \
  53. of the related document model"),
  54. 'ref_ir_value': fields.many2one(
  55. 'ir.values', 'Sidebar Button', readonly=True,
  56. help="Sidebar button to open the sidebar action"),
  57. 'model_ids': fields.many2many('ir.model', string='Model List')
  58. }
  59. _sql_constraints = [
  60. ('name_uniq', 'unique (name)', _('Name must be unique!')),
  61. ]
  62. def onchange_model(self, cr, uid, ids, model_id, context=None):
  63. if context is None:
  64. context = {}
  65. if not model_id:
  66. return {'value': {'model_ids': [(6, 0, [])]}}
  67. model_ids = [model_id]
  68. model_obj = self.pool['ir.model']
  69. active_model_obj = self.pool.get(model_obj.browse(
  70. cr, uid, model_id).model)
  71. if active_model_obj._inherits:
  72. for key, val in active_model_obj._inherits.items():
  73. found_model_ids = model_obj.search(
  74. cr, uid, [('model', '=', key)], context=context)
  75. model_ids += found_model_ids
  76. return {'value': {'model_ids': [(6, 0, model_ids)]}}
  77. def create_action(self, cr, uid, ids, context=None):
  78. vals = {}
  79. action_obj = self.pool['ir.actions.act_window']
  80. ir_values_obj = self.pool['ir.values']
  81. for data in self.browse(cr, uid, ids, context=context):
  82. src_obj = data.model_id.model
  83. button_name = _('Mass Editing (%s)') % data.name
  84. vals['ref_ir_act_window'] = action_obj.create(cr, SUPERUSER_ID, {
  85. 'name': button_name,
  86. 'type': 'ir.actions.act_window',
  87. 'res_model': 'mass.editing.wizard',
  88. 'src_model': src_obj,
  89. 'view_type': 'form',
  90. 'context': "{'mass_editing_object' : %d}" % (data.id),
  91. 'view_mode': 'form,tree',
  92. 'target': 'new',
  93. 'auto_refresh': 1,
  94. }, context)
  95. vals['ref_ir_value'] = ir_values_obj.create(cr, SUPERUSER_ID, {
  96. 'name': button_name,
  97. 'model': src_obj,
  98. 'key2': 'client_action_multi',
  99. 'value': (
  100. "ir.actions.act_window,"
  101. + str(vals['ref_ir_act_window'])),
  102. 'object': True,
  103. }, context)
  104. self.write(cr, uid, ids, {
  105. 'ref_ir_act_window': vals.get('ref_ir_act_window', False),
  106. 'ref_ir_value': vals.get('ref_ir_value', False),
  107. }, context)
  108. return True
  109. def unlink_action(self, cr, uid, ids, context=None):
  110. for template in self.browse(cr, uid, ids, context=context):
  111. try:
  112. if template.ref_ir_act_window:
  113. act_window_obj = self.pool['ir.actions.act_window']
  114. act_window_obj.unlink(
  115. cr, SUPERUSER_ID, [template.ref_ir_act_window.id],
  116. context=context)
  117. if template.ref_ir_value:
  118. ir_values_obj = self.pool['ir.values']
  119. ir_values_obj.unlink(
  120. cr, SUPERUSER_ID, template.ref_ir_value.id,
  121. context=context)
  122. except:
  123. raise orm.except_orm(
  124. _("Warning"),
  125. _("Deletion of the action record failed."))
  126. return True
  127. def unlink(self, cr, uid, ids, context=None):
  128. self.unlink_action(cr, uid, ids, context=context)
  129. return super(MassObject, self).unlink(cr, uid, ids, context=context)
  130. def copy(self, cr, uid, record_id, default=None, context=None):
  131. if default is None:
  132. default = {}
  133. default.update({'name': '', 'field_ids': []})
  134. return super(MassObject, self).copy(
  135. cr, uid, record_id, default, context=context)