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.

91 lines
3.4 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 storage_locations(self):
  32. locations = super(LObjectIrAttachment, self).storage_locations()
  33. locations.append('lobject')
  34. return locations
  35. @api.model
  36. def force_storage(self):
  37. if not self.env.user._is_admin():
  38. raise AccessError(_('Only administrators can execute this action.'))
  39. if self._storage() != 'lobject':
  40. return super(LObjectIrAttachment, self).force_storage()
  41. else:
  42. storage_domain = {
  43. 'lobject': ('store_lobject', '=', False),
  44. }
  45. record_domain = [
  46. '&', storage_domain[self._storage()],
  47. '|', ('res_field', '=', False), ('res_field', '!=', False)
  48. ]
  49. self.search(record_domain).migrate()
  50. return True
  51. @api.depends('store_lobject')
  52. def _compute_datas(self):
  53. bin_size = self._context.get('bin_size')
  54. for attach in self:
  55. if attach.store_lobject:
  56. if bin_size:
  57. attach.datas = attach.with_context({'human_size': True}).store_lobject
  58. else:
  59. attach.datas = attach.with_context({'base64': True}).store_lobject
  60. else:
  61. super(LObjectIrAttachment, attach)._compute_datas()
  62. def _inverse_datas(self):
  63. location = self._storage()
  64. for attach in self:
  65. if location == 'lobject':
  66. value = attach.datas
  67. bin_data = base64.b64decode(value) if value else b''
  68. vals = {
  69. 'file_size': len(bin_data),
  70. 'checksum': self._compute_checksum(bin_data),
  71. 'index_content': self._index(bin_data, attach.datas_fname, attach.mimetype),
  72. 'store_fname': False,
  73. 'db_datas': False,
  74. 'store_lobject': bin_data,
  75. }
  76. fname = attach.store_fname
  77. super(LObjectIrAttachment, attach.sudo()).write(vals)
  78. if fname:
  79. self._file_delete(fname)
  80. else:
  81. super(LObjectIrAttachment, attach)._inverse_datas()