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.

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