diff --git a/CHANGELOG b/CHANGELOG index 4a2ca8e3ef..0afa32b9aa 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add base64 converter to URL map * Add HTML widget * Support import of native numeric, date and datetime data * Add day view on calendar diff --git a/doc/topics/user_application.rst b/doc/topics/user_application.rst index 38123cfe9d..fdd7ea9c64 100644 --- a/doc/topics/user_application.rst +++ b/doc/topics/user_application.rst @@ -22,6 +22,13 @@ Example:: .. _Rule Format: http://werkzeug.pocoo.org/docs/latest/routing/#rule-format +The following converter is added by Tryton: + + - `base64`: This converter accepts any Base64_ string and transforms it + into its corresponding bytes value. + +.. _Base64: https://en.wikipedia.org/wiki/Base64 + Tryton also provides some wrappers in `trytond.protocols.wrappers` to ease the creation of such route. diff --git a/trytond/wsgi.py b/trytond/wsgi.py index a9ee0c62f5..b348e898eb 100644 --- a/trytond/wsgi.py +++ b/trytond/wsgi.py @@ -1,5 +1,6 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +import base64 import http.client import logging import os @@ -12,7 +13,7 @@ except ImportError: from http import client as HTTPStatus from werkzeug.wrappers import Response -from werkzeug.routing import Map, Rule +from werkzeug.routing import Map, Rule, BaseConverter from werkzeug.exceptions import abort, HTTPException, InternalServerError from werkzeug.contrib.fixers import ProxyFix from werkzeug.wsgi import SharedDataMiddleware @@ -29,10 +30,21 @@ __all__ = ['TrytondWSGI', 'app'] logger = logging.getLogger(__name__) +class Base64Converter(BaseConverter): + + def to_python(self, value): + return base64.urlsafe_b64decode(value).decode('utf-8') + + def to_url(self, value): + return base64.urlsafe_b64encode(value.encode('utf-8')) + + class TrytondWSGI(object): def __init__(self): - self.url_map = Map([]) + self.url_map = Map([], converters={ + 'base64': Base64Converter, + }) self.protocols = [JSONProtocol, XMLProtocol] self.error_handlers = []