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.

62 lines
2.1 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. _logger = logging.getLogger(__name__)
  25. class Attachment(models.Model):
  26. _inherit = 'ir.attachment'
  27. #----------------------------------------------------------
  28. # Functions
  29. #----------------------------------------------------------
  30. @api.model
  31. def storage_locations(self):
  32. return ['db', 'file']
  33. @api.model
  34. def force_storage(self):
  35. if not self.env.user._is_admin():
  36. raise AccessError(_('Only administrators can execute this action.'))
  37. storage_domain = {
  38. 'db': ('db_datas', '=', False),
  39. 'file': ('store_fname', '=', False),
  40. }
  41. record_domain = [
  42. '&', storage_domain[self._storage()],
  43. '|', ('res_field', '=', False), ('res_field', '!=', False)
  44. ]
  45. self.search(record_domain).migrate()
  46. return True
  47. @api.multi
  48. def migrate(self):
  49. record_count = len(self)
  50. storage = self._storage().upper()
  51. for index, attach in enumerate(self):
  52. _logger.info(_("Migrate Attachment %s of %s to %s") % (index + 1, record_count, storage))
  53. attach.write({'datas': attach.datas})