mirror of https://github.com/muk-it/muk_base
MuK IT GmbH
6 years ago
4 changed files with 281 additions and 264 deletions
-
3muk_converter/__manifest__.py
-
184muk_converter/models/converter.py
-
139muk_converter/views/convert.xml
-
219muk_converter/wizards/convert.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 <http://www.gnu.org/licenses/>. |
|
||||
# |
|
||||
################################################################################### |
|
||||
|
|
||||
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 <http://www.gnu.org/licenses/>. |
||||
|
# |
||||
|
################################################################################### |
||||
|
|
||||
|
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}) |
'content': content}) |
@ -1,69 +1,70 @@ |
|||||
<?xml version="1.0" encoding="utf-8"?> |
|
||||
|
|
||||
<!-- |
|
||||
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 <http://www.gnu.org/licenses/>. |
|
||||
--> |
|
||||
|
|
||||
<odoo> |
|
||||
|
|
||||
<record id="view_converter_convert_form" model="ir.ui.view"> |
|
||||
<field name="name">muk_converter_convert.form</field> |
|
||||
<field name="model">muk_converter.convert</field> |
|
||||
<field name="arch" type="xml"> |
|
||||
<form string="Convert File"> |
|
||||
<group states="export"> |
|
||||
<field invisible="1" name="state" /> |
|
||||
<group> |
|
||||
<field name="input_name" invisible="1" /> |
|
||||
<field name="input_binary" filename="input_name" /> |
|
||||
</group> |
|
||||
<group> |
|
||||
<field name="format" /> |
|
||||
</group> |
|
||||
</group> |
|
||||
<div states="download"> |
|
||||
<field name="output_name" invisible="1" /> |
|
||||
<h3>Conversion Complete</h3> |
|
||||
<p>The file has been successfully converted and can now be used. |
|
||||
You can download the file by clicking on the link below. |
|
||||
</p> |
|
||||
<p> |
|
||||
Here is the converted file: |
|
||||
<field name="output_binary" filename="output_name" options="{'no_export': True}"/> |
|
||||
</p> |
|
||||
</div> |
|
||||
<footer states="export"> |
|
||||
<button name="convert" string="Convert" type="object" class="btn-primary" /> |
|
||||
<button special="cancel" string="Cancel" type="object" |
|
||||
class="btn-default" /> |
|
||||
</footer> |
|
||||
<footer states="download"> |
|
||||
<button special="cancel" string="Close" type="object" class="btn-primary" /> |
|
||||
</footer> |
|
||||
</form> |
|
||||
</field> |
|
||||
</record> |
|
||||
|
|
||||
<record id="action_converter_convert" model="ir.actions.act_window"> |
|
||||
<field name="name">Convert File</field> |
|
||||
<field name="type">ir.actions.act_window</field> |
|
||||
<field name="res_model">muk_converter.convert</field> |
|
||||
<field name="view_type">form</field> |
|
||||
<field name="view_mode">form</field> |
|
||||
<field name="target">new</field> |
|
||||
</record> |
|
||||
|
|
||||
</odoo> |
|
||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
|
||||
|
<!-- |
||||
|
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 <http://www.gnu.org/licenses/>. |
||||
|
--> |
||||
|
|
||||
|
<odoo> |
||||
|
|
||||
|
<record id="view_converter_convert_form" model="ir.ui.view"> |
||||
|
<field name="name">muk_converter_convert.form</field> |
||||
|
<field name="model">muk_converter.convert</field> |
||||
|
<field name="arch" type="xml"> |
||||
|
<form string="Convert File"> |
||||
|
<group states="export"> |
||||
|
<field invisible="1" name="state" /> |
||||
|
<group> |
||||
|
<field name="input_name" invisible="1" /> |
||||
|
<field name="input_url" attrs="{'invisible':[('input_url','=',False)]}" readonly="1" /> |
||||
|
<field name="input_binary" filename="input_name" attrs="{'invisible':[('input_url','!=',False)]}" /> |
||||
|
</group> |
||||
|
<group> |
||||
|
<field name="format" /> |
||||
|
</group> |
||||
|
</group> |
||||
|
<div states="download"> |
||||
|
<field name="output_name" invisible="1" /> |
||||
|
<h3>Conversion Complete</h3> |
||||
|
<p>The file has been successfully converted and can now be used. |
||||
|
You can download the file by clicking on the link below. |
||||
|
</p> |
||||
|
<p> |
||||
|
Here is the converted file: |
||||
|
<field name="output_binary" filename="output_name" options="{'no_export': True}"/> |
||||
|
</p> |
||||
|
</div> |
||||
|
<footer states="export"> |
||||
|
<button name="convert" string="Convert" type="object" class="btn-primary" /> |
||||
|
<button special="cancel" string="Cancel" type="object" |
||||
|
class="btn-default" /> |
||||
|
</footer> |
||||
|
<footer states="download"> |
||||
|
<button special="cancel" string="Close" type="object" class="btn-primary" /> |
||||
|
</footer> |
||||
|
</form> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
<record id="action_converter_convert" model="ir.actions.act_window"> |
||||
|
<field name="name">Convert File</field> |
||||
|
<field name="type">ir.actions.act_window</field> |
||||
|
<field name="res_model">muk_converter.convert</field> |
||||
|
<field name="view_type">form</field> |
||||
|
<field name="view_mode">form</field> |
||||
|
<field name="target">new</field> |
||||
|
</record> |
||||
|
|
||||
|
</odoo> |
@ -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 <http://www.gnu.org/licenses/>. |
|
||||
# |
|
||||
################################################################################### |
|
||||
|
|
||||
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 <http://www.gnu.org/licenses/>. |
||||
|
# |
||||
|
################################################################################### |
||||
|
|
||||
|
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', |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue