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
  32. domain[0] == 'model_id' and
  33. isinstance(domain[2], basestring)):
  34. model_domain += [
  35. ('model_id', 'in', map(int, domain[2][1:-1].split(',')))
  36. ]
  37. else:
  38. model_domain.append(domain)
  39. return super(ir_model_fields, self).search(
  40. cr, uid, model_domain, offset=offset, limit=limit, order=order,
  41. context=context, count=count)
  42. ir_model_fields()
  43. class mass_object(orm.Model):
  44. _name = "mass.object"
  45. _columns = {
  46. 'name': fields.char("Name", size=64, required=True, select=1),
  47. 'model_id': fields.many2one(
  48. 'ir.model', 'Model', required=True, select=1),
  49. 'field_ids': fields.many2many(
  50. 'ir.model.fields', 'mass_field_rel', 'mass_id', 'field_id',
  51. 'Fields'),
  52. 'ref_ir_act_window': fields.many2one(
  53. 'ir.actions.act_window', 'Sidebar Action', readonly=True,
  54. help="Sidebar action to make this template available on records \
  55. of the related document model"),
  56. 'ref_ir_value': fields.many2one(
  57. 'ir.values', 'Sidebar Button', readonly=True,
  58. help="Sidebar button to open the sidebar action"),
  59. 'model_ids': fields.many2many('ir.model', string='Model List')
  60. }
  61. _sql_constraints = [
  62. ('name_uniq', 'unique (name)', _('Name must be unique!')),
  63. ]
  64. def onchange_model(self, cr, uid, ids, model_id, context=None):
  65. if context is None:
  66. context = {}
  67. if not model_id:
  68. return {'value': {'model_ids': [(6, 0, [])]}}
  69. model_ids = [model_id]
  70. model_obj = self.pool.get('ir.model')
  71. active_model_obj = self.pool.get(model_obj.browse(
  72. cr, uid, model_id).model)
  73. if active_model_obj._inherits:
  74. for key, val in active_model_obj._inherits.items():
  75. found_model_ids = model_obj.search(
  76. cr, uid, [('model', '=', key)], context=context)
  77. model_ids += found_model_ids
  78. return {'value': {'model_ids': [(6, 0, model_ids)]}}
  79. def create_action(self, cr, uid, ids, context=None):
  80. vals = {}
  81. action_obj = self.pool.get('ir.actions.act_window')
  82. ir_values_obj = self.pool.get('ir.values')
  83. for data in self.browse(cr, uid, ids, context=context):
  84. src_obj = data.model_id.model
  85. button_name = _('Mass Editing (%s)') % data.name
  86. vals['ref_ir_act_window'] = action_obj.create(cr, uid, {
  87. 'name': button_name,
  88. 'type': 'ir.actions.act_window',
  89. 'res_model': 'mass.editing.wizard',
  90. 'src_model': src_obj,
  91. 'view_type': 'form',
  92. 'context': "{'mass_editing_object' : %d}" % (data.id),
  93. 'view_mode': 'form,tree',
  94. 'target': 'new',
  95. 'auto_refresh': 1,
  96. }, context)
  97. vals['ref_ir_value'] = ir_values_obj.create(cr, uid, {
  98. 'name': button_name,
  99. 'model': src_obj,
  100. 'key2': 'client_action_multi',
  101. 'value': (
  102. "ir.actions.act_window,%s" % 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: