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.

113 lines
4.2 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. #----------------------------------------------------------
  29. # Database
  30. #----------------------------------------------------------
  31. store_lobject = LargeObject(
  32. string="Data")
  33. #----------------------------------------------------------
  34. # Helper
  35. #----------------------------------------------------------
  36. @api.model
  37. def _get_datas_inital_vals(self):
  38. vals = super(LObjectIrAttachment, self)._get_datas_inital_vals()
  39. vals.update({'store_lobject': False})
  40. return vals
  41. #----------------------------------------------------------
  42. # Function
  43. #----------------------------------------------------------
  44. @api.model
  45. def storage_locations(self):
  46. locations = super(LObjectIrAttachment, self).storage_locations()
  47. locations.append('lobject')
  48. return locations
  49. @api.model
  50. def force_storage(self):
  51. if not self.env.user._is_admin():
  52. raise AccessError(_('Only administrators can execute this action.'))
  53. if self._storage() != 'lobject':
  54. return super(LObjectIrAttachment, self).force_storage()
  55. else:
  56. storage_domain = {
  57. 'lobject': ('store_lobject', '=', False),
  58. }
  59. record_domain = [
  60. '&', ('type', '=', 'binary'),
  61. '&', storage_domain[self._storage()],
  62. '|', ('res_field', '=', False), ('res_field', '!=', False)
  63. ]
  64. self.search(record_domain).migrate()
  65. return True
  66. #----------------------------------------------------------
  67. # Read
  68. #----------------------------------------------------------
  69. @api.depends('store_lobject')
  70. def _compute_datas(self):
  71. bin_size = self._context.get('bin_size')
  72. for attach in self:
  73. if attach.store_lobject:
  74. if bin_size:
  75. attach.datas = attach.with_context({'human_size': True}).store_lobject
  76. else:
  77. attach.datas = attach.with_context({'base64': True}).store_lobject
  78. else:
  79. super(LObjectIrAttachment, attach)._compute_datas()
  80. #----------------------------------------------------------
  81. # Create, Write, Delete
  82. #----------------------------------------------------------
  83. @api.multi
  84. def _inverse_datas(self):
  85. location = self._storage()
  86. for attach in self:
  87. if location == 'lobject':
  88. value = attach.datas
  89. bin_data = base64.b64decode(value) if value else b''
  90. vals = self._get_datas_inital_vals()
  91. vals = self._update_datas_vals(vals, attach, bin_data)
  92. vals['store_lobject'] = bin_data
  93. clean_vals = self._get_datas_clean_vals(attach)
  94. models.Model.write(attach.sudo(), vals)
  95. self._clean_datas_after_write(clean_vals)
  96. else:
  97. super(LObjectIrAttachment, attach)._inverse_datas()