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.

227 lines
12 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 MassEditingWizard(orm.TransientModel):
  26. _name = 'mass.editing.wizard'
  27. def fields_view_get(
  28. self, cr, uid, view_id=None, view_type='form', context=None,
  29. toolbar=False, submenu=False):
  30. result = super(MassEditingWizard, self).fields_view_get(
  31. cr, uid, view_id, view_type, context, toolbar, submenu)
  32. if context.get('mass_editing_object'):
  33. mass_object = self.pool['mass.object']
  34. editing_data = mass_object.browse(
  35. cr, uid, context.get('mass_editing_object'), context)
  36. all_fields = {}
  37. xml_form = etree.Element('form', {
  38. 'string': tools.ustr(editing_data.name), 'version': '7.0'})
  39. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  40. etree.SubElement(xml_group, 'label', {
  41. 'string': '', 'colspan': '2'})
  42. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4',
  43. 'col': '4'})
  44. model_obj = self.pool[context.get('active_model')]
  45. field_info = model_obj.fields_get(cr, uid, [], context)
  46. for field in editing_data.field_ids:
  47. if field.ttype == "many2many":
  48. all_fields[field.name] = field_info[field.name]
  49. all_fields["selection__" + field.name] = {
  50. 'type': 'selection',
  51. 'string': field_info[field.name]['string'],
  52. 'selection': [
  53. ('set', 'Set'), ('remove_m2m', 'Remove'),
  54. ('add', 'Add')]}
  55. xml_group = etree.SubElement(xml_group, 'group', {
  56. 'colspan': '4'})
  57. etree.SubElement(xml_group, 'separator', {
  58. 'string': field_info[field.name]['string'],
  59. 'colspan': '2'})
  60. etree.SubElement(xml_group, 'field', {
  61. 'name': "selection__" + field.name,
  62. 'colspan': '2', 'nolabel': '1'})
  63. etree.SubElement(xml_group, 'field', {
  64. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  65. 'attrs': (
  66. "{'invisible':[('selection__" +
  67. field.name + "','=','remove_m2m')]}")})
  68. elif field.ttype == "one2many":
  69. all_fields["selection__" + field.name] = {
  70. 'type': 'selection',
  71. 'string': field_info[field.name]['string'],
  72. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  73. all_fields[field.name] = {
  74. 'type': field.ttype, 'string': field.field_description,
  75. 'relation': field.relation}
  76. etree.SubElement(xml_group, 'field', {
  77. 'name': "selection__" + field.name, 'colspan': '2'})
  78. etree.SubElement(xml_group, 'field', {
  79. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  80. 'attrs': (
  81. "{'invisible':[('selection__" +
  82. field.name + "','=','remove_o2m')]}")})
  83. elif field.ttype == "many2one":
  84. all_fields["selection__" + field.name] = {
  85. 'type': 'selection',
  86. 'string': field_info[field.name]['string'],
  87. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  88. all_fields[field.name] = {
  89. 'type': field.ttype, 'string': field.field_description,
  90. 'relation': field.relation}
  91. etree.SubElement(xml_group, 'field', {
  92. 'name': "selection__" + field.name, 'colspan': '2'})
  93. etree.SubElement(xml_group, 'field', {
  94. 'name': field.name, 'nolabel': '1', 'colspan': '2',
  95. 'attrs': (
  96. "{'invisible':[('selection__" +
  97. field.name + "','=','remove')]}")})
  98. elif field.ttype == "char":
  99. all_fields["selection__" + field.name] = {
  100. 'type': 'selection',
  101. 'string': field_info[field.name]['string'],
  102. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  103. all_fields[field.name] = {
  104. 'type': field.ttype, 'string': field.field_description,
  105. 'size': field.size or 256}
  106. etree.SubElement(xml_group, 'field', {
  107. 'name': "selection__" + field.name,
  108. 'colspan': '2',
  109. })
  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': 'Apply', 'icon': "gtk-execute",
  173. 'type': 'object', 'name': "action_apply",
  174. 'class': "oe_highlight"})
  175. etree.SubElement(xml_group3, 'button', {
  176. 'string': 'Close', 'icon': "gtk-close", 'special': 'cancel'})
  177. root = xml_form.getroottree()
  178. result['arch'] = etree.tostring(root)
  179. result['fields'] = all_fields
  180. return result
  181. def read(self, cr, uid, ids, fields, context=None):
  182. """ Without this call, dynamic fields defined in fields_view_get()
  183. generate a log warning, i.e.:
  184. openerp.models: mass.editing.wizard.read()
  185. with unknown field 'myfield'
  186. openerp.models: mass.editing.wizard.read()
  187. with unknown field 'selection__myfield'
  188. """
  189. # We remove fields which are not in _columns
  190. real_fields = [x for x in fields if x in self._columns]
  191. return super(MassEditingWizard, self).read(
  192. cr, uid, ids, real_fields, context=context)
  193. def create(self, cr, uid, vals, context=None):
  194. if context.get('active_model') and context.get('active_ids'):
  195. model_obj = self.pool.get(context.get('active_model'))
  196. dict = {}
  197. for key, val in vals.items():
  198. if key.startswith('selection__'):
  199. split_key = key.split('__', 1)[1]
  200. if val == 'set':
  201. dict.update({split_key: vals.get(split_key, False)})
  202. elif val == 'remove':
  203. dict.update({split_key: False})
  204. elif val == 'remove_m2m':
  205. dict.update({split_key: [
  206. (3, id) for id in vals.get(
  207. split_key, False)[0][2]]})
  208. elif val == 'add':
  209. m2m_list = []
  210. for m2m_id in vals.get(split_key, False)[0][2]:
  211. m2m_list.append((4, m2m_id))
  212. dict.update({split_key: m2m_list})
  213. if dict:
  214. model_obj.write(
  215. cr, uid, context.get('active_ids'), dict, context)
  216. result = super(MassEditingWizard, self).create(cr, uid, {}, context)
  217. return result
  218. def action_apply(self, cr, uid, ids, context=None):
  219. return {'type': 'ir.actions.act_window_close'}