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.

117 lines
3.8 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.exceptions import UserError
  26. from odoo.addons.muk_utils.tools.http import get_response
  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_url = fields.Char(
  50. string="URL")
  51. input_binary = fields.Binary(
  52. string="File")
  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. states={'download': [('required', True)]})
  66. #----------------------------------------------------------
  67. # Functions
  68. #----------------------------------------------------------
  69. @api.multi
  70. def convert(self):
  71. self.ensure_one()
  72. if not self.input_url and not self.input_binary:
  73. raise UserError(_("Please choose a file to convert."))
  74. if self.input_url:
  75. status, headers, content = get_response(self.input_url)
  76. if status != 200:
  77. raise ValueError(_("Failed to retrieve the file from the url."))
  78. else:
  79. content = base64.b64encode(content)
  80. else:
  81. content = self.input_binary
  82. name = "%s.%s" % (os.path.splitext(self.input_name)[0], self.format)
  83. output = self.env['muk_converter.converter'].convert(self.input_name, content, format=self.format)
  84. self.write({
  85. 'state': 'download',
  86. 'output_name': name,
  87. 'output_binary': output})
  88. return {
  89. "name": _("Convert File"),
  90. 'type': 'ir.actions.act_window',
  91. 'res_model': 'muk_converter.convert',
  92. 'view_mode': 'form',
  93. 'view_type': 'form',
  94. 'res_id': self.id,
  95. 'views': [(False, 'form')],
  96. 'target': 'new',
  97. }