forked from lucaswerkmeister/tool-lexeme-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_utils.py
42 lines (32 loc) · 1.37 KB
/
flask_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import flask
import flask.json
import flask.json.tag
import werkzeug.datastructures
class OrderedRequest(flask.Request):
"""Request subclass to use ordered parameter storage"""
parameter_storage_class = werkzeug.datastructures.ImmutableOrderedMultiDict
class OrderedFlask(flask.Flask):
"""Flask subclass to use ordered parameter storage for requests"""
request_class = OrderedRequest
# specific type needed to have access to .serializer
session_interface: flask.sessions.SecureCookieSessionInterface
class TagOrderedMultiDict(flask.json.tag.JSONTag):
__slots__ = ('serializer',)
key = ' omd'
def check(self, value):
return isinstance(value, werkzeug.datastructures.OrderedMultiDict)
def to_json(self, value):
return [(k, self.serializer.tag(v)) for k, v in value.items(multi=True)]
def to_python(self, value):
return werkzeug.datastructures.OrderedMultiDict(value)
class TagImmutableOrderedMultiDict(TagOrderedMultiDict):
key = ' iomd'
def check(self, value):
return isinstance(value, werkzeug.datastructures.ImmutableOrderedMultiDict)
def to_python(self, value):
return werkzeug.datastructures.ImmutableOrderedMultiDict(value)
class SetJSONEncoder(flask.json.JSONEncoder):
def default(self, o):
if isinstance(o, set):
return list(o)
return super().default(o)