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.

90 lines
3.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. ## -*- coding: utf-8 -*-
  2. ###################################################################################
  3. #
  4. # MuK Document Management System
  5. #
  6. # Copyright (C) 2018 MuK IT GmbH
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (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 Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import base64
  23. import hashlib
  24. import itertools
  25. import logging
  26. import mimetypes
  27. import os
  28. import re
  29. from collections import defaultdict
  30. from odoo import api, fields, models, tools, SUPERUSER_ID, _
  31. from odoo.exceptions import AccessError, ValidationError
  32. from odoo.tools import config, human_size, ustr, html_escape
  33. from odoo.tools.mimetypes import guess_mimetype
  34. from odoo.addons.muk_fields_lobject import fields as lobject_fields
  35. _logger = logging.getLogger(__name__)
  36. class LObjectIrAttachment(models.Model):
  37. _inherit = 'ir.attachment'
  38. store_lobject = lobject_fields.LargeObject(
  39. string="Data")
  40. @api.model
  41. def force_storage(self):
  42. """Override force_storage without calling super,
  43. cause domain need to be edited."""
  44. if not self.env.user._is_admin():
  45. raise AccessError(_('Only administrators can execute this action.'))
  46. for attach in self.search(['|', ['res_field', '=', False], ['res_field', '!=', False]]):
  47. attach.write({'datas': attach.datas})
  48. return True
  49. @api.depends('store_fname', 'db_datas', 'store_lobject')
  50. def _compute_datas(self):
  51. bin_size = self._context.get('bin_size')
  52. for attach in self:
  53. if attach.store_lobject:
  54. if bin_size:
  55. attach.datas = attach.store_lobject
  56. else:
  57. attach.datas = base64.b64encode(attach.store_lobject)
  58. else:
  59. super(LObjectIrAttachment, attach)._compute_datas()
  60. def _inverse_datas(self):
  61. location = self._storage()
  62. for attach in self:
  63. if location == 'lobject':
  64. value = attach.datas
  65. bin_data = base64.b64decode(value) if value else b''
  66. vals = {
  67. 'file_size': len(bin_data),
  68. 'checksum': self._compute_checksum(bin_data),
  69. 'index_content': self._index(bin_data, attach.datas_fname, attach.mimetype),
  70. 'store_fname': False,
  71. 'db_datas': False,
  72. 'store_lobject': bin_data,
  73. }
  74. fname = attach.store_fname
  75. super(LObjectIrAttachment, attach.sudo()).write(vals)
  76. if fname:
  77. self._file_delete(fname)
  78. else:
  79. super(LObjectIrAttachment, attach)._inverse_datas()