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.

104 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. attachment=False,
  52. states={'export': [('required', True)]})
  53. format = fields.Selection(
  54. selection=_format_selection,
  55. string="Format",
  56. default="pdf",
  57. states={'export': [('required', True)]})
  58. output_name = fields.Char(
  59. string="Filename",
  60. readonly=True,
  61. states={'download': [('required', True)]})
  62. output_binary = fields.Binary(
  63. string="File",
  64. readonly=True,
  65. attachment=False,
  66. states={'download': [('required', True)]})
  67. #----------------------------------------------------------
  68. # Functions
  69. #----------------------------------------------------------
  70. @api.multi
  71. def convert(self):
  72. self.ensure_one()
  73. name = "%s.%s" % (os.path.splitext(self.input_name)[0], self.format)
  74. output = self.env['muk_converter.converter'].convert(self.input_name, self.input_binary)
  75. self.write({
  76. 'state': 'download',
  77. 'output_name': name,
  78. 'output_binary': output})
  79. return {
  80. "name": _("Convert File"),
  81. 'type': 'ir.actions.act_window',
  82. 'res_model': 'muk_converter.convert',
  83. 'view_mode': 'form',
  84. 'view_type': 'form',
  85. 'res_id': self.id,
  86. 'views': [(False, 'form')],
  87. 'target': 'new',
  88. }