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.

103 lines
4.2 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. while True:
  33. ids = pool_obj.search(
  34. cr, uid,
  35. [(this.serialization_field_id.name, '!=', '{}')],
  36. offset=offset*step, limit=step, context=context)
  37. if not ids:
  38. break
  39. for data in pool_obj.read(cr, uid, ids,
  40. [this.serialization_field_id.name],
  41. context=context):
  42. self.unserialize_field(cr, uid, pool_obj, data,
  43. this.serialization_field_id.name,
  44. this.name, context=context)
  45. offset += 1
  46. return True
  47. def create_database_column(self, cr, uid, pool_obj, field_name):
  48. old = pool_obj._columns[field_name]
  49. field_declaration_args = []
  50. field_declaration_kwargs = dict(
  51. manual=old.manual,
  52. string=old.string,
  53. required=old.required,
  54. readonly=old.readonly,
  55. domain=old._domain,
  56. context=old._context,
  57. states=old.states,
  58. priority=old.priority,
  59. change_default=old.change_default,
  60. size=old.size,
  61. ondelete=old.ondelete,
  62. translate=old.translate,
  63. select=old.select,
  64. )
  65. if old._type == 'many2one':
  66. field_declaration_args = [old._obj]
  67. elif old._type == 'selection':
  68. field_declaration_args = [old.selection]
  69. elif old._type == 'one2many':
  70. field_declaration_args = [old._obj, old._fields_id]
  71. field_declaration_kwargs['limit'] = old._limit
  72. elif old._type == 'many2many':
  73. field_declaration_args = [old._obj]
  74. field_declaration_kwargs['rel'] = old._rel
  75. field_declaration_kwargs['id1'] = old._id1
  76. field_declaration_kwargs['id2'] = old._id2
  77. field_declaration_kwargs['limit'] = old._limit
  78. field_declaration = getattr(fields, old._type)(
  79. *field_declaration_args,
  80. **field_declaration_kwargs)
  81. pool_obj._columns[field_name] = field_declaration
  82. pool_obj._auto_init(cr, {'update_custom_fields': True})
  83. def unserialize_field(self, cr, uid, pool_obj, read_record,
  84. serialization_field_name, field_name,
  85. context=None):
  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. },
  94. context=context)