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.

109 lines
4.4 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013 Therp BV (<http://therp.nl>)
  6. # All Rights Reserved
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (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 Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp.osv.orm import Model
  23. from openerp.osv import fields
  24. class ir_model_fields(Model):
  25. _inherit = 'ir.model.fields'
  26. def action_unserialize_field(self, cr, uid, ids, context=None):
  27. step = 1000
  28. offset = 0
  29. for this in self.browse(cr, uid, ids, context=context):
  30. pool_obj = self.pool.get(this.model_id.model)
  31. needs_write = self.create_database_column(
  32. cr, uid, pool_obj, this.name)
  33. while needs_write:
  34. ids = pool_obj.search(
  35. cr, uid,
  36. [(this.serialization_field_id.name, '!=', '{}')],
  37. offset=offset*step, limit=step, context=context)
  38. if not ids:
  39. break
  40. for data in pool_obj.read(cr, uid, ids,
  41. [this.serialization_field_id.name],
  42. context=context):
  43. self.unserialize_field(cr, uid, pool_obj, data,
  44. this.serialization_field_id.name,
  45. this.name, context=context)
  46. offset += 1
  47. return True
  48. def create_database_column(self, cr, uid, pool_obj, field_name):
  49. needs_write = True
  50. old = pool_obj._columns[field_name]
  51. field_declaration_args = []
  52. field_declaration_kwargs = dict(
  53. manual=old.manual,
  54. string=old.string,
  55. required=old.required,
  56. readonly=old.readonly,
  57. domain=old._domain,
  58. context=old._context,
  59. states=old.states,
  60. priority=old.priority,
  61. change_default=old.change_default,
  62. size=old.size,
  63. ondelete=old.ondelete,
  64. translate=old.translate,
  65. select=old.select,
  66. )
  67. if old._type == 'many2one':
  68. field_declaration_args = [old._obj]
  69. elif old._type == 'selection':
  70. field_declaration_args = [old.selection]
  71. elif old._type == 'one2many':
  72. field_declaration_args = [old._obj, old._fields_id]
  73. field_declaration_kwargs['limit'] = old._limit
  74. needs_write = False
  75. elif old._type == 'many2many':
  76. field_declaration_args = [old._obj]
  77. field_declaration_kwargs['rel'] = old._rel
  78. field_declaration_kwargs['id1'] = old._id1
  79. field_declaration_kwargs['id2'] = old._id2
  80. field_declaration_kwargs['limit'] = old._limit
  81. needs_write = False
  82. field_declaration = getattr(fields, old._type)(
  83. *field_declaration_args,
  84. **field_declaration_kwargs)
  85. pool_obj._columns[field_name] = field_declaration
  86. pool_obj._auto_init(cr, {'update_custom_fields': True})
  87. return needs_write
  88. def unserialize_field(self, cr, uid, pool_obj, read_record,
  89. serialization_field_name, field_name,
  90. context=None):
  91. if not field_name in read_record[serialization_field_name]:
  92. return False
  93. pool_obj.write(
  94. cr, uid, read_record['id'],
  95. {
  96. field_name:
  97. read_record[serialization_field_name][field_name],
  98. },
  99. context=context)
  100. return True