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.

115 lines
4.8 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 import orm
  23. from openerp.tools.translate import _
  24. class ir_model_fields(orm.Model):
  25. _inherit = 'ir.model.fields'
  26. def action_unserialize_field(self, cr, uid, ids, context=None):
  27. step = 1000
  28. offset = 0
  29. # Prevent _auto_init to commit the transaction
  30. # before the data is migrated safely
  31. commit_org = cr.commit
  32. cr.commit = lambda *args: None
  33. try:
  34. for this in self.browse(cr, uid, ids, context=context):
  35. pool_obj = self.pool.get(this.model_id.model)
  36. self.create_database_column(cr, uid, pool_obj, this.name,
  37. context=context)
  38. while True:
  39. ids = pool_obj.search(
  40. cr, uid,
  41. [(this.serialization_field_id.name, '!=', '{}')],
  42. offset=offset*step, limit=step, context=context)
  43. if not ids:
  44. break
  45. for data in pool_obj.read(cr, uid, ids,
  46. [this.serialization_field_id.name],
  47. context=context):
  48. self.unserialize_field(cr, uid, pool_obj, data,
  49. this.serialization_field_id.name,
  50. this.name, context=context)
  51. offset += 1
  52. finally:
  53. cr.commit = commit_org
  54. return True
  55. def create_database_column(self, cr, uid, pool_obj, field_name,
  56. context=None):
  57. old = pool_obj._columns[field_name]
  58. if not old.manual:
  59. raise orm.except_orm(
  60. _('Error'),
  61. _('This operation can only be performed on manual fields'))
  62. if old._type == 'many2many':
  63. # Cross table name length of manually created many2many
  64. # fields can easily become too large. Although it would
  65. # probably work if the table name length was within bounds,
  66. # this scenario has not been tested because of this limitation.
  67. raise orm.except_orm(
  68. _("Error"),
  69. _("Many2many fields are not supported. See "
  70. "https://bugs.launchpad.net/openobject-server/+bug/1174078 "
  71. "for more information"))
  72. if old._type == 'one2many':
  73. # How to get a safe field name for the relation field
  74. # on the target model?
  75. raise orm.except_orm(
  76. _("Error"),
  77. _("One2many fields are not handled yet"))
  78. # ORM prohibits to change the 'storing system' of the field
  79. cr.execute("""
  80. UPDATE ir_model_fields
  81. SET serialization_field_id = NULL
  82. WHERE name = %s and model = %s
  83. """, (field_name, pool_obj._name))
  84. del pool_obj._columns[field_name]
  85. pool_obj.__init__(self.pool, cr)
  86. pool_obj._auto_init(cr, {'update_custom_fields': True})
  87. def unserialize_field(self, cr, uid, pool_obj, read_record,
  88. serialization_field_name, field_name,
  89. context=None):
  90. serialized_values = read_record[serialization_field_name]
  91. if not field_name in serialized_values:
  92. return False
  93. value = serialized_values.pop(field_name)
  94. if pool_obj._columns[field_name]._type in ('many2many', 'one2many'):
  95. value = [(6, 0, value)]
  96. return pool_obj.write(
  97. cr, uid, read_record['id'],
  98. {
  99. field_name: value,
  100. serialization_field_name: serialized_values,
  101. },
  102. context=context)