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.

101 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. import logging
  22. from odoo import _, api, models
  23. from odoo.exceptions import AccessError
  24. from odoo.osv import expression
  25. _logger = logging.getLogger(__name__)
  26. class IrAttachment(models.Model):
  27. _inherit = "ir.attachment"
  28. # ----------------------------------------------------------
  29. # Helper
  30. # ----------------------------------------------------------
  31. @api.model
  32. def _get_storage_domain(self, storage):
  33. return {
  34. "db": [("db_datas", "=", False)],
  35. "file": [("store_fname", "=", False)],
  36. }[storage]
  37. # ----------------------------------------------------------
  38. # Actions
  39. # ----------------------------------------------------------
  40. def action_migrate(self):
  41. self.migrate()
  42. # ----------------------------------------------------------
  43. # Functions
  44. # ----------------------------------------------------------
  45. @api.model
  46. def storage_locations(self):
  47. return ["db", "file"]
  48. @api.model
  49. def force_storage(self):
  50. """Force all attachments to be stored in the currently configured storage"""
  51. if not self.env.user._is_admin():
  52. raise AccessError(_("Only administrators can execute this action."))
  53. self.search(
  54. expression.AND(
  55. [
  56. self._get_storage_domain(self._storage()),
  57. [
  58. "&",
  59. "|",
  60. ("res_field", "=", False),
  61. ("res_field", "!=", False),
  62. ("type", "=", "binary"),
  63. ],
  64. ]
  65. )
  66. ).migrate(batch_size=100)
  67. return True
  68. def migrate(self, batch_size=None):
  69. commit_on_batch = bool(batch_size)
  70. attachments_to_migrate = len(self)
  71. batch_size = batch_size or len(self) or 1
  72. storage_location = self._storage().upper()
  73. for index, attachment in enumerate(self, start=1):
  74. _logger.info(
  75. "Migrate Attachment {index} of {total} to {storage}".format(
  76. **{
  77. "index": index,
  78. "total": attachments_to_migrate,
  79. "storage": storage_location,
  80. }
  81. )
  82. )
  83. attachment.write(
  84. {"datas": attachment.datas, "mimetype": attachment.mimetype}
  85. )
  86. if commit_on_batch and not index % batch_size:
  87. self.env.cr.commit()