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.

86 lines
2.9 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
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. oid = record.with_context({'oid': True})[self.name]
  38. if oid:
  39. record.env.cr._cnx.lobject(oid, 'rb').unlink()
  40. if not value:
  41. return None
  42. lobject = record.env.cr._cnx.lobject(0, 'wb')
  43. if isinstance(value, bytes):
  44. lobject.write(value)
  45. else:
  46. while True:
  47. chunk = value.read(4096)
  48. if not chunk:
  49. break
  50. lobject.write(chunk)
  51. return lobject.oid
  52. def convert_to_record(self, value, record):
  53. if value:
  54. lobject = record.env.cr._cnx.lobject(value, 'rb')
  55. if record._context.get('human_size'):
  56. return human_size(lobject.seek(0, 2))
  57. elif record._context.get('bin_size'):
  58. return lobject.seek(0, 2)
  59. elif record._context.get('oid'):
  60. return lobject.oid
  61. elif record._context.get('stream'):
  62. file = tempfile.TemporaryFile()
  63. while True:
  64. chunk = lobject.read(4096)
  65. if not chunk:
  66. file.seek(0)
  67. return file
  68. file.write(chunk)
  69. else:
  70. return lobject.read()
  71. return False
  72. def convert_to_export(self, value, record):
  73. if value:
  74. lobject = record.env.cr._cnx.lobject(value, 'rb')
  75. if record._context.get('export_raw_data'):
  76. return lobject.read()
  77. return base64.b64encode(lobject.read())
  78. return ''