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.

128 lines
8.9 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 == "one2many":
  49. all_fields["selection__" + field.name] = {'type':'selection',
  50. 'string': field_info[field.name]['string'],
  51. 'selection':[('set', 'Set'),
  52. ('remove', 'Remove')]}
  53. all_fields[field.name] = {'type':field.ttype,
  54. 'string': field.field_description,
  55. 'relation': field.relation}
  56. etree.SubElement(xml_group, 'field',
  57. {'name': "selection__" + field.name, 'colspan':'2'})
  58. etree.SubElement(xml_group, 'field',
  59. {'name': field.name, 'colspan':'4', 'nolabel':'1',
  60. 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove_o2m')]}"})
  61. elif field.ttype == "many2one":
  62. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  63. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'relation': field.relation}
  64. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2'})
  65. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'colspan':'2', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  66. elif field.ttype == "char":
  67. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  68. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'size': field.size or 256}
  69. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2', 'colspan':'2'})
  70. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}", 'colspan':'2'})
  71. elif field.ttype == 'selection':
  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', 'colspan':'2', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  75. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'selection': field_info[field.name]['selection']}
  76. else:
  77. all_fields[field.name] = {'type':field.ttype, 'string': field.field_description}
  78. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  79. if field.ttype == 'text':
  80. xml_group = etree.SubElement(xml_group, 'group', {'colspan': '6'})
  81. etree.SubElement(xml_group, 'separator', {'string': all_fields[field.name]['string'], 'colspan': '2'})
  82. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', 'nolabel':'1'})
  83. etree.SubElement(xml_group, 'field', {'name': field.name, 'colspan':'4', 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
  84. else:
  85. all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
  86. etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', })
  87. etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}", 'colspan': '2', })
  88. etree.SubElement(xml_form, 'separator', {'string' : '', 'colspan': '4'})
  89. xml_group3 = etree.SubElement(xml_form, 'footer', {})
  90. etree.SubElement(xml_group3, 'button', {'string' :'Close', 'icon': "gtk-close", 'special' :'cancel'})
  91. etree.SubElement(xml_group3, 'button', {'string' :'Apply', 'icon': "gtk-execute", 'type' :'object', 'name':"action_apply"})
  92. root = xml_form.getroottree()
  93. result['arch'] = etree.tostring(root)
  94. result['fields'] = all_fields
  95. return result
  96. def create(self, cr, uid, vals, context=None):
  97. if context.get('active_model') and context.get('active_ids'):
  98. model_obj = self.pool.get(context.get('active_model'))
  99. dict = {}
  100. for key , val in vals.items():
  101. if key.startswith('selection__'):
  102. split_key = key.split('__', 1)[1]
  103. if val == 'set':
  104. dict.update({split_key: vals.get(split_key, False)})
  105. elif val == 'remove':
  106. dict.update({split_key: False})
  107. elif val == 'remove_m2m':
  108. dict.update({split_key: [(3, id) for id in vals.get(split_key, False)[0][2]]})
  109. elif val == 'add':
  110. m2m_list = []
  111. for m2m_id in vals.get(split_key, False)[0][2]:
  112. m2m_list.append((4, m2m_id))
  113. dict.update({split_key: m2m_list})
  114. if dict:
  115. model_obj.write(cr, uid, context.get('active_ids'), dict, context)
  116. result = super(mass_editing_wizard, self).create(cr, uid, {}, context)
  117. return result
  118. def action_apply(self, cr, uid, ids, context=None):
  119. return {'type': 'ir.actions.act_window_close'}
  120. mass_editing_wizard()
  121. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: