mirror of https://github.com/muk-it/muk_base
MuK IT GmbH
6 years ago
21 changed files with 622 additions and 325 deletions
-
1muk_converter/__init__.py
-
12muk_converter/__manifest__.py
-
6muk_converter/data/params.xml
-
5muk_converter/doc/changelog.rst
-
7muk_converter/doc/index.rst
-
3muk_converter/models/__init__.py
-
78muk_converter/models/converter.py
-
83muk_converter/models/res_config_settings.py
-
10muk_converter/models/store.py
-
1muk_converter/service/__init__.py
-
89muk_converter/service/provider.py
-
161muk_converter/service/unoconv.py
-
BINmuk_converter/static/description/icon.png
-
1muk_converter/static/description/icon.svg
-
49muk_converter/static/description/index.html
-
34muk_converter/tests/test_converter.py
-
7muk_converter/tests/test_unoconv.py
-
14muk_converter/tools/converter.py
-
6muk_converter/views/convert.xml
-
64muk_converter/views/res_config_settings_view.xml
-
92muk_converter/wizards/convert.py
@ -0,0 +1,83 @@ |
|||||
|
################################################################################### |
||||
|
# |
||||
|
# 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 logging |
||||
|
import textwrap |
||||
|
|
||||
|
from odoo import api, fields, models |
||||
|
|
||||
|
class ResConfigSettings(models.TransientModel): |
||||
|
|
||||
|
_inherit = 'res.config.settings' |
||||
|
|
||||
|
converter_service = fields.Selection( |
||||
|
selection=[ |
||||
|
("unoconv", "Local"), |
||||
|
("provider", "Service")], |
||||
|
string="Converter", |
||||
|
default="provider", |
||||
|
help=textwrap.dedent("""\ |
||||
|
Converter engine, which is used for the conversion: |
||||
|
- Local: Use a locally installed unoconv installation |
||||
|
- Service: Use a service to do the conversion |
||||
|
""")) |
||||
|
|
||||
|
converter_max_store = fields.Integer( |
||||
|
string="Storage Size", |
||||
|
help=textwrap.dedent("""\ |
||||
|
To certify the conversion, converted files can be saved |
||||
|
and loaded from memory if necessary. You can set a maximum |
||||
|
size of the storage to prevent massive memory requirements. |
||||
|
""")) |
||||
|
|
||||
|
converter_credit = fields.Boolean( |
||||
|
compute='_compute_converter_credit', |
||||
|
string="Converter insufficient credit") |
||||
|
|
||||
|
@api.multi |
||||
|
def set_values(self): |
||||
|
res = super(ResConfigSettings, self).set_values() |
||||
|
param = self.env['ir.config_parameter'].sudo() |
||||
|
param.set_param("muk_converter.service", self.converter_service) |
||||
|
param.set_param("muk_converter.max_store", self.converter_max_store) |
||||
|
return res |
||||
|
|
||||
|
@api.model |
||||
|
def get_values(self): |
||||
|
res = super(ResConfigSettings, self).get_values() |
||||
|
params = self.env['ir.config_parameter'].sudo() |
||||
|
res.update( |
||||
|
converter_service=params.get_param("muk_converter.service", default="provider"), |
||||
|
converter_max_store=int(params.get_param("muk_converter.max_store", default=20)) |
||||
|
) |
||||
|
return res |
||||
|
|
||||
|
@api.multi |
||||
|
def _compute_converter_credit(self): |
||||
|
credits = self.env['iap.account'].get_credits('muk_converter') |
||||
|
for record in self: |
||||
|
record.converter_credit = credits <= 0 |
||||
|
@api.multi |
||||
|
def redirect_to_buy_converter_credit(self): |
||||
|
url = self.env['iap.account'].get_credits_url('muk_converter') |
||||
|
return { |
||||
|
'type': 'ir.actions.act_url', |
||||
|
'url': url, |
||||
|
'target': '_new', |
||||
|
} |
@ -0,0 +1,89 @@ |
|||||
|
################################################################################### |
||||
|
# |
||||
|
# 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 logging |
||||
|
|
||||
|
from odoo.addons.iap import jsonrpc |
||||
|
|
||||
|
from odoo.addons.muk_utils.tools.cache import memoize |
||||
|
from odoo.addons.muk_utils.tools.file import guess_extension |
||||
|
|
||||
|
_logger = logging.getLogger(__name__) |
||||
|
|
||||
|
CONVERTER_DEFAULT_ENDPOINT = 'https://iap-converter.mukit.at' |
||||
|
CONVERTER_ENDPOINT_FORMATS = '/iap/converter/1/formats' |
||||
|
CONVERTER_ENDPOINT_IMPORTS = '/iap/converter/1/imports' |
||||
|
CONVERTER_ENDPOINT_CONVERT = '/iap/converter/1/convert' |
||||
|
|
||||
|
class RemoteConverter(object): |
||||
|
|
||||
|
def __init__(self, env): |
||||
|
self.params = env['ir.config_parameter'].sudo() |
||||
|
self.account = env['iap.account'].get('muk_converter') |
||||
|
|
||||
|
def endpoint(self, route): |
||||
|
return "%s%s" % (self.params.get_param('muk_converter.endpoint', CONVERTER_DEFAULT_ENDPOINT), route) |
||||
|
|
||||
|
def payload(self, params={}): |
||||
|
params.update({ |
||||
|
'account_token': self.account.account_token, |
||||
|
'database_uuid': self.params.get_param('database.uuid'), |
||||
|
}) |
||||
|
return params |
||||
|
|
||||
|
@property |
||||
|
@memoize(timeout=3600) |
||||
|
def formats(self): |
||||
|
return jsonrpc(self.endpoint(CONVERTER_ENDPOINT_FORMATS), params=self.payload()) |
||||
|
|
||||
|
@property |
||||
|
@memoize(timeout=3600) |
||||
|
def imports(self): |
||||
|
return jsonrpc(self.endpoint(CONVERTER_ENDPOINT_IMPORTS), params=self.payload()) |
||||
|
|
||||
|
def convert(self, binary, mimetype=None, filename=None, export="binary", doctype="document", format="pdf"): |
||||
|
""" Converts a binary value to the given format. |
||||
|
|
||||
|
:param binary: The binary value. |
||||
|
:param mimetype: The mimetype of the binary value. |
||||
|
:param filename: The filename of the binary value. |
||||
|
:param export: The output format (binary, file, base64). |
||||
|
:param doctype: Specify the document type (document, graphics, presentation, spreadsheet). |
||||
|
:param format: Specify the output format for the document. |
||||
|
:return: Returns the output depending on the given format. |
||||
|
:raises ValueError: The file extension could not be determined or the format is invalid. |
||||
|
""" |
||||
|
params = { |
||||
|
'format': format, |
||||
|
'doctype': doctype, |
||||
|
'mimetype': mimetype, |
||||
|
'filename': filename, |
||||
|
'content': base64.b64encode(binary), |
||||
|
} |
||||
|
result = jsonrpc(self.endpoint(CONVERTER_ENDPOINT_CONVERT), params=self.payload(params)) |
||||
|
if export == 'base64': |
||||
|
return result |
||||
|
if export == 'file': |
||||
|
output = io.BytesIO() |
||||
|
output.write(base64.b64decode(result)) |
||||
|
output.close() |
||||
|
return output |
||||
|
else: |
||||
|
return base64.b64decode(result) |
Before Width: 250 | Height: 250 | Size: 19 KiB After Width: 250 | Height: 250 | Size: 20 KiB |
1
muk_converter/static/description/icon.svg
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,64 @@ |
|||||
|
<?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="res_config_settings_view_form" model="ir.ui.view"> |
||||
|
<field name="name">res.config.settings.view.form</field> |
||||
|
<field name="model">res.config.settings</field> |
||||
|
<field name="inherit_id" ref="base_setup.res_config_settings_view_form"/> |
||||
|
<field name="arch" type="xml"> |
||||
|
<div name="multi_company" position="after"> |
||||
|
<h2>File Converter</h2> |
||||
|
<div class="row mt16 o_settings_container" name="web_client"> |
||||
|
<div class="col-xs-12 col-md-6 o_setting_box"> |
||||
|
<div class="o_setting_left_pane"></div> |
||||
|
<div class="o_setting_right_pane"> |
||||
|
<label for="converter_service"/> |
||||
|
<div class="text-muted"> |
||||
|
Converter engine, which is used for the conversion |
||||
|
</div> |
||||
|
<div class="mt8"> |
||||
|
<field name="converter_service" class="o_light_label" widget="radio" required="True"/> |
||||
|
</div> |
||||
|
<div class="content-group" attrs="{'invisible': [('converter_service','!=','provider')]}"> |
||||
|
<div id="partner_autocomplete_settings" position="inside"> |
||||
|
<widget name="iap_credit_checker" service_name="muk_converter"/> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
<div class="col-xs-12 col-md-6 o_setting_box"> |
||||
|
<div class="o_setting_left_pane"></div> |
||||
|
<div class="o_setting_right_pane"> |
||||
|
<label for="converter_max_store"/> |
||||
|
<div class="text-muted"> |
||||
|
Maximum storage size of the converter store |
||||
|
</div> |
||||
|
<div class="mt8"> |
||||
|
<field name="converter_max_store" class="o_light_label" /> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
</odoo> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue