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.

117 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 osv import osv
  22. from osv import fields
  23. from lxml import etree
  24. import tools
  25. class mass_editing_wizard(osv.osv_memory):
  26. _name = 'mass.editing.wizard'
  27. _columns = {
  28. }
  29. def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
  30. result = super(mass_editing_wizard, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
  31. if context.get('mass_editing_object'):
  32. mass_object = self.pool.get('mass.object')
  33. editing_data = mass_object.browse(cr, uid, context.get('mass_editing_object'), context)
  34. all_fields = {}
  35. xml_form = etree.Element('form', {'string': tools.ustr(editing_data.name), 'version':'7.0'})
  36. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  37. etree.SubElement(xml_group, 'label', {'string': '', 'colspan': '2'})
  38. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  39. model_obj = self.pool.get(context.get('active_model'))
  40. field_info = model_obj.fields_get(cr, uid, [], context)
  41. for field in editing_data.field_ids:
  42. if field.ttype == "many2many":
  43. all_fields[field.name] = field_info[field.name]
  44. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove_m2m', 'Remove'), ('add', 'Add')]}
  45. xml_group = etree.SubElement(xml_group, 'group', {'colspan': '4'})
  46. etree.SubElement(xml_group, 'separator', {'string': field_info[field.name]['string'], 'colspan': '2'})
  47. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', 'nolabel':'1'})
  48. etree.SubElement(xml_group, 'field', {'name': field.name, 'colspan':'4', 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove_m2m')]}"})
  49. elif field.ttype == "many2one":
  50. if field_info:
  51. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  52. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'relation': field.relation}
  53. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2'})
  54. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'colspan':'2', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  55. elif field.ttype == "char":
  56. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  57. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'size': field.size or 256}
  58. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2', 'colspan':'2'})
  59. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}", 'colspan':'2'})
  60. elif field.ttype == 'selection':
  61. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  62. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2'})
  63. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'colspan':'2', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  64. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'selection': field_info[field.name]['selection']}
  65. else:
  66. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description}
  67. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  68. if field.ttype == 'text':
  69. xml_group = etree.SubElement(xml_group, 'group', {'colspan': '6'})
  70. etree.SubElement(xml_group, 'separator', {'string': all_fields[field.name]['string'], 'colspan': '2'})
  71. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', 'nolabel':'1'})
  72. etree.SubElement(xml_group, 'field', {'name': field.name, 'colspan':'4', 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  73. else:
  74. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  75. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', })
  76. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}", 'colspan': '2', })
  77. etree.SubElement(xml_form, 'separator', {'string' : '', 'colspan': '4'})
  78. xml_group3 = etree.SubElement(xml_form, 'footer', {})
  79. etree.SubElement(xml_group3, 'button', {'string' :'Close', 'icon': "gtk-close", 'special' :'cancel'})
  80. etree.SubElement(xml_group3, 'button', {'string' :'Apply', 'icon': "gtk-execute", 'type' :'object', 'name':"action_apply"})
  81. root = xml_form.getroottree()
  82. result['arch'] = etree.tostring(root)
  83. result['fields'] = all_fields
  84. return result
  85. def create(self, cr, uid, vals, context=None):
  86. if context.get('active_model') and context.get('active_ids'):
  87. model_obj = self.pool.get(context.get('active_model'))
  88. dict = {}
  89. for key , val in vals.items():
  90. if key.startswith('selection__'):
  91. split_key = key.split('__', 1)[1]
  92. if val == 'set':
  93. dict.update({split_key: vals.get(split_key, False)})
  94. elif val == 'remove':
  95. dict.update({split_key: False})
  96. elif val == 'remove_m2m':
  97. dict.update({split_key: [(3, id) for id in vals.get(split_key, False)[0][2]]})
  98. elif val == 'add':
  99. m2m_list = []
  100. for m2m_id in vals.get(split_key, False)[0][2]:
  101. m2m_list.append((4, m2m_id))
  102. dict.update({split_key: m2m_list})
  103. if dict:
  104. model_obj.write(cr, uid, context.get('active_ids'), dict, context)
  105. result = super(mass_editing_wizard, self).create(cr, uid, {}, context)
  106. return result
  107. def action_apply(self, cr, uid, ids, context=None):
  108. return {'type': 'ir.actions.act_window_close'}
  109. mass_editing_wizard()
  110. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: