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.

73 lines
2.6 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 Attachment(models.Model):
  27. _inherit = 'ir.attachment'
  28. #----------------------------------------------------------
  29. # Functions
  30. #----------------------------------------------------------
  31. @api.model
  32. def storage_locations(self):
  33. return ['db', 'file']
  34. @api.model
  35. def force_storage(self):
  36. if not self.env.user._is_admin():
  37. raise AccessError(_('Only administrators can execute this action.'))
  38. storage_domain = {
  39. 'db': ('db_datas', '=', False),
  40. 'file': ('store_fname', '=', False),
  41. }
  42. record_domain = [
  43. '&', storage_domain[self._storage()],
  44. '|', ('res_field', '=', False), ('res_field', '!=', False)
  45. ]
  46. self.search(record_domain).migrate()
  47. return True
  48. @api.multi
  49. def migrate(self):
  50. record_count = len(self)
  51. storage = self._storage().upper()
  52. for index, attach in enumerate(self):
  53. _logger.info(_("Migrate Attachment %s of %s to %s") % (index + 1, record_count, storage))
  54. attach.with_context(migration=True).write({'datas': attach.datas})
  55. #----------------------------------------------------------
  56. # Read
  57. #----------------------------------------------------------
  58. def _compute_mimetype(self, values):
  59. if self.env.context.get('migration') and len(self) == 1:
  60. return self.mimetype or 'application/octet-stream'
  61. else:
  62. return super(Attachment, self)._compute_mimetype(values)