From 07c53588dbf615047feefc3da87eaf08496fbc49 Mon Sep 17 00:00:00 2001 From: Mathias Markl Date: Mon, 14 May 2018 02:06:45 +0200 Subject: [PATCH] 2.0 --- muk_web_preview_mail/README.md | 5 -- muk_web_preview_mail/__init__.py | 2 - muk_web_preview_mail/__manifest__.py | 18 ++-- muk_web_preview_mail/controllers/__init__.py | 2 - muk_web_preview_mail/controllers/main.py | 87 +++++-------------- muk_web_preview_mail/demo/demo.xml | 28 ++++++ .../demo/preview_mail_demo.xml | 13 --- muk_web_preview_mail/doc/changelog.rst | 6 +- muk_web_preview_mail/doc/index.rst | 49 +++++++++++ .../static/description/index.html | 38 +++++++- .../static/src/js/preview_handler.js | 5 +- muk_web_preview_mail/tests/__init__.py | 3 - muk_web_preview_mail/tests/test_mail_parse.py | 12 +-- 13 files changed, 146 insertions(+), 122 deletions(-) delete mode 100644 muk_web_preview_mail/README.md create mode 100644 muk_web_preview_mail/demo/demo.xml delete mode 100644 muk_web_preview_mail/demo/preview_mail_demo.xml create mode 100644 muk_web_preview_mail/doc/index.rst diff --git a/muk_web_preview_mail/README.md b/muk_web_preview_mail/README.md deleted file mode 100644 index d422a74..0000000 --- a/muk_web_preview_mail/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# MuK Preview Mail - -Extendes the Preview Dialog to support mails. Currently the following mail extensions are supported: - -* Microsoft Outlook Express Mail Message (*.eml, message/rfc822) \ No newline at end of file diff --git a/muk_web_preview_mail/__init__.py b/muk_web_preview_mail/__init__.py index 8cfb4c9..6d2ecf5 100644 --- a/muk_web_preview_mail/__init__.py +++ b/muk_web_preview_mail/__init__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - ################################################################################### # # Copyright (C) 2017 MuK IT GmbH diff --git a/muk_web_preview_mail/__manifest__.py b/muk_web_preview_mail/__manifest__.py index 20887eb..d8ef91d 100644 --- a/muk_web_preview_mail/__manifest__.py +++ b/muk_web_preview_mail/__manifest__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - ################################################################################### # # Copyright (C) 2017 MuK IT GmbH @@ -22,28 +20,25 @@ { "name": "MuK Preview Mail", "summary": """Mail Preview""", - "description": """ - Extendes the Extendes the Preview Dialog to support mails. - Currently the following mail extensions are supported: - - Microsoft Outlook Express Mail Message (*.eml, message/rfc822) - """, - "version": "11.0.1.1.0", + "version": "11.0.2.0.0", "category": "Extra Tools", "license": "AGPL-3", "website": "http://www.mukit.at", + "live_test_url": "https://demo.mukit.at/web/login", "author": "MuK IT", "contributors": [ "Mathias Markl ", ], "depends": [ "mail", + "muk_utils", "muk_web_preview", ], "data": [ "template/assets.xml", ], "demo": [ - "demo/preview_mail_demo.xml", + "demo/demo.xml", ], "qweb": [ "static/src/xml/*.xml", @@ -52,10 +47,7 @@ 'static/description/banner.png' ], "external_dependencies": { - "python": [ - 'requests', - 'cachetools' - ], + "python": [], "bin": [], }, "application": False, diff --git a/muk_web_preview_mail/controllers/__init__.py b/muk_web_preview_mail/controllers/__init__.py index fa6e444..4b71c01 100644 --- a/muk_web_preview_mail/controllers/__init__.py +++ b/muk_web_preview_mail/controllers/__init__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - ################################################################################### # # Copyright (C) 2017 MuK IT GmbH diff --git a/muk_web_preview_mail/controllers/main.py b/muk_web_preview_mail/controllers/main.py index 8d8d202..c01dd06 100644 --- a/muk_web_preview_mail/controllers/main.py +++ b/muk_web_preview_mail/controllers/main.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - ################################################################################### # # Copyright (C) 2017 MuK IT GmbH @@ -21,89 +19,44 @@ import os import json -import email -import base64 import urllib import logging import mimetypes import collections -import werkzeug.exceptions -from urllib import parse - -from odoo import _ -from odoo import tools -from odoo import http -from odoo.http import request -from odoo.http import Response +import werkzeug -_logger = logging.getLogger(__name__) +from odoo import _, http +from odoo.http import request, Response -try: - import requests -except ImportError: - _logger.warn('Cannot `import requests`.') +from odoo.addons.muk_utils.http import get_response +from odoo.addons.muk_utils.http import make_error_response -try: - from cachetools import TTLCache - mail_cache = TTLCache(maxsize=50, ttl=600) -except ImportError: - _logger.warn('Cannot `import cachetools`.') +_logger = logging.getLogger(__name__) class MailParserController(http.Controller): _Attachment = collections.namedtuple('Attachment', 'name mimetype extension url info') - @http.route('/web/preview/converter/mail', auth="user", type='http') - def parse_mail(self, url, attachment=None, force_compute=False, **kw): - try: - message = mail_cache[url] if not force_compute else None - except KeyError: - message = None - if not message: - if not bool(parse.urlparse(url).netloc): - url_parts = url.split('?') - path = url_parts[0] - query_string = url_parts[1] if len(url_parts) > 1 else None - router = request.httprequest.app.get_db_router(request.db).bind('') - match = router.match(path, query_args=query_string) - method = router.match(path, query_args=query_string)[0] - params = dict(parse.parse_qsl(query_string)) - if len(match) > 1: - params.update(match[1]) - response = method(**params) - if not response.status_code == 200: - return self._make_error_response(response.status_code,response.description if hasattr(response, 'description') else _("Unknown Error")) - else: - if response.headers['content-type'] == 'message/rfc822': - message = request.env['mail.thread'].message_parse(response.data, False) - else: - return werkzeug.exceptions.UnsupportedMediaType(_("Unparsable message! The file has to be of type: message/rfc822")) - else: - try: - response = requests.get(url) - if response.headers['content-type'] == 'message/rfc822': - message = request.env['mail.thread'].message_parse(response.content, False) - else: - return werkzeug.exceptions.UnsupportedMediaType(_("Unparsable message! The file has to be of type: message/rfc822")) - except requests.exceptions.RequestException as exception: - return self._make_error_response(exception.response.status_code, exception.response.reason or _("Unknown Error")) - mail_cache[url] = message - return self._make_parse_response(request.httprequest.url, message.copy(), attachment) + @http.route('/web/preview/mail', auth="user", type='http') + def preview_mail(self, url, attachment=None, **kw): + status, headers, content = get_response(url) + if status != 200: + return make_error_response(status, content or _("Unknown Error")) + elif headers['content-type'] != 'message/rfc822': + return werkzeug.exceptions.UnsupportedMediaType( + _("Unparsable message! The file has to be of type: message/rfc822")) + else: + message = request.env['mail.thread'].message_parse(content, False) + return self._make_parse_response(request.httprequest.url, message, attachment) def _set_query_parameter(self, url, param_name, param_value): - scheme, netloc, path, query_string, fragment = parse.urlsplit(url) - query_params = parse.parse_qs(query_string) + scheme, netloc, path, query_string, fragment = urllib.parse.urlsplit(url) + query_params = urllib.parse.parse_qs(query_string) query_params[param_name] = [param_value] new_query_string = urllib.parse.urlencode(query_params, doseq=True) - return parse.urlunsplit((scheme, netloc, path, new_query_string, fragment)) + return urllib.parse.urlunsplit((scheme, netloc, path, new_query_string, fragment)) - def _make_error_response(self, status, message): - exception = werkzeug.exceptions.HTTPException() - exception.code = status - exception.description = message - return exception - def _make_attachment_response(self, file, filename): headers = [('Content-Type', mimetypes.guess_type(urllib.request.pathname2url(filename))[0]), ('Content-Disposition', 'attachment; filename="{}";'.format(filename)), diff --git a/muk_web_preview_mail/demo/demo.xml b/muk_web_preview_mail/demo/demo.xml new file mode 100644 index 0000000..7789817 --- /dev/null +++ b/muk_web_preview_mail/demo/demo.xml @@ -0,0 +1,28 @@ + + + + + + + + sample.eml + sample.eml + + + + \ No newline at end of file diff --git a/muk_web_preview_mail/demo/preview_mail_demo.xml b/muk_web_preview_mail/demo/preview_mail_demo.xml deleted file mode 100644 index 65bced4..0000000 --- a/muk_web_preview_mail/demo/preview_mail_demo.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - sample.eml - sample.eml - - - - - - \ No newline at end of file diff --git a/muk_web_preview_mail/doc/changelog.rst b/muk_web_preview_mail/doc/changelog.rst index 0a9ed4b..f10b321 100644 --- a/muk_web_preview_mail/doc/changelog.rst +++ b/muk_web_preview_mail/doc/changelog.rst @@ -1,9 +1,13 @@ +`2.0.0` +------- + +- Migrated to Python 3 + `1.1.0` ------- - Lazy load javascript - `1.0.0` ------- diff --git a/muk_web_preview_mail/doc/index.rst b/muk_web_preview_mail/doc/index.rst new file mode 100644 index 0000000..d9d723d --- /dev/null +++ b/muk_web_preview_mail/doc/index.rst @@ -0,0 +1,49 @@ +================ +MuK Preview Mail +================ + +Extendes the Preview Dialog to support mails. Currently the following +mail extensions are supported: + +* Microsoft Outlook Express Mail Message (\*.eml, message/rfc822) + +Installation +============ + +To install this module, you need to: + +Download the module and add it to your Odoo addons folder. Afterward, log on to +your Odoo server and go to the Apps menu. Trigger the debug modus and update the +list by clicking on the "Update Apps List" link. Now install the module by +clicking on the install button. + +Configuration +============= + +No additional configuration is needed to use this module. + +Usage +============= + +Go to a binary that contains a mail and open the preview dialog to view +the preview. + +Credits +======= + +Contributors +------------ + +* Mathias Markl + +Author & Maintainer +------------------- + +This module is maintained by the `MuK IT GmbH `_. + +MuK IT is an Austrian company specialized in customizing and extending Odoo. +We develop custom solutions for your individual needs to help you focus on +your strength and expertise to grow your business. + +If you want to get in touch please contact us via mail +(sale@mukit.at) or visit our website (https://mukit.at). diff --git a/muk_web_preview_mail/static/description/index.html b/muk_web_preview_mail/static/description/index.html index 9d3931c..20d8dd8 100644 --- a/muk_web_preview_mail/static/description/index.html +++ b/muk_web_preview_mail/static/description/index.html @@ -4,15 +4,15 @@

Preview your mails directly in Odoo.

MuK IT GmbH - www.mukit.at

-
+
-
+
-
+

Overview

Extendes the Preview Dialog to support mails. Currently the following mail extensions are supported:

@@ -24,14 +24,46 @@
+
+

Demo

+
+
+
User:
+
+
+
apps
+
+
+
Password:
+
+
+
demo
+
+
+ +
+

Help and Support

+
Feel free to + contact us, if you need any help with your Odoo integration or + addiontal features.