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.

252 lines
13 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 openerp.tools.translate import _
  25. from lxml import etree
  26. class MassEditingWizard(orm.TransientModel):
  27. _name = 'mass.editing.wizard'
  28. def fields_view_get(
  29. self, cr, uid, view_id=None, view_type='form', context=None,
  30. toolbar=False, submenu=False):
  31. s_set = _("Set")
  32. s_add = _("Add")
  33. s_remove = _("Remove")
  34. result = super(MassEditingWizard, self).fields_view_get(
  35. cr, uid, view_id, view_type, context, toolbar, submenu)
  36. if context.get('mass_editing_object'):
  37. mass_object = self.pool['mass.object']
  38. editing_data = mass_object.browse(
  39. cr, uid, context.get('mass_editing_object'), context)
  40. all_fields = {}
  41. xml_form = etree.Element('form', {
  42. 'string': tools.ustr(editing_data.name), 'version': '7.0'})
  43. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  44. etree.SubElement(xml_group, 'label', {
  45. 'string': '', 'colspan': '2'})
  46. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4',
  47. 'col': '4'})
  48. model_obj = self.pool[context.get('active_model')]
  49. field_info = model_obj.fields_get(cr, uid, [], context)
  50. for field in editing_data.field_ids:
  51. if field.ttype == "many2many":
  52. all_fields[field.name] = field_info[field.name]
  53. all_fields["selection__" + field.name] = {
  54. 'type': 'selection',
  55. 'string': field_info[field.name]['string'],
  56. 'selection': [
  57. ('add', s_add), ('set', s_set),
  58. ('remove_m2m', s_remove)]}
  59. xml_group = etree.SubElement(xml_group, 'group', {
  60. 'colspan': '4'})
  61. etree.SubElement(xml_group, 'separator', {
  62. 'string': field_info[field.name]['string'],
  63. 'colspan': '2'})
  64. etree.SubElement(xml_group, 'field', {
  65. 'name': "selection__" + field.name,
  66. 'colspan': '2', 'nolabel': '1'})
  67. etree.SubElement(xml_group, 'field', {
  68. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  69. 'attrs': (
  70. "{'invisible':[('selection__" +
  71. field.name + "','=','remove_m2m')]}")})
  72. elif field.ttype == "one2many":
  73. all_fields["selection__" + field.name] = {
  74. 'type': 'selection',
  75. 'string': field_info[field.name]['string'],
  76. 'selection': [('set', s_set), ('remove', s_remove)]}
  77. all_fields[field.name] = {
  78. 'type': field.ttype, 'string': field.field_description,
  79. 'relation': field.relation}
  80. etree.SubElement(xml_group, 'field', {
  81. 'name': "selection__" + field.name, 'colspan': '2'})
  82. etree.SubElement(xml_group, 'field', {
  83. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  84. 'attrs': (
  85. "{'invisible':[('selection__" +
  86. field.name + "','=','remove_o2m')]}")})
  87. elif field.ttype == "many2one":
  88. all_fields["selection__" + field.name] = {
  89. 'type': 'selection',
  90. 'string': field_info[field.name]['string'],
  91. 'selection': [('set', s_set), ('remove', s_remove)]}
  92. all_fields[field.name] = {
  93. 'type': field.ttype, 'string': field.field_description,
  94. 'relation': field.relation}
  95. etree.SubElement(xml_group, 'field', {
  96. 'name': "selection__" + field.name, 'colspan': '2'})
  97. etree.SubElement(xml_group, 'field', {
  98. 'name': field.name, 'nolabel': '1', 'colspan': '2',
  99. 'attrs': (
  100. "{'invisible':[('selection__" +
  101. field.name + "','=','remove')]}")})
  102. elif field.ttype == "char":
  103. all_fields["selection__" + field.name] = {
  104. 'type': 'selection',
  105. 'string': field_info[field.name]['string'],
  106. 'selection': [('set', s_set), ('remove', s_remove)]}
  107. all_fields[field.name] = {
  108. 'type': field.ttype, 'string': field.field_description,
  109. 'size': field.size or 256}
  110. etree.SubElement(xml_group, 'field', {
  111. 'name': "selection__" + field.name,
  112. 'colspan': '2',
  113. })
  114. etree.SubElement(xml_group, 'field', {
  115. 'name': field.name, 'nolabel': '1',
  116. 'attrs': (
  117. "{'invisible':[('selection__" +
  118. field.name + "','=','remove')]}"),
  119. 'colspan': '2'})
  120. elif field.ttype == 'selection':
  121. all_fields["selection__" + field.name] = {
  122. 'type': 'selection',
  123. 'string': field_info[field.name]['string'],
  124. 'selection': [('set', s_set), ('remove', s_remove)]}
  125. etree.SubElement(xml_group, 'field', {
  126. 'name': "selection__" + field.name, 'colspan': '2'})
  127. etree.SubElement(xml_group, 'field', {
  128. 'name': field.name, 'nolabel': '1', 'colspan': '2',
  129. 'attrs': (
  130. "{'invisible':[('selection__" +
  131. field.name + "','=','remove')]}")})
  132. all_fields[field.name] = {
  133. 'type': field.ttype,
  134. 'string': field.field_description,
  135. 'selection': field_info[field.name]['selection']}
  136. else:
  137. all_fields[field.name] = {
  138. 'type': field.ttype, 'string': field.field_description}
  139. all_fields["selection__" + field.name] = {
  140. 'type': 'selection',
  141. 'string': field_info[field.name]['string'],
  142. 'selection': [('set', s_set), ('remove', s_remove)]}
  143. if field.ttype == 'text':
  144. xml_group = etree.SubElement(xml_group, 'group', {
  145. 'colspan': '6'})
  146. etree.SubElement(xml_group, 'separator', {
  147. 'string': all_fields[field.name]['string'],
  148. 'colspan': '2'})
  149. etree.SubElement(xml_group, 'field', {
  150. 'name': "selection__" + field.name,
  151. 'colspan': '2', 'nolabel': '1'})
  152. etree.SubElement(xml_group, 'field', {
  153. 'name': field.name, 'colspan': '4', 'nolabel': '1',
  154. 'attrs': (
  155. "{'invisible':[('selection__" +
  156. field.name + "','=','remove')]}")})
  157. else:
  158. all_fields["selection__" + field.name] = {
  159. 'type': 'selection',
  160. 'string': field_info[field.name]['string'],
  161. 'selection': [(
  162. 'set', s_set), ('remove', s_remove)]}
  163. etree.SubElement(xml_group, 'field', {
  164. 'name': "selection__" + field.name,
  165. 'colspan': '2', })
  166. etree.SubElement(xml_group, 'field', {
  167. 'name': field.name, 'nolabel': '1',
  168. 'attrs': (
  169. "{'invisible':[('selection__" +
  170. field.name + "','=','remove')]}"),
  171. 'colspan': '2', })
  172. etree.SubElement(
  173. xml_form, 'separator', {'string': '', 'colspan': '4'})
  174. xml_group3 = etree.SubElement(xml_form, 'footer', {})
  175. s_apply = _("Apply")
  176. etree.SubElement(xml_group3, 'button', {
  177. 'string': s_apply, 'icon': "gtk-execute",
  178. 'type': 'object', 'name': "action_apply",
  179. 'class': "oe_highlight"})
  180. s_close = _("Close")
  181. etree.SubElement(xml_group3, 'button', {
  182. 'string': s_close, 'icon': "gtk-close", 'special': 'cancel'})
  183. root = xml_form.getroottree()
  184. result['arch'] = etree.tostring(root)
  185. result['fields'] = all_fields
  186. return result
  187. def read(self, cr, uid, ids, fields, context=None):
  188. """ Without this call, dynamic fields defined in fields_view_get()
  189. generate a log warning, i.e.:
  190. openerp.models: mass.editing.wizard.read()
  191. with unknown field 'myfield'
  192. openerp.models: mass.editing.wizard.read()
  193. with unknown field 'selection__myfield'
  194. """
  195. # We remove fields which are not in _columns
  196. real_fields = [x for x in fields if x in self._columns]
  197. return super(MassEditingWizard, self).read(
  198. cr, uid, ids, real_fields, context=context)
  199. def create(self, cr, uid, vals, context=None):
  200. if context.get('active_model') and context.get('active_ids'):
  201. model_obj = self.pool.get(context.get('active_model'))
  202. model_field_obj = self.pool.get('ir.model.fields')
  203. translation_obj = self.pool.get('ir.translation')
  204. dict = {}
  205. for key, val in vals.items():
  206. if key.startswith('selection__'):
  207. split_key = key.split('__', 1)[1]
  208. if val == 'set':
  209. dict.update({split_key: vals.get(split_key, False)})
  210. elif val == 'remove':
  211. dict.update({split_key: False})
  212. # If field to remove is translatable,
  213. # its translations have to be removed
  214. model_field_id = model_field_obj.search(cr, uid, [
  215. ('model', '=', context.get('active_model')),
  216. ('name', '=', split_key)
  217. ])
  218. if model_field_id and model_field_obj.browse(
  219. cr, uid, model_field_id,
  220. context=context).translate:
  221. translation_ids = translation_obj.search(cr, uid, [
  222. ('res_id', 'in', context.get('active_ids')),
  223. ('type', '=', 'model'),
  224. ('name', '=', u"{0},{1}".format(
  225. context.get('active_model'), split_key))])
  226. translation_obj.unlink(cr, uid, translation_ids,
  227. context=context)
  228. elif val == 'remove_m2m':
  229. dict.update({split_key: [
  230. (3, id) for id in vals.get(
  231. split_key, False)[0][2]]})
  232. elif val == 'add':
  233. m2m_list = []
  234. for m2m_id in vals.get(split_key, False)[0][2]:
  235. m2m_list.append((4, m2m_id))
  236. dict.update({split_key: m2m_list})
  237. if dict:
  238. model_obj.write(
  239. cr, uid, context.get('active_ids'), dict, context)
  240. result = super(MassEditingWizard, self).create(cr, uid, {}, context)
  241. return result
  242. def action_apply(self, cr, uid, ids, context=None):
  243. return {'type': 'ir.actions.act_window_close'}