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.

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