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.

102 lines
4.1 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. self.create_database_column(cr, uid, pool_obj, this.name)
  32. this.write({'state': 'manual'})
  33. while True:
  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)
  46. offset += 1
  47. return True
  48. def create_database_column(self, cr, uid, pool_obj, field_name):
  49. old = pool_obj._columns[field_name]
  50. field_declaration_args = []
  51. field_declaration_kwargs = dict(
  52. manual=False,
  53. string=old.string,
  54. required=old.required,
  55. readonly=old.readonly,
  56. domain=old._domain,
  57. context=old._context,
  58. states=old.states,
  59. priority=old.priority,
  60. change_default=old.change_default,
  61. size=old.size,
  62. ondelete=old.ondelete,
  63. translate=old.translate,
  64. select=old.select,
  65. )
  66. if old._type == 'many2one':
  67. field_declaration_args = [old._obj]
  68. elif old._type == 'selection':
  69. field_declaration_args = [old.selection]
  70. elif old._type == 'one2many':
  71. field_declaration_args = [old._obj, old._fields_id]
  72. field_declaration_kwargs['limit'] = old._limit
  73. elif old._type == 'many2many':
  74. field_declaration_args = [old._obj]
  75. field_declaration_kwargs['rel'] = old._rel
  76. field_declaration_kwargs['id1'] = old._id1
  77. field_declaration_kwargs['id2'] = old._id2
  78. field_declaration_kwargs['limit'] = old._limit
  79. field_declaration = getattr(fields, old._type)(
  80. *field_declaration_args,
  81. **field_declaration_kwargs)
  82. pool_obj._columns[field_name] = field_declaration
  83. pool_obj._auto_init(cr, {})
  84. def unserialize_field(self, cr, uid, pool_obj, read_record,
  85. serialization_field_name, field_name):
  86. if not field_name in read_record[serialization_field_name]:
  87. return False
  88. pool_obj.write(
  89. cr, uid, read_record['id'],
  90. {
  91. field_name:
  92. read_record[serialization_field_name][field_name],
  93. })