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.

57 lines
1.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017-2018 Onestein (<https://www.onestein.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. import logging
  5. import os
  6. from odoo import api, fields, models, _
  7. from odoo.exceptions import UserError
  8. from odoo.tools import human_size
  9. _logger = logging.getLogger(__name__)
  10. class IrFilesystemDirectoryLine(models.TransientModel):
  11. _name = 'ir.filesystem.file'
  12. name = fields.Char(required=True)
  13. filename = fields.Char()
  14. file_content = fields.Binary(compute='_compute_file')
  15. stored_filename = fields.Char()
  16. directory_id = fields.Many2one(
  17. 'ir.filesystem.directory',
  18. string='Directory',
  19. )
  20. @api.multi
  21. def _file_read(self, fname, bin_size=False):
  22. def file_not_found(fname):
  23. raise UserError(_(
  24. '''Error while reading file %s.
  25. Maybe it was removed or permission is changed.
  26. Please refresh the list.''' % fname))
  27. self.ensure_one()
  28. r = ''
  29. directory = self.directory_id.get_dir()
  30. full_path = directory + fname
  31. if not (directory and os.path.isfile(full_path)):
  32. file_not_found(fname)
  33. try:
  34. if bin_size:
  35. r = human_size(os.path.getsize(full_path))
  36. else:
  37. r = open(full_path, 'rb').read().encode('base64')
  38. except (IOError, OSError):
  39. _logger.info("_read_file reading %s", fname, exc_info=True)
  40. return r
  41. @api.depends('stored_filename')
  42. def _compute_file(self):
  43. bin_size = self._context.get('bin_size')
  44. for line in self:
  45. if line.stored_filename:
  46. content = line._file_read(line.stored_filename, bin_size)
  47. line.file_content = content