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.

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