[NEW] Automatic install mailhog. Mailhog integration in Odoo
This commit is contained in:
@@ -1,21 +1,60 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# from odoo import http
|
||||
# Copyright 2024 Open2Bizz <info@open2bizz.nl>
|
||||
# License LGPL-3
|
||||
|
||||
import logging
|
||||
import requests
|
||||
|
||||
from odoo import http
|
||||
from odoo.http import request, Response
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
MAILHOG_BASE = "http://localhost:8025"
|
||||
|
||||
|
||||
# class TestserverO2b(http.Controller):
|
||||
# @http.route('/testserver_o2b/testserver_o2b/', auth='public')
|
||||
# def index(self, **kw):
|
||||
# return "Hello, world"
|
||||
class MailcatcherProxy(http.Controller):
|
||||
|
||||
# @http.route('/testserver_o2b/testserver_o2b/objects/', auth='public')
|
||||
# def list(self, **kw):
|
||||
# return http.request.render('testserver_o2b.listing', {
|
||||
# 'root': '/testserver_o2b/testserver_o2b',
|
||||
# 'objects': http.request.env['testserver_o2b.testserver_o2b'].search([]),
|
||||
# })
|
||||
@http.route('/mailcatcher', type='http', auth='user', website=False)
|
||||
def mailcatcher_index(self, **kw):
|
||||
"""Redirect to the MailHog UI index page."""
|
||||
return request.redirect('/mailcatcher/')
|
||||
|
||||
# @http.route('/testserver_o2b/testserver_o2b/objects/<model("testserver_o2b.testserver_o2b"):obj>/', auth='public')
|
||||
# def object(self, obj, **kw):
|
||||
# return http.request.render('testserver_o2b.object', {
|
||||
# 'object': obj
|
||||
# })
|
||||
@http.route('/mailcatcher/<path:subpath>', type='http', auth='user',
|
||||
website=False, csrf=False)
|
||||
def mailcatcher_proxy(self, subpath='', **kw):
|
||||
"""Proxy requests to the local MailHog instance."""
|
||||
target_url = f"{MAILHOG_BASE}/{subpath}"
|
||||
if request.httprequest.query_string:
|
||||
target_url += '?' + request.httprequest.query_string.decode('utf-8')
|
||||
|
||||
try:
|
||||
headers = {
|
||||
k: v for k, v in request.httprequest.headers
|
||||
if k.lower() not in ('host', 'cookie', 'authorization')
|
||||
}
|
||||
resp = requests.request(
|
||||
method=request.httprequest.method,
|
||||
url=target_url,
|
||||
headers=headers,
|
||||
data=request.httprequest.get_data(),
|
||||
timeout=30,
|
||||
allow_redirects=False,
|
||||
)
|
||||
except requests.exceptions.ConnectionError:
|
||||
_logger.warning("Could not connect to MailHog at %s", MAILHOG_BASE)
|
||||
return Response("MailHog is not available", status=502)
|
||||
|
||||
excluded_headers = {
|
||||
'content-encoding', 'content-length', 'transfer-encoding',
|
||||
'connection',
|
||||
}
|
||||
response_headers = [
|
||||
(k, v) for k, v in resp.headers.items()
|
||||
if k.lower() not in excluded_headers
|
||||
]
|
||||
|
||||
return Response(
|
||||
resp.content,
|
||||
status=resp.status_code,
|
||||
headers=response_headers,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user