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.

87 lines
2.7 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 openerp 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, %%s, 'db', %(field)s
  24. FROM %(table)s
  25. WHERE %(field)s IS NOT NULL
  26. """ % {"table": table, "field": field}
  27. image_field = 'file_db_store'
  28. # fields.Binary(attachment=True), get the ir_attachment record ID
  29. else:
  30. extract_query = """
  31. SELECT
  32. res_id,
  33. res_model,
  34. CONCAT_WS(',', res_model, res_id),
  35. 'filestore',
  36. id
  37. FROM ir_attachment
  38. WHERE res_field='%(field)s' AND res_model='%(model)s'
  39. """ % {"model": model, "field": field}
  40. image_field = 'attachment_id'
  41. cr.execute(
  42. """
  43. INSERT INTO base_multi_image_image (
  44. owner_id,
  45. owner_model,
  46. owner_ref_id,
  47. storage,
  48. %s
  49. )
  50. %s
  51. """ % (image_field, extract_query)
  52. )
  53. def uninstall_hook_for_submodules(cr, registry, model):
  54. """Remove multi-images for a given model.
  55. :param openerp.sql_db.Cursor cr:
  56. Database cursor.
  57. :param openerp.modules.registry.RegistryManager registry:
  58. Database registry, using v7 api.
  59. :param str model:
  60. Model technical name, like "res.partner". All multi-images for that
  61. model will be deleted
  62. """
  63. Image = registry["base_multi_image.image"]
  64. ids = Image.search(cr, SUPERUSER_ID, [("owner_model", "=", model)])
  65. Image.unlink(cr, SUPERUSER_ID, ids)
  66. def table_has_column(cr, table, field):
  67. query = """
  68. SELECT %(field)s
  69. FROM information_schema.columns
  70. WHERE table_name=%(table)s and column_name=%(field)s;
  71. """
  72. cr.execute(query, {'table': table, 'field': field})
  73. return bool(cr.fetchall())