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.

65 lines
2.0 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. # 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. # Extract the binary content from the ir_attachment table
  29. else:
  30. extract_query = """
  31. SELECT res_id, res_model, 'filestore', id
  32. FROM ir_attachment
  33. WHERE res_field='%(field)s' AND res_model='%(model)s'
  34. """ % {"model": model, "field": field}
  35. image_field = 'attachment_id'
  36. cr.execute(extract_query)
  37. cr.execute(
  38. """
  39. INSERT INTO base_multi_image_image (
  40. owner_id,
  41. owner_model,
  42. storage,
  43. %s
  44. )
  45. %s
  46. """ % (image_field, extract_query)
  47. )
  48. def table_has_column(cr, table, field):
  49. query = """
  50. SELECT %(field)s
  51. FROM information_schema.columns
  52. WHERE table_name=%(table)s and column_name=%(field)s;
  53. """
  54. cr.execute(query, {'table': table, 'field': field})
  55. return bool(cr.fetchall())