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.

135 lines
4.7 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.tools.mimetypes import guess_mimetype
  25. _logger = logging.getLogger(__name__)
  26. class IrAttachment(models.Model):
  27. _inherit = 'ir.attachment'
  28. #----------------------------------------------------------
  29. # Helper
  30. #----------------------------------------------------------
  31. @api.model
  32. def _get_datas_inital_vals(self):
  33. return {
  34. 'store_fname': False,
  35. 'db_datas': False,
  36. }
  37. @api.model
  38. def _update_datas_vals(self, vals, attach, bin_data):
  39. vals.update({
  40. 'file_size': len(bin_data),
  41. 'checksum': self._compute_checksum(bin_data),
  42. 'index_content': self._index(bin_data, attach.datas_fname, attach.mimetype),
  43. })
  44. return vals
  45. @api.model
  46. def _get_datas_clean_vals(self, attach):
  47. vals = {}
  48. if attach.store_fname:
  49. vals['store_fname'] = attach.store_fname
  50. return vals
  51. @api.model
  52. def _clean_datas_after_write(self, vals):
  53. if 'store_fname' in vals:
  54. self._file_delete(vals['store_fname'])
  55. #----------------------------------------------------------
  56. # Actions
  57. #----------------------------------------------------------
  58. @api.multi
  59. def action_migrate(self):
  60. self.migrate()
  61. #----------------------------------------------------------
  62. # Functions
  63. #----------------------------------------------------------
  64. @api.model
  65. def storage_locations(self):
  66. return ['db', 'file']
  67. @api.model
  68. def force_storage(self):
  69. if not self.env.user._is_admin():
  70. raise AccessError(_('Only administrators can execute this action.'))
  71. storage_domain = {
  72. 'db': ('db_datas', '=', False),
  73. 'file': ('store_fname', '=', False),
  74. }
  75. record_domain = [
  76. '&', ('type', '=', 'binary'),
  77. '&', storage_domain[self._storage()],
  78. '|', ('res_field', '=', False), ('res_field', '!=', False)
  79. ]
  80. self.search(record_domain).migrate()
  81. return True
  82. @api.multi
  83. def migrate(self):
  84. record_count = len(self)
  85. storage = self._storage().upper()
  86. for index, attach in enumerate(self):
  87. _logger.info(_("Migrate Attachment %s of %s to %s") % (index + 1, record_count, storage))
  88. attach.with_context(migration=True).write({'datas': attach.datas})
  89. #----------------------------------------------------------
  90. # Read
  91. #----------------------------------------------------------
  92. @api.multi
  93. def _compute_mimetype(self, values):
  94. if self.env.context.get('migration') and len(self) == 1:
  95. return self.mimetype or 'application/octet-stream'
  96. else:
  97. return super(IrAttachment, self)._compute_mimetype(values)
  98. #----------------------------------------------------------
  99. # Create, Write, Delete
  100. #----------------------------------------------------------
  101. @api.multi
  102. def _inverse_datas(self):
  103. location = self._storage()
  104. for attach in self:
  105. value = attach.datas
  106. bin_data = base64.b64decode(value) if value else b''
  107. vals = self._get_datas_inital_vals()
  108. vals = self._update_datas_vals(vals, attach, bin_data)
  109. if value and location != 'db':
  110. vals['store_fname'] = self._file_write(value, vals['checksum'])
  111. else:
  112. vals['db_datas'] = value
  113. clean_vals = self._get_datas_clean_vals(attach)
  114. super(IrAttachment, attach.sudo()).write(vals)
  115. self._clean_datas_after_write(clean_vals)