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.

258 lines
12 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Serpent Consulting Services Pvt. Ltd. (support@serpentcs.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from lxml import etree
  5. import openerp.tools as tools
  6. from openerp import api, models
  7. class MassEditingWizard(models.TransientModel):
  8. _name = 'mass.editing.wizard'
  9. @api.model
  10. def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
  11. submenu=False):
  12. result =\
  13. super(MassEditingWizard, self).fields_view_get(view_id=view_id,
  14. view_type=view_type,
  15. toolbar=toolbar,
  16. submenu=submenu)
  17. context = self._context
  18. if context.get('mass_editing_object'):
  19. mass_obj = self.env['mass.object']
  20. editing_data = mass_obj.browse(context.get('mass_editing_object'))
  21. all_fields = {}
  22. xml_form = etree.Element('form', {
  23. 'string': tools.ustr(editing_data.name)
  24. })
  25. xml_group = etree.SubElement(xml_form, 'group', {
  26. 'colspan': '6',
  27. 'col': '6',
  28. })
  29. etree.SubElement(xml_group, 'label', {
  30. 'string': '',
  31. 'colspan': '2',
  32. })
  33. xml_group = etree.SubElement(xml_form, 'group', {
  34. 'colspan': '6',
  35. 'col': '6',
  36. })
  37. model_obj = self.env[context.get('active_model')]
  38. field_info = model_obj.fields_get()
  39. for field in editing_data.field_ids:
  40. if field.ttype == "many2many":
  41. all_fields[field.name] = field_info[field.name]
  42. all_fields["selection__" + field.name] = {
  43. 'type': 'selection',
  44. 'string': field_info[field.name]['string'],
  45. 'selection': [('set', 'Set'),
  46. ('remove_m2m', 'Remove'),
  47. ('add', 'Add')]
  48. }
  49. xml_group = etree.SubElement(xml_group, 'group', {
  50. 'colspan': '6',
  51. 'col': '6',
  52. })
  53. etree.SubElement(xml_group, 'separator', {
  54. 'string': field_info[field.name]['string'],
  55. 'colspan': '6',
  56. })
  57. etree.SubElement(xml_group, 'field', {
  58. 'name': "selection__" + field.name,
  59. 'colspan': '6',
  60. 'nolabel': '1'
  61. })
  62. etree.SubElement(xml_group, 'field', {
  63. 'name': field.name,
  64. 'colspan': '6',
  65. 'nolabel': '1',
  66. 'attrs': ("{'invisible': [('selection__" +
  67. field.name + "', '=', 'remove_m2m')]}"),
  68. })
  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. }
  75. all_fields[field.name] = {
  76. 'type': field.ttype,
  77. 'string': field.field_description,
  78. 'relation': field.relation,
  79. }
  80. etree.SubElement(xml_group, 'field', {
  81. 'name': "selection__" + field.name,
  82. 'colspan': '4',
  83. })
  84. etree.SubElement(xml_group, 'field', {
  85. 'name': field.name,
  86. 'colspan': '6',
  87. 'nolabel': '1',
  88. 'attrs': ("{'invisible':[('selection__" +
  89. field.name + "', '=', 'remove_o2m')]}"),
  90. })
  91. elif field.ttype == "many2one":
  92. all_fields["selection__" + field.name] = {
  93. 'type': 'selection',
  94. 'string': field_info[field.name]['string'],
  95. 'selection': [('set', 'Set'), ('remove', 'Remove')],
  96. }
  97. all_fields[field.name] = {
  98. 'type': field.ttype,
  99. 'string': field.field_description,
  100. 'relation': field.relation,
  101. }
  102. etree.SubElement(xml_group, 'field', {
  103. 'name': "selection__" + field.name,
  104. 'colspan': '2',
  105. })
  106. etree.SubElement(xml_group, 'field', {
  107. 'name': field.name,
  108. 'nolabel': '1',
  109. 'colspan': '4',
  110. 'attrs': ("{'invisible':[('selection__" +
  111. field.name + "', '=', 'remove')]}"),
  112. })
  113. elif field.ttype == "char":
  114. all_fields["selection__" + field.name] = {
  115. 'type': 'selection',
  116. 'string': field_info[field.name]['string'],
  117. 'selection': [('set', 'Set'), ('remove', 'Remove')],
  118. }
  119. all_fields[field.name] = {
  120. 'type': field.ttype,
  121. 'string': field.field_description,
  122. 'size': field.size or 256,
  123. }
  124. etree.SubElement(xml_group, 'field', {
  125. 'name': "selection__" + field.name,
  126. 'colspan': '2',
  127. })
  128. etree.SubElement(xml_group, 'field', {
  129. 'name': field.name,
  130. 'nolabel': '1',
  131. 'attrs': ("{'invisible':[('selection__" +
  132. field.name + "','=','remove')]}"),
  133. 'colspan': '4',
  134. })
  135. elif field.ttype == 'selection':
  136. all_fields["selection__" + field.name] = {
  137. 'type': 'selection',
  138. 'string': field_info[field.name]['string'],
  139. 'selection': [('set', 'Set'), ('remove', 'Remove')]
  140. }
  141. etree.SubElement(xml_group, 'field', {
  142. 'name': "selection__" + field.name,
  143. 'colspan': '2',
  144. })
  145. etree.SubElement(xml_group, 'field', {
  146. 'name': field.name,
  147. 'nolabel': '1',
  148. 'colspan': '4',
  149. 'attrs': ("{'invisible':[('selection__" +
  150. field.name + "', '=', 'remove')]}"),
  151. })
  152. all_fields[field.name] = {
  153. 'type': field.ttype,
  154. 'string': field.field_description,
  155. 'selection': field_info[field.name]['selection'],
  156. }
  157. else:
  158. all_fields[field.name] = {
  159. 'type': field.ttype,
  160. 'string': field.field_description,
  161. }
  162. all_fields["selection__" + field.name] = {
  163. 'type': 'selection',
  164. 'string': field_info[field.name]['string'],
  165. 'selection': [('set', 'Set'), ('remove', 'Remove')]
  166. }
  167. if field.ttype == 'text':
  168. xml_group = etree.SubElement(xml_group, 'group', {
  169. 'colspan': '6',
  170. 'col': '6',
  171. })
  172. etree.SubElement(xml_group, 'separator', {
  173. 'string': all_fields[field.name]['string'],
  174. 'colspan': '6',
  175. })
  176. etree.SubElement(xml_group, 'field', {
  177. 'name': "selection__" + field.name,
  178. 'colspan': '6',
  179. 'nolabel': '1',
  180. })
  181. etree.SubElement(xml_group, 'field', {
  182. 'name': field.name,
  183. 'colspan': '6',
  184. 'nolabel': '1',
  185. 'attrs': ("{'invisible':[('selection__" +
  186. field.name + "','=','remove')]}"),
  187. })
  188. else:
  189. all_fields["selection__" + field.name] = {
  190. 'type': 'selection',
  191. 'string': field_info[field.name]['string'],
  192. 'selection': [('set', 'Set'), ('remove', 'Remove')]
  193. }
  194. etree.SubElement(xml_group, 'field', {
  195. 'name': "selection__" + field.name,
  196. 'colspan': '2',
  197. })
  198. etree.SubElement(xml_group, 'field', {
  199. 'name': field.name,
  200. 'nolabel': '1',
  201. 'attrs': ("{'invisible':[('selection__" +
  202. field.name + "','=','remove')]}"),
  203. 'colspan': '4',
  204. })
  205. etree.SubElement(xml_form, 'separator', {
  206. 'string': '',
  207. 'colspan': '6',
  208. 'col': '6',
  209. })
  210. xml_group3 = etree.SubElement(xml_form, 'footer', {})
  211. etree.SubElement(xml_group3, 'button', {
  212. 'string': 'Apply',
  213. 'class': 'btn-primary',
  214. 'type': 'object',
  215. 'name': 'action_apply',
  216. })
  217. etree.SubElement(xml_group3, 'button', {
  218. 'string': 'Close',
  219. 'class': 'btn-default',
  220. 'special': 'cancel',
  221. })
  222. root = xml_form.getroottree()
  223. result['arch'] = etree.tostring(root)
  224. result['fields'] = all_fields
  225. return result
  226. @api.model
  227. def create(self, vals):
  228. if (self._context.get('active_model') and
  229. self._context.get('active_ids')):
  230. model_obj = self.env[self._context.get('active_model')]
  231. values = {}
  232. for key, val in vals.items():
  233. if key.startswith('selection_'):
  234. split_key = key.split('__', 1)[1]
  235. if val == 'set':
  236. values.update({split_key: vals.get(split_key, False)})
  237. elif val == 'remove':
  238. values.update({split_key: False})
  239. elif val == 'remove_m2m':
  240. values.update({split_key: [(5, 0, [])]})
  241. elif val == 'add':
  242. m2m_list = []
  243. for m2m_id in vals.get(split_key, False)[0][2]:
  244. m2m_list.append((4, m2m_id))
  245. values.update({split_key: m2m_list})
  246. if values:
  247. model_obj.browse(self._context.get('active_ids')).write(values)
  248. return super(MassEditingWizard, self).create({})
  249. @api.multi
  250. def action_apply(self):
  251. return {'type': 'ir.actions.act_window_close'}