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.

217 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. 'colspan': '2'})
  111. etree.SubElement(xml_group, 'field', {
  112. 'name': field.name, 'nolabel': '1',
  113. 'attrs': (
  114. "{'invisible':[('selection__"
  115. + field.name + "','=','remove')]}"),
  116. 'colspan': '2'})
  117. elif field.ttype == 'selection':
  118. all_fields["selection__" + field.name] = {
  119. 'type': 'selection',
  120. 'string': field_info[field.name]['string'],
  121. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  122. etree.SubElement(xml_group, 'field', {
  123. 'name': "selection__" + field.name, 'colspan': '2'})
  124. etree.SubElement(xml_group, 'field', {
  125. 'name': field.name, 'nolabel': '1', 'colspan': '2',
  126. 'attrs': (
  127. "{'invisible':[('selection__"
  128. + field.name + "','=','remove')]}")})
  129. all_fields[field.name] = {
  130. 'type': field.ttype,
  131. 'string': field.field_description,
  132. 'selection': field_info[field.name]['selection']}
  133. else:
  134. all_fields[field.name] = {
  135. 'type': field.ttype, 'string': field.field_description}
  136. all_fields["selection__" + field.name] = {
  137. 'type': 'selection',
  138. 'string': field_info[field.name]['string'],
  139. 'selection': [('set', 'Set'), ('remove', 'Remove')]}
  140. if field.ttype == 'text':
  141. xml_group = etree.SubElement(xml_group, 'group', {
  142. 'colspan': '6'})
  143. etree.SubElement(xml_group, 'separator', {
  144. 'string': all_fields[field.name]['string'],
  145. 'colspan': '2'})
  146. etree.SubElement(xml_group, 'field', {
  147. 'name': "selection__" + field.name,
  148. 'colspan': '2', 'nolabel': '1'})
  149. etree.SubElement(xml_group, 'field', {
  150. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  151. 'attrs': (
  152. "{'invisible':[('selection__"
  153. + field.name + "','=','remove')]}")})
  154. else:
  155. all_fields["selection__" + field.name] = {
  156. 'type': 'selection',
  157. 'string': field_info[field.name]['string'],
  158. 'selection': [(
  159. 'set', 'Set'), ('remove', 'Remove')]}
  160. etree.SubElement(xml_group, 'field', {
  161. 'name': "selection__" + field.name,
  162. 'colspan': '2', })
  163. etree.SubElement(xml_group, 'field', {
  164. 'name': field.name, 'nolabel': '1',
  165. 'attrs': (
  166. "{'invisible':[('selection__"
  167. + field.name + "','=','remove')]}"),
  168. 'colspan': '2', })
  169. etree.SubElement(
  170. xml_form, 'separator', {'string': '', 'colspan': '4'})
  171. xml_group3 = etree.SubElement(xml_form, 'footer', {})
  172. etree.SubElement(xml_group3, 'button', {
  173. 'string': 'Close', 'icon': "gtk-close", 'special': 'cancel'})
  174. etree.SubElement(xml_group3, 'button', {
  175. 'string': 'Apply', 'icon': "gtk-execute",
  176. 'type': 'object', 'name': "action_apply"})
  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(mass_editing_wizard, 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'}
  208. mass_editing_wizard()
  209. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: