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.

103 lines
3.5 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Utils
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import logging
  23. from odoo import _, api, models
  24. from odoo.exceptions import AccessError
  25. from odoo.osv import expression
  26. _logger = logging.getLogger(__name__)
  27. class IrAttachment(models.Model):
  28. _inherit = "ir.attachment"
  29. # ----------------------------------------------------------
  30. # Helper
  31. # ----------------------------------------------------------
  32. @api.model
  33. def _get_storage_domain(self, storage):
  34. return {
  35. "db": [("db_datas", "=", False)],
  36. "file": [("store_fname", "=", False)],
  37. }[storage]
  38. # ----------------------------------------------------------
  39. # Actions
  40. # ----------------------------------------------------------
  41. def action_migrate(self):
  42. self.migrate()
  43. # ----------------------------------------------------------
  44. # Functions
  45. # ----------------------------------------------------------
  46. @api.model
  47. def storage_locations(self):
  48. return ["db", "file"]
  49. @api.model
  50. def force_storage(self):
  51. if not self._storage() in self.storage_locations():
  52. return super(IrAttachment, self).force_storage()
  53. if not self.env.user._is_admin():
  54. raise AccessError(_("Only administrators can execute this action."))
  55. self.search(
  56. expression.AND(
  57. [
  58. self._get_storage_domain(self._storage()),
  59. [
  60. "&",
  61. "|",
  62. ("res_field", "=", False),
  63. ("res_field", "!=", False),
  64. ("type", "=", "binary"),
  65. ],
  66. ]
  67. )
  68. ).migrate(batch_size=100)
  69. return True
  70. def migrate(self, batch_size=None):
  71. commit_on_batch = bool(batch_size)
  72. attachments_to_migrate = len(self)
  73. batch_size = batch_size or len(self) or 1
  74. storage_location = self._storage().upper()
  75. for index, attachment in enumerate(self, start=1):
  76. _logger.info(
  77. "Migrate Attachment {index} of {total} to {storage}".format(
  78. **{
  79. "index": index,
  80. "total": attachments_to_migrate,
  81. "storage": storage_location,
  82. }
  83. )
  84. )
  85. attachment.write(
  86. {"datas": attachment.datas, "mimetype": attachment.mimetype}
  87. )
  88. if commit_on_batch and not index % batch_size:
  89. self.env.cr.commit()