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.

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