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.

115 lines
8.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # This module uses OpenERP, Open Source Management Solution Framework.
  5. # Copyright (C) 2012-Today Serpent Consulting Services (<http://www.serpentcs.com>)
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>
  19. #
  20. ##############################################################################
  21. from openerp.osv import orm, fields
  22. import openerp.tools as tools
  23. from lxml import etree
  24. class mass_editing_wizard(orm.TransientModel):
  25. _name = 'mass.editing.wizard'
  26. _columns = {
  27. }
  28. def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
  29. result = super(mass_editing_wizard, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
  30. if context.get('mass_editing_object'):
  31. mass_object = self.pool.get('mass.object')
  32. editing_data = mass_object.browse(cr, uid, context.get('mass_editing_object'), context)
  33. all_fields = {}
  34. xml_form = etree.Element('form', {'string': tools.ustr(editing_data.name), 'version':'7.0'})
  35. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  36. etree.SubElement(xml_group, 'label', {'string': '', 'colspan': '2'})
  37. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  38. model_obj = self.pool.get(context.get('active_model'))
  39. field_info = model_obj.fields_get(cr, uid, [], context)
  40. for field in editing_data.field_ids:
  41. if field.ttype == "many2many":
  42. all_fields[field.name] = field_info[field.name]
  43. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove_m2m', 'Remove'), ('add', 'Add')]}
  44. xml_group = etree.SubElement(xml_group, 'group', {'colspan': '4'})
  45. etree.SubElement(xml_group, 'separator', {'string': field_info[field.name]['string'], 'colspan': '2'})
  46. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', 'nolabel':'1'})
  47. etree.SubElement(xml_group, 'field', {'name': field.name, 'colspan':'4', 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove_m2m')]}"})
  48. elif field.ttype == "many2one":
  49. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  50. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'relation': field.relation}
  51. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2'})
  52. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'colspan':'2', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  53. elif field.ttype == "char":
  54. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  55. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'size': field.size or 256}
  56. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2', 'colspan':'2'})
  57. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}", 'colspan':'2'})
  58. elif field.ttype == 'selection':
  59. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  60. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2'})
  61. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'colspan':'2', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  62. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'selection': field_info[field.name]['selection']}
  63. else:
  64. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description}
  65. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  66. if field.ttype == 'text':
  67. xml_group = etree.SubElement(xml_group, 'group', {'colspan': '6'})
  68. etree.SubElement(xml_group, 'separator', {'string': all_fields[field.name]['string'], 'colspan': '2'})
  69. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', 'nolabel':'1'})
  70. etree.SubElement(xml_group, 'field', {'name': field.name, 'colspan':'4', 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  71. else:
  72. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  73. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', })
  74. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}", 'colspan': '2', })
  75. etree.SubElement(xml_form, 'separator', {'string' : '', 'colspan': '4'})
  76. xml_group3 = etree.SubElement(xml_form, 'footer', {})
  77. etree.SubElement(xml_group3, 'button', {'string' :'Close', 'icon': "gtk-close", 'special' :'cancel'})
  78. etree.SubElement(xml_group3, 'button', {'string' :'Apply', 'icon': "gtk-execute", 'type' :'object', 'name':"action_apply"})
  79. root = xml_form.getroottree()
  80. result['arch'] = etree.tostring(root)
  81. result['fields'] = all_fields
  82. return result
  83. def create(self, cr, uid, vals, context=None):
  84. if context.get('active_model') and context.get('active_ids'):
  85. model_obj = self.pool.get(context.get('active_model'))
  86. dict = {}
  87. for key , val in vals.items():
  88. if key.startswith('selection__'):
  89. split_key = key.split('__', 1)[1]
  90. if val == 'set':
  91. dict.update({split_key: vals.get(split_key, False)})
  92. elif val == 'remove':
  93. dict.update({split_key: False})
  94. elif val == 'remove_m2m':
  95. dict.update({split_key: [(3, id) for id in vals.get(split_key, False)[0][2]]})
  96. elif val == 'add':
  97. m2m_list = []
  98. for m2m_id in vals.get(split_key, False)[0][2]:
  99. m2m_list.append((4, m2m_id))
  100. dict.update({split_key: m2m_list})
  101. if dict:
  102. model_obj.write(cr, uid, context.get('active_ids'), dict, context)
  103. result = super(mass_editing_wizard, self).create(cr, uid, {}, context)
  104. return result
  105. def action_apply(self, cr, uid, ids, context=None):
  106. return {'type': 'ir.actions.act_window_close'}
  107. mass_editing_wizard()
  108. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: