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.

102 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. """Force all attachments to be stored in the currently configured storage"""
  52. if not self.env.user._is_admin():
  53. raise AccessError(_("Only administrators can execute this action."))
  54. self.search(
  55. expression.AND(
  56. [
  57. self._get_storage_domain(self._storage()),
  58. [
  59. "&",
  60. "|",
  61. ("res_field", "=", False),
  62. ("res_field", "!=", False),
  63. ("type", "=", "binary"),
  64. ],
  65. ]
  66. )
  67. ).migrate(batch_size=100)
  68. return True
  69. def migrate(self, batch_size=None):
  70. commit_on_batch = bool(batch_size)
  71. attachments_to_migrate = len(self)
  72. batch_size = batch_size or len(self) or 1
  73. storage_location = self._storage().upper()
  74. for index, attachment in enumerate(self, start=1):
  75. _logger.info(
  76. "Migrate Attachment {index} of {total} to {storage}".format(
  77. **{
  78. "index": index,
  79. "total": attachments_to_migrate,
  80. "storage": storage_location,
  81. }
  82. )
  83. )
  84. attachment.write(
  85. {"datas": attachment.datas, "mimetype": attachment.mimetype}
  86. )
  87. if commit_on_batch and not index % batch_size:
  88. self.env.cr.commit()