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.

92 lines
3.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. ###################################################################################
  2. #
  3. # MuK Document Management System
  4. #
  5. # Copyright (C) 2018 MuK IT GmbH
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ###################################################################################
  21. import base64
  22. import logging
  23. import mimetypes
  24. from odoo import api, models, _
  25. from odoo.exceptions import AccessError
  26. from odoo.addons.muk_fields_lobject.fields import LargeObject
  27. _logger = logging.getLogger(__name__)
  28. class LObjectIrAttachment(models.Model):
  29. _inherit = 'ir.attachment'
  30. store_lobject = LargeObject(
  31. string="Data")
  32. @api.model
  33. def force_storage(self):
  34. if not self.env.user._is_admin():
  35. raise AccessError(_('Only administrators can execute this action.'))
  36. attachments = self.search(['|', ['res_field', '=', False], ['res_field', '!=', False]])
  37. for index, attach in enumerate(attachments):
  38. _logger.info(_("Migrate Attachment %s of %s") % (index, len(attachments)))
  39. attach.write({'datas': attach.datas})
  40. return True
  41. @api.depends('store_fname', 'db_datas', 'store_lobject')
  42. def _compute_datas(self):
  43. bin_size = self._context.get('bin_size')
  44. for attach in self:
  45. if attach.store_lobject:
  46. if bin_size:
  47. attach.datas = attach.store_lobject
  48. else:
  49. attach.datas = attach.with_context({'base64': True}).store_lobject
  50. else:
  51. super(LObjectIrAttachment, attach)._compute_datas()
  52. def _inverse_datas(self):
  53. location = self._storage()
  54. for attach in self:
  55. if location == 'lobject':
  56. value = attach.datas
  57. bin_data = base64.b64decode(value) if value else b''
  58. vals = {
  59. 'file_size': len(bin_data),
  60. 'checksum': self._compute_checksum(bin_data),
  61. 'index_content': self._index(bin_data, attach.datas_fname, attach.mimetype),
  62. 'store_fname': False,
  63. 'db_datas': False,
  64. 'store_lobject': bin_data,
  65. }
  66. fname = attach.store_fname
  67. super(LObjectIrAttachment, attach.sudo()).write(vals)
  68. if fname:
  69. self._file_delete(fname)
  70. else:
  71. super(LObjectIrAttachment, attach)._inverse_datas()
  72. def _compute_mimetype(self, values):
  73. mimetype = super(LObjectIrAttachment, self)._compute_mimetype(values)
  74. if not mimetype or mimetype == 'application/octet-stream':
  75. mimetype = None
  76. for attach in self:
  77. if attach.mimetype:
  78. mimetype = attach.mimetype
  79. if not mimetype and attach.datas_fname:
  80. mimetype = mimetypes.guess_type(attach.datas_fname)[0]
  81. return mimetype or 'application/octet-stream'