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.

63 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. # 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 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(
  37. """
  38. INSERT INTO base_multi_image_image (
  39. owner_id,
  40. owner_model,
  41. storage,
  42. %s
  43. )
  44. %s
  45. """ % (image_field, extract_query)
  46. )
  47. def table_has_column(cr, table, field):
  48. query = """
  49. SELECT %(field)s
  50. FROM information_schema.columns
  51. WHERE table_name=%(table)s and column_name=%(field)s;
  52. """
  53. cr.execute(query, {'table': table, 'field': field})
  54. return bool(cr.fetchall())