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.

213 lines
11 KiB

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. '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': '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 create(self, cr, uid, vals, context=None):
  182. if context.get('active_model') and context.get('active_ids'):
  183. model_obj = self.pool.get(context.get('active_model'))
  184. dict = {}
  185. for key, val in vals.items():
  186. if key.startswith('selection__'):
  187. split_key = key.split('__', 1)[1]
  188. if val == 'set':
  189. dict.update({split_key: vals.get(split_key, False)})
  190. elif val == 'remove':
  191. dict.update({split_key: False})
  192. elif val == 'remove_m2m':
  193. dict.update({split_key: [
  194. (3, id) for id in vals.get(
  195. split_key, False)[0][2]]})
  196. elif val == 'add':
  197. m2m_list = []
  198. for m2m_id in vals.get(split_key, False)[0][2]:
  199. m2m_list.append((4, m2m_id))
  200. dict.update({split_key: m2m_list})
  201. if dict:
  202. model_obj.write(
  203. cr, uid, context.get('active_ids'), dict, context)
  204. result = super(MassEditingWizard, self).create(cr, uid, {}, context)
  205. return result
  206. def action_apply(self, cr, uid, ids, context=None):
  207. return {'type': 'ir.actions.act_window_close'}