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.

121 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.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. #----------------------------------------------------------
  46. # Actions
  47. #----------------------------------------------------------
  48. @api.multi
  49. def action_migrate(self):
  50. self.migrate()
  51. #----------------------------------------------------------
  52. # Functions
  53. #----------------------------------------------------------
  54. @api.model
  55. def storage_locations(self):
  56. return ['db', 'file']
  57. @api.model
  58. def force_storage(self):
  59. if not self.env.user._is_admin():
  60. raise AccessError(_('Only administrators can execute this action.'))
  61. storage_domain = {
  62. 'db': ('db_datas', '=', False),
  63. 'file': ('store_fname', '=', False),
  64. }
  65. record_domain = [
  66. '&', storage_domain[self._storage()],
  67. '|', ('res_field', '=', False), ('res_field', '!=', False)
  68. ]
  69. self.search(record_domain).migrate()
  70. return True
  71. @api.multi
  72. def migrate(self):
  73. record_count = len(self)
  74. storage = self._storage().upper()
  75. for index, attach in enumerate(self):
  76. _logger.info(_("Migrate Attachment %s of %s to %s") % (index + 1, record_count, storage))
  77. attach.with_context(migration=True).write({'datas': attach.datas})
  78. #----------------------------------------------------------
  79. # Read
  80. #----------------------------------------------------------
  81. def _compute_mimetype(self, values):
  82. if self.env.context.get('migration') and len(self) == 1:
  83. return self.mimetype or 'application/octet-stream'
  84. else:
  85. return super(IrAttachment, self)._compute_mimetype(values)
  86. #----------------------------------------------------------
  87. # Create, Write, Delete
  88. #----------------------------------------------------------
  89. def _inverse_datas(self):
  90. location = self._storage()
  91. for attach in self:
  92. value = attach.datas
  93. bin_data = base64.b64decode(value) if value else b''
  94. vals = self._get_datas_inital_vals()
  95. vals = self._update_datas_vals(vals, attach, bin_data)
  96. if value and location != 'db':
  97. vals['store_fname'] = self._file_write(value, vals['checksum'])
  98. else:
  99. vals['db_datas'] = value
  100. fname = attach.store_fname
  101. super(IrAttachment, attach.sudo()).write(vals)
  102. if fname:
  103. self._file_delete(fname)