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.

216 lines
11 KiB

  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
  23. import openerp.tools as tools
  24. from lxml import etree
  25. class mass_editing_wizard(orm.TransientModel):
  26. _name = 'mass.editing.wizard'
  27. _columns = {
  28. }
  29. def fields_view_get(
  30. self, cr, uid, view_id=None, view_type='form', context=None,
  31. toolbar=False, submenu=False):
  32. result = super(mass_editing_wizard, self).fields_view_get(
  33. cr, uid, view_id, view_type, context, toolbar, submenu)
  34. if context.get('mass_editing_object'):
  35. mass_object = self.pool.get('mass.object')
  36. editing_data = mass_object.browse(
  37. cr, uid, context.get('mass_editing_object'), context)
  38. all_fields = {}
  39. xml_form = etree.Element('form', {
  40. 'string': tools.ustr(editing_data.name), 'version': '7.0'})
  41. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  42. etree.SubElement(xml_group, 'label', {
  43. 'string': '', 'colspan': '2'})
  44. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  45. model_obj = self.pool.get(context.get('active_model'))
  46. field_info = model_obj.fields_get(cr, uid, [], context)
  47. for field in editing_data.field_ids:
  48. if field.ttype == "many2many":
  49. all_fields[field.name] = field_info[field.name]
  50. all_fields["selection__" + field.name] = {
  51. 'type': 'selection',
  52. 'string': field_info[field.name]['string'],
  53. 'selection': [
  54. ('set', 'Set'), ('remove_m2m', 'Remove'),
  55. ('add', 'Add')]}
  56. xml_group = etree.SubElement(xml_group, 'group', {
  57. 'colspan': '4'})
  58. etree.SubElement(xml_group, 'separator', {
  59. 'string': field_info[field.name]['string'],
  60. 'colspan': '2'})
  61. etree.SubElement(xml_group, 'field', {
  62. 'name': "selection__" + field.name,
  63. 'colspan': '2', 'nolabel': '1'})
  64. etree.SubElement(xml_group, 'field', {
  65. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  66. 'attrs': (
  67. "{'invisible':[('selection__" +
  68. field.name + "','=','remove_m2m')]}")})
  69. elif field.ttype == "one2many":
  70. all_fields["selection__" + field.name] = {
  71. 'type': 'selection',
  72. 'string': field_info[field.name]['string'],
  73. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  74. all_fields[field.name] = {
  75. 'type': field.ttype, 'string': field.field_description,
  76. 'relation': field.relation}
  77. etree.SubElement(xml_group, 'field', {
  78. 'name': "selection__" + field.name, 'colspan': '2'})
  79. etree.SubElement(xml_group, 'field', {
  80. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  81. 'attrs': (
  82. "{'invisible':[('selection__" +
  83. field.name + "','=','remove_o2m')]}")})
  84. elif field.ttype == "many2one":
  85. all_fields["selection__" + field.name] = {
  86. 'type': 'selection',
  87. 'string': field_info[field.name]['string'],
  88. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  89. all_fields[field.name] = {
  90. 'type': field.ttype, 'string': field.field_description,
  91. 'relation': field.relation}
  92. etree.SubElement(xml_group, 'field', {
  93. 'name': "selection__" + field.name, 'colspan': '2'})
  94. etree.SubElement(xml_group, 'field', {
  95. 'name': field.name, 'nolabel': '1', 'colspan': '2',
  96. 'attrs': (
  97. "{'invisible':[('selection__" +
  98. field.name + "','=','remove')]}")})
  99. elif field.ttype == "char":
  100. all_fields["selection__" + field.name] = {
  101. 'type': 'selection',
  102. 'string': field_info[field.name]['string'],
  103. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  104. all_fields[field.name] = {
  105. 'type': field.ttype, 'string': field.field_description,
  106. 'size': field.size or 256}
  107. etree.SubElement(xml_group, 'field', {
  108. 'name': "selection__" + field.name,
  109. 'colspan': '2'})
  110. etree.SubElement(xml_group, 'field', {
  111. 'name': field.name, 'nolabel': '1',
  112. 'attrs': (
  113. "{'invisible':[('selection__" +
  114. field.name + "','=','remove')]}"),
  115. 'colspan': '2'})
  116. elif field.ttype == 'selection':
  117. all_fields["selection__" + field.name] = {
  118. 'type': 'selection',
  119. 'string': field_info[field.name]['string'],
  120. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  121. etree.SubElement(xml_group, 'field', {
  122. 'name': "selection__" + field.name, 'colspan': '2'})
  123. etree.SubElement(xml_group, 'field', {
  124. 'name': field.name, 'nolabel': '1', 'colspan': '2',
  125. 'attrs': (
  126. "{'invisible':[('selection__" +
  127. field.name + "','=','remove')]}")})
  128. all_fields[field.name] = {
  129. 'type': field.ttype,
  130. 'string': field.field_description,
  131. 'selection': field_info[field.name]['selection']}
  132. else:
  133. all_fields[field.name] = {
  134. 'type': field.ttype, 'string': field.field_description}
  135. all_fields["selection__" + field.name] = {
  136. 'type': 'selection',
  137. 'string': field_info[field.name]['string'],
  138. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  139. if field.ttype == 'text':
  140. xml_group = etree.SubElement(xml_group, 'group', {
  141. 'colspan': '6'})
  142. etree.SubElement(xml_group, 'separator', {
  143. 'string': all_fields[field.name]['string'],
  144. 'colspan': '2'})
  145. etree.SubElement(xml_group, 'field', {
  146. 'name': "selection__" + field.name,
  147. 'colspan': '2', 'nolabel': '1'})
  148. etree.SubElement(xml_group, 'field', {
  149. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  150. 'attrs': (
  151. "{'invisible':[('selection__" +
  152. field.name + "','=','remove')]}")})
  153. else:
  154. all_fields["selection__" + field.name] = {
  155. 'type': 'selection',
  156. 'string': field_info[field.name]['string'],
  157. 'selection': [(
  158. 'set', 'Set'), ('remove', 'Remove')]}
  159. etree.SubElement(xml_group, 'field', {
  160. 'name': "selection__" + field.name,
  161. 'colspan': '2', })
  162. etree.SubElement(xml_group, 'field', {
  163. 'name': field.name, 'nolabel': '1',
  164. 'attrs': (
  165. "{'invisible':[('selection__" +
  166. field.name + "','=','remove')]}"),
  167. 'colspan': '2', })
  168. etree.SubElement(
  169. xml_form, 'separator', {'string': '', 'colspan': '4'})
  170. xml_group3 = etree.SubElement(xml_form, 'footer', {})
  171. etree.SubElement(xml_group3, 'button', {
  172. 'string': 'Close', 'icon': "gtk-close", 'special': 'cancel'})
  173. etree.SubElement(xml_group3, 'button', {
  174. 'string': 'Apply', 'icon': "gtk-execute",
  175. 'type': 'object', 'name': "action_apply"})
  176. root = xml_form.getroottree()
  177. result['arch'] = etree.tostring(root)
  178. result['fields'] = all_fields
  179. return result
  180. def create(self, cr, uid, vals, context=None):
  181. if context.get('active_model') and context.get('active_ids'):
  182. model_obj = self.pool.get(context.get('active_model'))
  183. dict = {}
  184. for key, val in vals.items():
  185. if key.startswith('selection__'):
  186. split_key = key.split('__', 1)[1]
  187. if val == 'set':
  188. dict.update({split_key: vals.get(split_key, False)})
  189. elif val == 'remove':
  190. dict.update({split_key: False})
  191. elif val == 'remove_m2m':
  192. dict.update({split_key: [
  193. (3, id) for id in vals.get(
  194. split_key, False)[0][2]]})
  195. elif val == 'add':
  196. m2m_list = []
  197. for m2m_id in vals.get(split_key, False)[0][2]:
  198. m2m_list.append((4, m2m_id))
  199. dict.update({split_key: m2m_list})
  200. if dict:
  201. model_obj.write(
  202. cr, uid, context.get('active_ids'), dict, context)
  203. result = super(mass_editing_wizard, self).create(cr, uid, {}, context)
  204. return result
  205. def action_apply(self, cr, uid, ids, context=None):
  206. return {'type': 'ir.actions.act_window_close'}
  207. mass_editing_wizard()
  208. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: