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.

126 lines
8.5 KiB

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