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 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import os
  20. import base64
  21. import uuid
  22. import logging
  23. import mimetypes
  24. from odoo import _, api, fields, models
  25. # from odoo.addons.muk_utils.tools.http import get_response TODO
  26. #from odoo.addons.muk_converter.tools import converter
  27. _logger = logging.getLogger(__name__)
  28. class ConverterWizard(models.TransientModel):
  29. _name = "muk_converter.convert"
  30. #----------------------------------------------------------
  31. # Selections
  32. #----------------------------------------------------------
  33. def _format_selection(self):
  34. formats = self.env['muk_converter.converter'].formats()
  35. return list(map(lambda format: (format, format.upper()), formats))
  36. #----------------------------------------------------------
  37. # Database
  38. #----------------------------------------------------------
  39. state = fields.Selection(
  40. selection=[
  41. ("export", "Export"),
  42. ("download", "Download")],
  43. string="State",
  44. required=True,
  45. default="export")
  46. input_name = fields.Char(
  47. string="Filename",
  48. states={'export': [('required', True)]})
  49. input_binary = fields.Binary(
  50. string="File",
  51. states={'export': [('required', True)]})
  52. format = fields.Selection(
  53. selection=_format_selection,
  54. string="Format",
  55. default="pdf",
  56. states={'export': [('required', True)]})
  57. output_name = fields.Char(
  58. string="Filename",
  59. readonly=True,
  60. states={'download': [('required', True)]})
  61. output_binary = fields.Binary(
  62. string="File",
  63. readonly=True,
  64. states={'download': [('required', True)]})
  65. #----------------------------------------------------------
  66. # Functions
  67. #----------------------------------------------------------
  68. @api.multi
  69. def convert(self):
  70. self.ensure_one()
  71. name = "%s.%s" % (os.path.splitext(self.input_name)[0], self.format)
  72. output = self.env['muk_converter.converter'].convert(self.input_name, self.input_binary)
  73. self.write({
  74. 'state': 'download',
  75. 'output_name': name,
  76. 'output_binary': output})
  77. return {
  78. "name": _("Convert File"),
  79. 'type': 'ir.actions.act_window',
  80. 'res_model': 'muk_converter.convert',
  81. 'view_mode': 'form',
  82. 'view_type': 'form',
  83. 'res_id': self.id,
  84. 'views': [(False, 'form')],
  85. 'target': 'new',
  86. }