Add base64 converter to URL map

issue7920
review45751002
This commit is contained in:
Cédric Krier
2019-03-30 17:33:20 +01:00
parent d15f283b92
commit 7e6d840dd9
3 changed files with 22 additions and 2 deletions
+1
View File
@@ -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
+7
View File
@@ -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.
+14 -2
View File
@@ -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 = []