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.

98 lines
3.5 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Large Objects Attachment
  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 base64
  23. import logging
  24. from odoo import api, models
  25. from odoo.addons.muk_fields_lobject.fields.lobject import LargeObject
  26. _logger = logging.getLogger(__name__)
  27. class LObjectIrAttachment(models.Model):
  28. _inherit = "ir.attachment"
  29. # ----------------------------------------------------------
  30. # Database
  31. # ----------------------------------------------------------
  32. store_lobject = LargeObject(string="Data")
  33. # ----------------------------------------------------------
  34. # Helper
  35. # ----------------------------------------------------------
  36. @api.model
  37. def _get_storage_domain(self, storage):
  38. if storage == "lobject":
  39. return [("store_lobject", "=", False)]
  40. return super(LObjectIrAttachment, self)._get_storage_domain(storage)
  41. # ----------------------------------------------------------
  42. # Function
  43. # ----------------------------------------------------------
  44. @api.model
  45. def storage_locations(self):
  46. locations = super(LObjectIrAttachment, self).storage_locations()
  47. locations.append("lobject")
  48. return locations
  49. # ----------------------------------------------------------
  50. # Read
  51. # ----------------------------------------------------------
  52. @api.depends("store_lobject")
  53. def _compute_datas(self):
  54. bin_size = self._context.get("bin_size")
  55. for attach in self:
  56. if attach.store_lobject:
  57. if bin_size:
  58. attach.datas = attach.with_context(
  59. {"human_size": True}
  60. ).store_lobject
  61. else:
  62. attach.datas = attach.with_context({"base64": True}).store_lobject
  63. else:
  64. super(LObjectIrAttachment, attach)._compute_datas()
  65. # ----------------------------------------------------------
  66. # Create, Write, Delete
  67. # ----------------------------------------------------------
  68. def _get_datas_related_values(self, data, mimetype):
  69. if self._storage() == "lobject":
  70. bin_data = base64.b64decode(data) if data else b""
  71. values = {
  72. "file_size": len(bin_data),
  73. "checksum": self._compute_checksum(bin_data),
  74. "index_content": self._index(bin_data, mimetype),
  75. "store_lobject": bin_data,
  76. "store_fname": False,
  77. "db_datas": False,
  78. }
  79. return values
  80. return super(LObjectIrAttachment, attach)._get_datas_related_values(
  81. data, mimetype
  82. )