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.

149 lines
6.0 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.osv import orm, fields, osv
  23. from openerp.tools.translate import _
  24. class ir_model_fields(orm.Model):
  25. _inherit = 'ir.model.fields'
  26. def search(
  27. self, cr, uid, args, offset=0, limit=0, order=None, context=None,
  28. count=False):
  29. model_domain = []
  30. for domain in args:
  31. if domain[0] == 'model_id' and domain[2]\
  32. and type(domain[2]) != list:
  33. model_domain += [(
  34. 'model_id', 'in', map(int, domain[2][1:-1].split(',')))]
  35. else:
  36. model_domain.append(domain)
  37. return super(ir_model_fields, self).search(
  38. cr, uid, model_domain, offset=offset, limit=limit, order=order,
  39. context=context, count=count)
  40. ir_model_fields()
  41. class mass_object(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.get('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.get('ir.actions.act_window')
  80. ir_values_obj = self.pool.get('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, uid, {
  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, uid, {
  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. self.pool.get('ir.actions.act_window').unlink(
  114. cr, uid, template.ref_ir_act_window.id, context)
  115. if template.ref_ir_value:
  116. ir_values_obj = self.pool.get('ir.values')
  117. ir_values_obj.unlink(
  118. cr, uid, template.ref_ir_value.id, context)
  119. except:
  120. raise osv.except_osv(
  121. _("Warning"),
  122. _("Deletion of the action record failed."))
  123. return True
  124. def unlink(self, cr, uid, ids, context=None):
  125. self.unlink_action(cr, uid, ids, context)
  126. return super(mass_object, self).unlink(cr, uid, ids, context)
  127. def copy(self, cr, uid, record_id, default=None, context=None):
  128. if default is None:
  129. default = {}
  130. default.update({'name': '', 'field_ids': []})
  131. return super(mass_object, self).copy(
  132. cr, uid, record_id, default, context)
  133. mass_object()
  134. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: