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.

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