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.

116 lines
4.3 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Large Objects Attachment
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import base64
  23. import logging
  24. import mimetypes
  25. from odoo import api, models, _
  26. from odoo.exceptions import AccessError
  27. from odoo.addons.muk_fields_lobject.fields.lobject import LargeObject
  28. _logger = logging.getLogger(__name__)
  29. class LObjectIrAttachment(models.Model):
  30. _inherit = 'ir.attachment'
  31. #----------------------------------------------------------
  32. # Database
  33. #----------------------------------------------------------
  34. store_lobject = LargeObject(
  35. string="Data")
  36. #----------------------------------------------------------
  37. # Helper
  38. #----------------------------------------------------------
  39. @api.model
  40. def _get_datas_inital_vals(self):
  41. vals = super(LObjectIrAttachment, self)._get_datas_inital_vals()
  42. vals.update({'store_lobject': False})
  43. return vals
  44. #----------------------------------------------------------
  45. # Function
  46. #----------------------------------------------------------
  47. @api.model
  48. def storage_locations(self):
  49. locations = super(LObjectIrAttachment, self).storage_locations()
  50. locations.append('lobject')
  51. return locations
  52. @api.model
  53. def force_storage(self):
  54. if not self.env.user._is_admin():
  55. raise AccessError(_('Only administrators can execute this action.'))
  56. if self._storage() != 'lobject':
  57. return super(LObjectIrAttachment, self).force_storage()
  58. else:
  59. storage_domain = {
  60. 'lobject': ('store_lobject', '=', False),
  61. }
  62. record_domain = [
  63. '&', ('type', '=', 'binary'),
  64. '&', storage_domain[self._storage()],
  65. '|', ('res_field', '=', False), ('res_field', '!=', False)
  66. ]
  67. self.search(record_domain).migrate(batch_size=100)
  68. return True
  69. #----------------------------------------------------------
  70. # Read
  71. #----------------------------------------------------------
  72. @api.depends('store_lobject')
  73. def _compute_datas(self):
  74. bin_size = self._context.get('bin_size')
  75. for attach in self:
  76. if attach.store_lobject:
  77. if bin_size:
  78. attach.datas = attach.with_context({'human_size': True}).store_lobject
  79. else:
  80. attach.datas = attach.with_context({'base64': True}).store_lobject
  81. else:
  82. super(LObjectIrAttachment, attach)._compute_datas()
  83. #----------------------------------------------------------
  84. # Create, Write, Delete
  85. #----------------------------------------------------------
  86. @api.multi
  87. def _inverse_datas(self):
  88. location = self._storage()
  89. if location == 'lobject':
  90. for attach in self:
  91. value = attach.datas
  92. bin_data = base64.b64decode(value) if value else b''
  93. vals = self._get_datas_inital_vals()
  94. vals = self._update_datas_vals(vals, attach, bin_data)
  95. vals['store_lobject'] = bin_data
  96. clean_vals = self._get_datas_clean_vals(attach)
  97. models.Model.write(attach.sudo(), vals)
  98. self._clean_datas_after_write(clean_vals)
  99. else:
  100. super(LObjectIrAttachment, self)._inverse_datas()