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.

91 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Antiun Ingeniería S.L. - Jairo Llopis
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, SUPERUSER_ID
  5. import logging
  6. _logger = logging.getLogger(__name__)
  7. def pre_init_hook_for_submodules(cr, model, field):
  8. """Moves images from single to multi mode.
  9. Feel free to use this as a ``pre_init_hook`` for submodules.
  10. :param str model:
  11. Model name, like ``product.template``.
  12. :param str field:
  13. Binary field that had the images in that :param:`model`, like
  14. ``image``.
  15. """
  16. env = api.Environment(cr, SUPERUSER_ID, dict())
  17. with cr.savepoint():
  18. table = env[model]._table
  19. column_exists = table_has_column(cr, table, field)
  20. # fields.Binary(), extract the binary content directly from the table
  21. if column_exists:
  22. extract_query = """
  23. SELECT id, '%(model)s', '%(model)s,' || id, 'db', %(field)s
  24. FROM %(table)s
  25. WHERE %(field)s IS NOT NULL
  26. """ % {
  27. "table": table,
  28. "field": field,
  29. "model": model,
  30. }
  31. image_field = 'file_db_store'
  32. # fields.Binary(attachment=True), get the ir_attachment record ID
  33. else:
  34. extract_query = """
  35. SELECT
  36. res_id,
  37. res_model,
  38. CONCAT_WS(',', res_model, res_id),
  39. 'filestore',
  40. id
  41. FROM ir_attachment
  42. WHERE res_field='%(field)s' AND res_model='%(model)s'
  43. """ % {"model": model, "field": field}
  44. image_field = 'attachment_id'
  45. cr.execute(
  46. """
  47. INSERT INTO base_multi_image_image (
  48. owner_id,
  49. owner_model,
  50. owner_ref_id,
  51. storage,
  52. %s
  53. )
  54. %s
  55. """ % (image_field, extract_query)
  56. )
  57. def uninstall_hook_for_submodules(cr, registry, model):
  58. """Remove multi-images for a given model.
  59. :param odoo.sql_db.Cursor cr:
  60. Database cursor.
  61. :param odoo.modules.registry.RegistryManager registry:
  62. Database registry, using v7 api.
  63. :param str model:
  64. Model technical name, like "res.partner". All multi-images for that
  65. model will be deleted
  66. """
  67. Image = registry["base_multi_image.image"]
  68. ids = Image.search(cr, SUPERUSER_ID, [("owner_model", "=", model)])
  69. Image.unlink(cr, SUPERUSER_ID, ids)
  70. def table_has_column(cr, table, field):
  71. query = """
  72. SELECT %(field)s
  73. FROM information_schema.columns
  74. WHERE table_name=%(table)s and column_name=%(field)s;
  75. """
  76. cr.execute(query, {'table': table, 'field': field})
  77. return bool(cr.fetchall())