changeset 8bc99d04fe2a in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=8bc99d04fe2a
description:
        Add base64 converter to URL map

        issue7920
        review45751002
diffstat:

 CHANGELOG                       |   1 +
 doc/topics/user_application.rst |   7 +++++++
 trytond/wsgi.py                 |  16 ++++++++++++++--
 3 files changed, 22 insertions(+), 2 deletions(-)

diffs (67 lines):

diff -r 67de7622e007 -r 8bc99d04fe2a CHANGELOG
--- a/CHANGELOG Sat Mar 30 17:09:26 2019 +0100
+++ b/CHANGELOG Sat Mar 30 17:33:20 2019 +0100
@@ -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 -r 67de7622e007 -r 8bc99d04fe2a doc/topics/user_application.rst
--- a/doc/topics/user_application.rst   Sat Mar 30 17:09:26 2019 +0100
+++ b/doc/topics/user_application.rst   Sat Mar 30 17:33:20 2019 +0100
@@ -22,6 +22,13 @@
 
 .. _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 -r 67de7622e007 -r 8bc99d04fe2a trytond/wsgi.py
--- a/trytond/wsgi.py   Sat Mar 30 17:09:26 2019 +0100
+++ b/trytond/wsgi.py   Sat Mar 30 17:33:20 2019 +0100
@@ -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 @@
     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 @@
 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 = []
 

Reply via email to