From 6045d6092c3101d92316a9d64c463e3f52c0cd11 Mon Sep 17 00:00:00 2001 From: MuK IT GmbH Date: Sat, 13 Jul 2019 19:49:34 +0000 Subject: [PATCH] publish muk_converter - 11.0 --- muk_converter/__manifest__.py | 3 +- muk_converter/models/converter.py | 184 ++++++++++++------------- muk_converter/views/convert.xml | 139 +++++++++---------- muk_converter/wizards/convert.py | 219 ++++++++++++++++-------------- 4 files changed, 281 insertions(+), 264 deletions(-) diff --git a/muk_converter/__manifest__.py b/muk_converter/__manifest__.py index bfa4c37..9363692 100644 --- a/muk_converter/__manifest__.py +++ b/muk_converter/__manifest__.py @@ -20,7 +20,7 @@ { "name": "MuK Converter", "summary": """Universal Converter""", - "version": '11.0.1.2.3', + "version": '11.0.1.2.4', "category": 'Extra Tools', "license": "AGPL-3", "website": "https://www.mukit.at", @@ -28,6 +28,7 @@ "author": "MuK IT", "contributors": [ "Mathias Markl ", + "Kerrim Abd El-Hamed ", ], "depends": [ "iap", diff --git a/muk_converter/models/converter.py b/muk_converter/models/converter.py index 3b0339f..513c972 100644 --- a/muk_converter/models/converter.py +++ b/muk_converter/models/converter.py @@ -1,93 +1,93 @@ -################################################################################### -# -# Copyright (C) 2018 MuK IT GmbH -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -################################################################################### - -import base64 -import hashlib -import logging - -from odoo import api, models, fields, SUPERUSER_ID - -from odoo.addons.muk_converter.service.unoconv import UnoconvConverter -from odoo.addons.muk_converter.service.provider import RemoteConverter - -_logger = logging.getLogger(__name__) - -class Converter(models.AbstractModel): - - _name = 'muk_converter.converter' - _description = 'Converter' - - #---------------------------------------------------------- - # Functions - #---------------------------------------------------------- - - @api.model - def formats(self): - return self._provider().formats - - @api.model - def imports(self): - return self._provider().imports - - @api.model - def convert(self, filename, content, format="pdf", recompute=False, store=True): - binary_content = base64.b64decode(content) - checksum = hashlib.sha1(binary_content).hexdigest() - stored = self._retrieve(checksum, format) - if not recompute and stored.exists(): - return base64.b64encode(stored.content) - else: - name = "%s.%s" % (filename, format) - output = self._parse(filename, binary_content, format) - if store: - self._store(checksum, name, output, format, stored) - return base64.b64encode(output) - - #---------------------------------------------------------- - # Helper - #---------------------------------------------------------- - - @api.model - def _provider(self): - params = self.env['ir.config_parameter'].sudo() - service = params.get_param('muk_converter.service') - if service == 'unoconv': - return UnoconvConverter() - else: - return RemoteConverter(env=self.env) - - @api.model - def _parse(self, filename, content, format): - return self._provider().convert(content, filename=filename, format=format) - - @api.model - def _retrieve(self, checksum, format): - domain = [["checksum", "=", checksum], ["format", "=", format]] - return self.env['muk_converter.store'].sudo().search(domain, limit=1) - - @api.model - def _store(self, checksum, filename, content, format, stored): - if stored and stored.exists(): - stored.write({'used_date': fields.Datetime.now}) - else: - self.env['muk_converter.store'].sudo().create({ - 'checksum': checksum, - 'format': format, - 'content_fname': filename, +################################################################################### +# +# Copyright (C) 2018 MuK IT GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +################################################################################### + +import base64 +import hashlib +import logging + +from odoo import api, models, fields, SUPERUSER_ID + +from odoo.addons.muk_converter.service.unoconv import UnoconvConverter +from odoo.addons.muk_converter.service.provider import RemoteConverter + +_logger = logging.getLogger(__name__) + +class Converter(models.AbstractModel): + + _name = 'muk_converter.converter' + _description = 'Converter' + + #---------------------------------------------------------- + # Functions + #---------------------------------------------------------- + + @api.model + def formats(self): + return self._provider().formats + + @api.model + def imports(self): + return self._provider().imports + + @api.model + def convert(self, filename, content, format="pdf", recompute=False, store=True): + binary_content = base64.b64decode(content) + checksum = hashlib.sha1(binary_content).hexdigest() + stored = self._retrieve(checksum, format) + if not recompute and stored.exists(): + return base64.b64encode(stored.content) + else: + name = "%s.%s" % (filename, format) + output = self._parse(filename, binary_content, format) + if store: + self._store(checksum, name, output, format, stored) + return base64.b64encode(output) + + #---------------------------------------------------------- + # Helper + #---------------------------------------------------------- + + @api.model + def _provider(self): + params = self.env['ir.config_parameter'].sudo() + service = params.get_param('muk_converter.service') + if service == 'unoconv': + return UnoconvConverter() + else: + return RemoteConverter(env=self.env) + + @api.model + def _parse(self, filename, content, format): + return self._provider().convert(content, filename=filename, format=format) + + @api.model + def _retrieve(self, checksum, format): + domain = [["checksum", "=", checksum], ["format", "=", format]] + return self.env['muk_converter.store'].sudo().search(domain, limit=1) + + @api.model + def _store(self, checksum, filename, content, format, stored): + if stored and stored.exists(): + stored.write({'used_date': fields.Datetime.now}) + else: + self.env['muk_converter.store'].sudo().create({ + 'checksum': checksum, + 'format': format, + 'content_fname': filename, 'content': content}) \ No newline at end of file diff --git a/muk_converter/views/convert.xml b/muk_converter/views/convert.xml index 4b8c2e4..0653c17 100644 --- a/muk_converter/views/convert.xml +++ b/muk_converter/views/convert.xml @@ -1,69 +1,70 @@ - - - - - - - - muk_converter_convert.form - muk_converter.convert - -
- - - - - - - - - - -
- -

Conversion Complete

-

The file has been successfully converted and can now be used. - You can download the file by clicking on the link below. -

-

- Here is the converted file: - -

-
-
-
-
-
-
-
-
- - - Convert File - ir.actions.act_window - muk_converter.convert - form - form - new - - -
+ + + + + + + + muk_converter_convert.form + muk_converter.convert + +
+ + + + + + + + + + + +
+ +

Conversion Complete

+

The file has been successfully converted and can now be used. + You can download the file by clicking on the link below. +

+

+ Here is the converted file: + +

+
+
+
+
+
+
+
+
+ + + Convert File + ir.actions.act_window + muk_converter.convert + form + form + new + + +
diff --git a/muk_converter/wizards/convert.py b/muk_converter/wizards/convert.py index d766a8f..166165a 100644 --- a/muk_converter/wizards/convert.py +++ b/muk_converter/wizards/convert.py @@ -1,103 +1,118 @@ -################################################################################### -# -# Copyright (C) 2017 MuK IT GmbH -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -################################################################################### - -import os -import base64 -import uuid -import logging -import mimetypes - -from odoo import _, api, fields, models - -# from odoo.addons.muk_utils.tools.http import get_response TODO -#from odoo.addons.muk_converter.tools import converter - -_logger = logging.getLogger(__name__) - -class ConverterWizard(models.TransientModel): - - _name = "muk_converter.convert" - - #---------------------------------------------------------- - # Selections - #---------------------------------------------------------- - - def _format_selection(self): - formats = self.env['muk_converter.converter'].formats() - return list(map(lambda format: (format, format.upper()), formats)) - - #---------------------------------------------------------- - # Database - #---------------------------------------------------------- - - state = fields.Selection( - selection=[ - ("export", "Export"), - ("download", "Download")], - string="State", - required=True, - default="export") - - input_name = fields.Char( - string="Filename", - states={'export': [('required', True)]}) - - input_binary = fields.Binary( - string="File", - states={'export': [('required', True)]}) - - format = fields.Selection( - selection=_format_selection, - string="Format", - default="pdf", - states={'export': [('required', True)]}) - - output_name = fields.Char( - string="Filename", - readonly=True, - states={'download': [('required', True)]}) - - output_binary = fields.Binary( - string="File", - readonly=True, - states={'download': [('required', True)]}) - - #---------------------------------------------------------- - # Functions - #---------------------------------------------------------- - - @api.multi - def convert(self): - self.ensure_one() - name = "%s.%s" % (os.path.splitext(self.input_name)[0], self.format) - output = self.env['muk_converter.converter'].convert(self.input_name, self.input_binary) - self.write({ - 'state': 'download', - 'output_name': name, - 'output_binary': output}) - return { - "name": _("Convert File"), - 'type': 'ir.actions.act_window', - 'res_model': 'muk_converter.convert', - 'view_mode': 'form', - 'view_type': 'form', - 'res_id': self.id, - 'views': [(False, 'form')], - 'target': 'new', +################################################################################### +# +# Copyright (C) 2017 MuK IT GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +################################################################################### + +import os +import base64 +import uuid +import logging +import mimetypes + +from odoo import _, api, fields, models +from odoo.exceptions import UserError + +from odoo.addons.muk_utils.tools.http import get_response + +_logger = logging.getLogger(__name__) + +class ConverterWizard(models.TransientModel): + + _name = "muk_converter.convert" + + #---------------------------------------------------------- + # Selections + #---------------------------------------------------------- + + def _format_selection(self): + formats = self.env['muk_converter.converter'].formats() + return list(map(lambda format: (format, format.upper()), formats)) + + #---------------------------------------------------------- + # Database + #---------------------------------------------------------- + + state = fields.Selection( + selection=[ + ("export", "Export"), + ("download", "Download")], + string="State", + required=True, + default="export") + + input_name = fields.Char( + string="Filename", + states={'export': [('required', True)]}) + + input_url = fields.Char( + string="URL") + + input_binary = fields.Binary( + string="File") + + format = fields.Selection( + selection=_format_selection, + string="Format", + default="pdf", + states={'export': [('required', True)]}) + + output_name = fields.Char( + string="Filename", + readonly=True, + states={'download': [('required', True)]}) + + output_binary = fields.Binary( + string="File", + readonly=True, + states={'download': [('required', True)]}) + + #---------------------------------------------------------- + # Functions + #---------------------------------------------------------- + + @api.multi + def convert(self): + self.ensure_one() + + if not self.input_url and not self.input_binary: + raise UserError(_("Please choose a file to convert.")) + + if self.input_url: + status, headers, content = get_response(self.input_url) + if status != 200: + raise ValueError(_("Failed to retrieve the file from the url.")) + else: + content = base64.b64encode(content) + else: + content = self.input_binary + + name = "%s.%s" % (os.path.splitext(self.input_name)[0], self.format) + output = self.env['muk_converter.converter'].convert(self.input_name, content, format=self.format) + self.write({ + 'state': 'download', + 'output_name': name, + 'output_binary': output}) + return { + "name": _("Convert File"), + 'type': 'ir.actions.act_window', + 'res_model': 'muk_converter.convert', + 'view_mode': 'form', + 'view_type': 'form', + 'res_id': self.id, + 'views': [(False, 'form')], + 'target': 'new', } \ No newline at end of file