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.

108 lines
4.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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
  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. state = fields.Selection(
  31. selection=[("export", "Export"), ("download", "Download")],
  32. string="State",
  33. required=True,
  34. default="export")
  35. type = fields.Selection(
  36. selection=[("url", "URL"), ("binary", "File")],
  37. string="Type",
  38. default="binary",
  39. change_default=True,
  40. states={'export': [('required', True)]},
  41. help="Either a binary file or an url can be converted")
  42. input_url = fields.Char(
  43. string="Url")
  44. input_name = fields.Char(
  45. string="Filename")
  46. input_binary = fields.Binary(
  47. string="File")
  48. format = fields.Selection(
  49. selection=converter.selection_formats(),
  50. string="Format",
  51. default="pdf",
  52. states={'export': [('required', True)]})
  53. output_name = fields.Char(
  54. string="Filename",
  55. readonly=True,
  56. states={'download': [('required', True)]})
  57. output_binary = fields.Binary(
  58. string="File",
  59. readonly=True,
  60. states={'download': [('required', True)]})
  61. @api.multi
  62. def convert(self):
  63. def export(record, content, filename):
  64. name = "%s.%s" % (os.path.splitext(filename)[0], record.format)
  65. output = record.env['muk_converter.converter'].convert(filename, content)
  66. record.write({
  67. 'state': 'download',
  68. 'output_name': name,
  69. 'output_binary': base64.b64encode(output)})
  70. return {
  71. "name": _("Convert File"),
  72. 'type': 'ir.actions.act_window',
  73. 'res_model': 'muk_converter.convert',
  74. 'view_mode': 'form',
  75. 'view_type': 'form',
  76. 'res_id': record.id,
  77. 'views': [(False, 'form')],
  78. 'target': 'new',
  79. }
  80. record = self[0]
  81. if record.input_url:
  82. status, headers, content = get_response(record.input_url)
  83. if status != 200:
  84. raise ValueError("Failed to retrieve the file from the url.")
  85. else:
  86. extension = mimetypes.guess_extension(headers['content-type'])[1:].strip().lower()
  87. if extension not in converter.imports():
  88. raise ValueError("Invalid import format.")
  89. else:
  90. return export(record, content, record.input_name or "%s.%s" % (uuid.uuid4(), extension))
  91. elif record.input_name and record.input_binary:
  92. return export(record, base64.b64decode(record.input_binary), record.input_name)
  93. else:
  94. raise ValueError("The conversion requires either a valid url or a filename and a file.")