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.

107 lines
3.5 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Converter
  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 os
  23. import base64
  24. import uuid
  25. import logging
  26. import mimetypes
  27. from odoo import _, api, fields, models
  28. # from odoo.addons.muk_utils.tools.http import get_response TODO
  29. #from odoo.addons.muk_converter.tools import converter
  30. _logger = logging.getLogger(__name__)
  31. class ConverterWizard(models.TransientModel):
  32. _name = "muk_converter.convert"
  33. #----------------------------------------------------------
  34. # Selections
  35. #----------------------------------------------------------
  36. def _format_selection(self):
  37. formats = self.env['muk_converter.converter'].formats()
  38. return list(map(lambda format: (format, format.upper()), formats))
  39. #----------------------------------------------------------
  40. # Database
  41. #----------------------------------------------------------
  42. state = fields.Selection(
  43. selection=[
  44. ("export", "Export"),
  45. ("download", "Download")],
  46. string="State",
  47. required=True,
  48. default="export")
  49. input_name = fields.Char(
  50. string="Filename",
  51. states={'export': [('required', True)]})
  52. input_binary = fields.Binary(
  53. string="File",
  54. attachment=False,
  55. states={'export': [('required', True)]})
  56. format = fields.Selection(
  57. selection=_format_selection,
  58. string="Format",
  59. default="pdf",
  60. states={'export': [('required', True)]})
  61. output_name = fields.Char(
  62. string="Filename",
  63. readonly=True,
  64. states={'download': [('required', True)]})
  65. output_binary = fields.Binary(
  66. string="File",
  67. readonly=True,
  68. attachment=False,
  69. states={'download': [('required', True)]})
  70. #----------------------------------------------------------
  71. # Functions
  72. #----------------------------------------------------------
  73. @api.multi
  74. def convert(self):
  75. self.ensure_one()
  76. name = "%s.%s" % (os.path.splitext(self.input_name)[0], self.format)
  77. output = self.env['muk_converter.converter'].convert(self.input_name, self.input_binary)
  78. self.write({
  79. 'state': 'download',
  80. 'output_name': name,
  81. 'output_binary': output})
  82. return {
  83. "name": _("Convert File"),
  84. 'type': 'ir.actions.act_window',
  85. 'res_model': 'muk_converter.convert',
  86. 'view_mode': 'form',
  87. 'view_type': 'form',
  88. 'res_id': self.id,
  89. 'views': [(False, 'form')],
  90. 'target': 'new',
  91. }