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.

83 lines
2.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # -*- coding: utf-8 -*-
  2. ###################################################################################
  3. #
  4. # Copyright (C) 2018 MuK IT GmbH
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ###################################################################################
  20. import re
  21. import hashlib
  22. import logging
  23. import psycopg2
  24. import tempfile
  25. from odoo import _
  26. from odoo import models, api, fields
  27. from odoo.tools import ustr, pycompat, human_size
  28. _logger = logging.getLogger(__name__)
  29. class LargeObject(fields.Field):
  30. type = 'lobject'
  31. column_type = ('oid', 'oid')
  32. _slots = {
  33. 'prefetch': False,
  34. 'context_dependent': True,
  35. }
  36. def convert_to_column(self, value, record, values=None):
  37. if not value:
  38. return None
  39. lobject = record.env.cr._cnx.lobject(0, 'wb')
  40. if isinstance(value, bytes):
  41. lobject.write(value)
  42. else:
  43. while True:
  44. chunk = value.read(4096)
  45. if not chunk:
  46. break
  47. lobject.write(chunk)
  48. return lobject.oid
  49. def convert_to_record(self, value, record):
  50. if value:
  51. lobject = record.env.cr._cnx.lobject(value, 'rb')
  52. if record._context.get('human_size'):
  53. return human_size(lobject.seek(0, 2))
  54. elif record._context.get('bin_size'):
  55. return lobject.seek(0, 2)
  56. elif record._context.get('oid'):
  57. return lobject.oid
  58. elif record._context.get('stream'):
  59. file = tempfile.TemporaryFile()
  60. while True:
  61. chunk = lobject.read(4096)
  62. if not chunk:
  63. file.seek(0)
  64. return file
  65. file.write(chunk)
  66. else:
  67. return lobject.read()
  68. return False
  69. def convert_to_export(self, value, record):
  70. if value:
  71. lobject = record.env.cr._cnx.lobject(value, 'rb')
  72. if record._context.get('export_raw_data'):
  73. return lobject.read()
  74. return base64.b64encode(lobject.read())
  75. return ''