13
13
import math
14
14
15
15
16
+ def _force_text_recursive (data ):
17
+ """
18
+ Descend into a nested data structure, forcing any
19
+ lazy translation strings into plain text.
20
+ """
21
+ if isinstance (data , list ):
22
+ return [
23
+ _force_text_recursive (item ) for item in data
24
+ ]
25
+ elif isinstance (data , dict ):
26
+ return dict ([
27
+ (key , _force_text_recursive (value ))
28
+ for key , value in data .items ()
29
+ ])
30
+ return force_text (data )
31
+
32
+
16
33
class APIException (Exception ):
17
34
"""
18
35
Base class for REST framework exceptions.
@@ -38,19 +55,6 @@ def __str__(self):
38
55
# from rest_framework import serializers
39
56
# raise serializers.ValidationError('Value was invalid')
40
57
41
- def force_text_recursive (data ):
42
- if isinstance (data , list ):
43
- return [
44
- force_text_recursive (item ) for item in data
45
- ]
46
- elif isinstance (data , dict ):
47
- return dict ([
48
- (key , force_text_recursive (value ))
49
- for key , value in data .items ()
50
- ])
51
- return force_text (data )
52
-
53
-
54
58
class ValidationError (APIException ):
55
59
status_code = status .HTTP_400_BAD_REQUEST
56
60
@@ -59,7 +63,7 @@ def __init__(self, detail):
59
63
# The details should always be coerced to a list if not already.
60
64
if not isinstance (detail , dict ) and not isinstance (detail , list ):
61
65
detail = [detail ]
62
- self .detail = force_text_recursive (detail )
66
+ self .detail = _force_text_recursive (detail )
63
67
64
68
def __str__ (self ):
65
69
return str (self .detail )
0 commit comments