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.

307 lines
13 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2012 Serpent Consulting Services
  6. # (<http://www.serpentcs.com>)
  7. # Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>
  21. #
  22. ##############################################################################
  23. from osv import osv
  24. from lxml import etree
  25. import tools
  26. class mass_editing_wizard(osv.osv_memory):
  27. _name = 'mass.editing.wizard'
  28. _columns = {
  29. }
  30. def fields_view_get(self, cr, uid, view_id=None, view_type='form',
  31. context=None, 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(
  40. 'form', {'string': tools.ustr(editing_data.name)})
  41. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  42. etree.SubElement(
  43. xml_group, 'label', {'string': '', 'colspan': '2'})
  44. xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
  45. model_obj = self.pool.get(context.get('active_model'))
  46. for field in editing_data.field_ids:
  47. if field.ttype == "many2many":
  48. field_info = model_obj.fields_get(
  49. cr, uid, [field.name], context)
  50. all_fields[field.name] = field_info[field.name]
  51. all_fields["selection_" + field.name] = {
  52. 'type': 'selection',
  53. 'string': field_info[field.name]['string'],
  54. 'selection': [
  55. ('set', 'Set'),
  56. ('remove_m2m', 'Remove'),
  57. ('add', 'Add')
  58. ]
  59. }
  60. xml_group = etree.SubElement(
  61. xml_group, 'group', {'colspan': '4'})
  62. etree.SubElement(
  63. xml_group, 'separator',
  64. {
  65. 'string': field_info[field.name]['string'],
  66. 'colspan': '2'
  67. })
  68. etree.SubElement(
  69. xml_group, 'field',
  70. {
  71. 'name': "selection_" + field.name,
  72. 'colspan': '2',
  73. 'nolabel': '1'
  74. })
  75. etree.SubElement(
  76. xml_group, 'field',
  77. {
  78. 'name': field.name,
  79. 'colspan': '4',
  80. 'nolabel': '1',
  81. 'attrs': "{'invisible':[('selection_" + field.name
  82. + "','=','remove_m2m')]}"
  83. })
  84. elif field.ttype == "many2one":
  85. field_info = model_obj.fields_get(
  86. cr, uid, [field.name], context)
  87. if field_info:
  88. all_fields["selection_" + field.name] = {
  89. 'type': 'selection',
  90. 'string': field_info[field.name]['string'],
  91. 'selection': [
  92. ('set', 'Set'),
  93. ('remove', 'Remove')
  94. ]
  95. }
  96. all_fields[field.name] = {
  97. 'type': field.ttype,
  98. 'string': field.field_description,
  99. 'relation': field.relation
  100. }
  101. etree.SubElement(
  102. xml_group, 'field',
  103. {
  104. 'name': "selection_" + field.name,
  105. 'colspan': '2'
  106. })
  107. etree.SubElement(
  108. xml_group, 'field',
  109. {
  110. 'name': field.name,
  111. 'nolabel': '1',
  112. 'colspan': '2',
  113. 'attrs': "{'invisible':[('selection_" +
  114. field.name + "','=','remove')]}"
  115. })
  116. elif field.ttype == "char":
  117. field_info = model_obj.fields_get(
  118. cr, uid, [field.name], context)
  119. all_fields["selection_" + field.name] = {
  120. 'type': 'selection',
  121. 'string': field_info[field.name]['string'],
  122. 'selection': [
  123. ('set', 'Set'),
  124. ('remove', 'Remove')
  125. ]
  126. }
  127. all_fields[field.name] = {
  128. 'type': field.ttype,
  129. 'string': field.field_description,
  130. 'size': field.size or 256
  131. }
  132. etree.SubElement(
  133. xml_group, 'field',
  134. {
  135. 'name': "selection_" + field.name,
  136. 'colspan': '2',
  137. })
  138. etree.SubElement(
  139. xml_group, 'field',
  140. {
  141. 'name': field.name,
  142. 'nolabel': '1',
  143. 'attrs': "{'invisible':[('selection_" +
  144. field.name + "','=','remove')]}",
  145. 'colspan': '2'
  146. })
  147. elif field.ttype == 'selection':
  148. field_info = model_obj.fields_get(
  149. cr, uid, [field.name], context)
  150. all_fields["selection_" + field.name] = {
  151. 'type': 'selection',
  152. 'string': field_info[field.name]['string'],
  153. 'selection': [
  154. ('set', 'Set'),
  155. ('remove', 'Remove')
  156. ]
  157. }
  158. field_info = model_obj.fields_get(
  159. cr, uid, [field.name], context)
  160. etree.SubElement(
  161. xml_group, 'field',
  162. {
  163. 'name': "selection_" + field.name,
  164. 'colspan': '2'
  165. })
  166. etree.SubElement(
  167. xml_group, 'field',
  168. {
  169. 'name': field.name,
  170. 'nolabel': '1',
  171. 'colspan': '2',
  172. 'attrs': "{'invisible':[('selection_" +
  173. field.name + "','=','remove')]}"
  174. })
  175. all_fields[field.name] = {
  176. 'type': field.ttype,
  177. 'string': field.field_description,
  178. 'selection': field_info[field.name]['selection']
  179. }
  180. else:
  181. field_info = model_obj.fields_get(
  182. cr, uid, [field.name], context)
  183. all_fields[field.name] = {
  184. 'type': field.ttype,
  185. 'string': field.field_description
  186. }
  187. all_fields["selection_" + field.name] = {
  188. 'type': 'selection',
  189. 'string': field_info[field.name]['string'],
  190. 'selection': [
  191. ('set', 'Set'),
  192. ('remove', 'Remove')
  193. ]
  194. }
  195. if field.ttype == 'text':
  196. xml_group = etree.SubElement(
  197. xml_group, 'group', {'colspan': '6'})
  198. etree.SubElement(
  199. xml_group, 'separator',
  200. {
  201. 'string': all_fields[field.name]['string'],
  202. 'colspan': '2'
  203. })
  204. etree.SubElement(
  205. xml_group, 'field',
  206. {
  207. 'name': "selection_" + field.name,
  208. 'colspan': '2',
  209. 'nolabel': '1',
  210. })
  211. etree.SubElement(
  212. xml_group, 'field',
  213. {
  214. 'name': field.name,
  215. 'colspan': '4',
  216. 'nolabel': '1',
  217. 'attrs': "{'invisible':[('selection_" +
  218. field.name + "','=','remove')]}"
  219. })
  220. else:
  221. all_fields["selection_" + field.name] = {
  222. 'type': 'selection',
  223. 'string': field_info[field.name]['string'],
  224. 'selection': [
  225. ('set', 'Set'),
  226. ('remove', 'Remove')
  227. ]
  228. }
  229. etree.SubElement(
  230. xml_group, 'field',
  231. {
  232. 'name': "selection_" + field.name,
  233. 'colspan': '2',
  234. })
  235. etree.SubElement(
  236. xml_group, 'field',
  237. {
  238. 'name': field.name,
  239. 'nolabel': '1',
  240. 'attrs': "{'invisible':[('selection_" +
  241. field.name + "','=','remove')]}",
  242. 'colspan': '2',
  243. })
  244. etree.SubElement(
  245. xml_form, 'separator', {'string': '', 'colspan': '6'})
  246. xml_group3 = etree.SubElement(
  247. xml_form, 'group', {'col': '2', 'colspan': '4'})
  248. etree.SubElement(
  249. xml_group3, 'button',
  250. {
  251. 'string': 'Close',
  252. 'icon': "gtk-close",
  253. 'special': 'cancel'
  254. })
  255. etree.SubElement(
  256. xml_group3, 'button',
  257. {
  258. 'string': 'Apply',
  259. 'icon': "gtk-execute",
  260. 'type': 'object',
  261. 'name': "action_apply"
  262. })
  263. root = xml_form.getroottree()
  264. result['arch'] = etree.tostring(root)
  265. result['fields'] = all_fields
  266. return result
  267. def create(self, cr, uid, vals, context=None):
  268. if context.get('active_model') and context.get('active_ids'):
  269. model_obj = self.pool.get(context.get('active_model'))
  270. model_vals = {}
  271. for key, val in vals.items():
  272. if key.startswith('selection_'):
  273. model_field = key.split('_', 1)[1]
  274. if val == 'set':
  275. model_vals[model_field] = vals[model_field]
  276. elif val == 'remove':
  277. model_vals[model_field] = False
  278. elif val == 'remove_m2m':
  279. m2m_list = []
  280. for m2m_id in vals[model_field][0][2] or []:
  281. m2m_list.append((3, m2m_id))
  282. model_vals[model_field] = m2m_list
  283. elif val == 'add':
  284. m2m_list = []
  285. for m2m_id in vals[model_field][0][2] or []:
  286. m2m_list.append((4, m2m_id))
  287. model_vals[model_field] = m2m_list
  288. if model_vals:
  289. model_obj.write(
  290. cr, uid, context['active_ids'], model_vals, context)
  291. result = super(mass_editing_wizard, self).create(cr, uid, {}, context)
  292. return result
  293. def action_apply(self, cr, uid, ids, context=None):
  294. return {'type': 'ir.actions.act_window_close'}
  295. mass_editing_wizard()
  296. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: