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
3.1 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import base64
  20. import logging
  21. import mimetypes
  22. from odoo import api, models, _
  23. from odoo.exceptions import AccessError
  24. from odoo.addons.muk_fields_lobject.fields.lobject import LargeObject
  25. _logger = logging.getLogger(__name__)
  26. class LObjectIrAttachment(models.Model):
  27. _inherit = 'ir.attachment'
  28. store_lobject = LargeObject(
  29. string="Data")
  30. @api.model
  31. def force_storage(self):
  32. if not self.env.user._is_admin():
  33. raise AccessError(_('Only administrators can execute this action.'))
  34. storage_domain = {
  35. 'db': ('store_lobject', '=', False),
  36. 'file': ('store_fname', '=', False),
  37. }
  38. record_domain = [
  39. '&', storage_domain[self._storage()],
  40. '|', ('res_field', '=', False), ('res_field', '!=', False)
  41. ]
  42. self.search(record_domain).migrate()
  43. return True
  44. @api.depends('store_lobject')
  45. def _compute_datas(self):
  46. bin_size = self._context.get('bin_size')
  47. for attach in self:
  48. if attach.store_lobject:
  49. if bin_size:
  50. attach.datas = attach.store_lobject
  51. else:
  52. attach.datas = attach.with_context({'base64': True}).store_lobject
  53. else:
  54. super(LObjectIrAttachment, attach)._compute_datas()
  55. def _inverse_datas(self):
  56. location = self._storage()
  57. for attach in self:
  58. if location == 'lobject':
  59. value = attach.datas
  60. bin_data = base64.b64decode(value) if value else b''
  61. vals = {
  62. 'file_size': len(bin_data),
  63. 'checksum': self._compute_checksum(bin_data),
  64. 'index_content': self._index(bin_data, attach.datas_fname, attach.mimetype),
  65. 'store_fname': False,
  66. 'db_datas': False,
  67. 'store_lobject': bin_data,
  68. }
  69. fname = attach.store_fname
  70. super(LObjectIrAttachment, attach.sudo()).write(vals)
  71. if fname:
  72. self._file_delete(fname)
  73. else:
  74. super(LObjectIrAttachment, attach)._inverse_datas()