django-cattrs-fields 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_cattrs_fields/__init__.py +1 -0
- django_cattrs_fields/converters/__init__.py +9 -0
- django_cattrs_fields/converters/bson.py +54 -0
- django_cattrs_fields/converters/cbor2.py +31 -0
- django_cattrs_fields/converters/json.py +39 -0
- django_cattrs_fields/converters/msgpack.py +39 -0
- django_cattrs_fields/converters/msgspec.py +13 -0
- django_cattrs_fields/converters/orjson.py +32 -0
- django_cattrs_fields/converters/pyyaml.py +37 -0
- django_cattrs_fields/converters/register_hooks.py +310 -0
- django_cattrs_fields/converters/tomlkit.py +30 -0
- django_cattrs_fields/converters/ujson.py +38 -0
- django_cattrs_fields/fields/__init__.py +61 -0
- django_cattrs_fields/fields/files.py +9 -0
- django_cattrs_fields/hooks/__init__.py +134 -0
- django_cattrs_fields/hooks/bool_hooks.py +49 -0
- django_cattrs_fields/hooks/char_hooks.py +139 -0
- django_cattrs_fields/hooks/date_hooks.py +135 -0
- django_cattrs_fields/hooks/empty_hooks.py +407 -0
- django_cattrs_fields/hooks/file_hooks.py +64 -0
- django_cattrs_fields/hooks/number_hooks.py +124 -0
- django_cattrs_fields/utils/decimal.py +14 -0
- django_cattrs_fields/utils/timezone.py +56 -0
- django_cattrs_fields/validators/__init__.py +29 -0
- django_cattrs_fields-0.0.1.dist-info/METADATA +404 -0
- django_cattrs_fields-0.0.1.dist-info/RECORD +28 -0
- django_cattrs_fields-0.0.1.dist-info/WHEEL +4 -0
- django_cattrs_fields-0.0.1.dist-info/licenses/LICENSE +27 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.0.1"
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from bson.binary import Binary
|
|
4
|
+
|
|
5
|
+
from cattrs.preconf.bson import make_converter
|
|
6
|
+
|
|
7
|
+
from django.conf import settings
|
|
8
|
+
|
|
9
|
+
from django_cattrs_fields.fields import DateField, DecimalField, TimeField, UUIDField
|
|
10
|
+
from django_cattrs_fields.hooks.date_hooks import time_unstructure_str
|
|
11
|
+
from django_cattrs_fields.hooks.number_hooks import decimal_unstructure_str
|
|
12
|
+
|
|
13
|
+
from .register_hooks import (
|
|
14
|
+
register_structure_hooks,
|
|
15
|
+
register_unstructure_hooks,
|
|
16
|
+
register_datetime_unstructure_hooks,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
serializer = make_converter()
|
|
20
|
+
|
|
21
|
+
register_structure_hooks(serializer)
|
|
22
|
+
|
|
23
|
+
register_unstructure_hooks(serializer)
|
|
24
|
+
register_datetime_unstructure_hooks(serializer)
|
|
25
|
+
|
|
26
|
+
if getattr(settings, "DCF_SERIALIZER_HOOKS", True):
|
|
27
|
+
serializer.register_unstructure_hook(UUIDField, lambda x: Binary.from_uuid(x))
|
|
28
|
+
serializer.register_unstructure_hook(
|
|
29
|
+
Union[UUIDField, None], lambda x: Binary.from_uuid(x) if x else None
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
def bson_uuid_structure(val, _) -> UUIDField:
|
|
33
|
+
if isinstance(val, Binary):
|
|
34
|
+
return val.as_uuid()
|
|
35
|
+
return val
|
|
36
|
+
|
|
37
|
+
def bson_uuid_structure_nullable(val, _) -> UUIDField | None:
|
|
38
|
+
if val is None:
|
|
39
|
+
return None
|
|
40
|
+
return bson_uuid_structure(val, _)
|
|
41
|
+
|
|
42
|
+
serializer.register_unstructure_hook(DateField, lambda x: x.isoformat())
|
|
43
|
+
serializer.register_unstructure_hook(
|
|
44
|
+
Union[DateField, None], lambda x: x.isoformat() if x else None
|
|
45
|
+
)
|
|
46
|
+
serializer.register_structure_hook(UUIDField, bson_uuid_structure)
|
|
47
|
+
serializer.register_structure_hook(Union[UUIDField, None], bson_uuid_structure_nullable)
|
|
48
|
+
|
|
49
|
+
serializer.register_unstructure_hook(DecimalField, decimal_unstructure_str)
|
|
50
|
+
serializer.register_unstructure_hook(Union[DecimalField, None], decimal_unstructure_str)
|
|
51
|
+
serializer.register_unstructure_hook(TimeField, time_unstructure_str)
|
|
52
|
+
serializer.register_unstructure_hook(Union[TimeField, None], time_unstructure_str)
|
|
53
|
+
|
|
54
|
+
__all__ = ("serializer",)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from cattrs.preconf.cbor2 import make_converter
|
|
4
|
+
from django.conf import settings
|
|
5
|
+
|
|
6
|
+
from django_cattrs_fields.fields import TimeField
|
|
7
|
+
from django_cattrs_fields.hooks.date_hooks import time_unstructure_str
|
|
8
|
+
|
|
9
|
+
from .register_hooks import (
|
|
10
|
+
register_date_unstructure_hooks,
|
|
11
|
+
register_datetime_unstructure_hooks,
|
|
12
|
+
register_decimal_unstructure_hooks,
|
|
13
|
+
register_structure_hooks,
|
|
14
|
+
register_unstructure_hooks,
|
|
15
|
+
register_uuid_unstructure_hooks,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
serializer = make_converter()
|
|
19
|
+
|
|
20
|
+
register_structure_hooks(serializer)
|
|
21
|
+
register_unstructure_hooks(serializer)
|
|
22
|
+
register_datetime_unstructure_hooks(serializer)
|
|
23
|
+
register_date_unstructure_hooks(serializer)
|
|
24
|
+
register_decimal_unstructure_hooks(serializer)
|
|
25
|
+
register_uuid_unstructure_hooks(serializer)
|
|
26
|
+
|
|
27
|
+
if getattr(settings, "DCF_SERIALIZER_HOOKS", True):
|
|
28
|
+
serializer.register_unstructure_hook(TimeField, time_unstructure_str)
|
|
29
|
+
serializer.register_unstructure_hook(Union[TimeField, None], time_unstructure_str)
|
|
30
|
+
|
|
31
|
+
__all__ = ("serializer",)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from django.conf import settings
|
|
4
|
+
|
|
5
|
+
from cattrs.preconf.json import make_converter
|
|
6
|
+
|
|
7
|
+
from django_cattrs_fields.fields import DateField, DateTimeField, DecimalField, TimeField, UUIDField
|
|
8
|
+
from django_cattrs_fields.hooks.date_hooks import time_unstructure_str
|
|
9
|
+
from django_cattrs_fields.hooks.number_hooks import decimal_unstructure_str
|
|
10
|
+
|
|
11
|
+
from .register_hooks import (
|
|
12
|
+
register_structure_hooks,
|
|
13
|
+
register_unstructure_hooks,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
serializer = make_converter()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
register_structure_hooks(serializer)
|
|
20
|
+
register_unstructure_hooks(serializer)
|
|
21
|
+
|
|
22
|
+
if getattr(settings, "DCF_SERIALIZER_HOOKS", True):
|
|
23
|
+
serializer.register_unstructure_hook(UUIDField, lambda x: str(x))
|
|
24
|
+
serializer.register_unstructure_hook(Union[UUIDField, None], lambda x: str(x) if x else None)
|
|
25
|
+
|
|
26
|
+
serializer.register_unstructure_hook(DateField, lambda x: x.isoformat())
|
|
27
|
+
serializer.register_unstructure_hook(
|
|
28
|
+
Union[DateField, None], lambda x: x.isoformat() if x else None
|
|
29
|
+
)
|
|
30
|
+
serializer.register_unstructure_hook(DateTimeField, lambda x: x.isoformat())
|
|
31
|
+
serializer.register_unstructure_hook(
|
|
32
|
+
Union[DateTimeField, None], lambda x: x.isoformat() if x else None
|
|
33
|
+
)
|
|
34
|
+
serializer.register_unstructure_hook(DecimalField, decimal_unstructure_str)
|
|
35
|
+
serializer.register_unstructure_hook(Union[DecimalField, None], decimal_unstructure_str)
|
|
36
|
+
serializer.register_unstructure_hook(TimeField, time_unstructure_str)
|
|
37
|
+
serializer.register_unstructure_hook(Union[TimeField, None], time_unstructure_str)
|
|
38
|
+
|
|
39
|
+
__all__ = ("serializer",)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from django.conf import settings
|
|
4
|
+
|
|
5
|
+
from cattrs.preconf.msgpack import make_converter
|
|
6
|
+
|
|
7
|
+
from django_cattrs_fields.fields import DateField, DateTimeField, DecimalField, TimeField, UUIDField
|
|
8
|
+
from django_cattrs_fields.hooks.date_hooks import time_unstructure_str
|
|
9
|
+
from django_cattrs_fields.hooks.number_hooks import decimal_unstructure_str
|
|
10
|
+
|
|
11
|
+
from .register_hooks import (
|
|
12
|
+
register_structure_hooks,
|
|
13
|
+
register_unstructure_hooks,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
serializer = make_converter()
|
|
17
|
+
|
|
18
|
+
register_structure_hooks(serializer)
|
|
19
|
+
register_unstructure_hooks(serializer)
|
|
20
|
+
|
|
21
|
+
if getattr(settings, "DCF_SERIALIZER_HOOKS", True):
|
|
22
|
+
serializer.register_unstructure_hook(UUIDField, lambda x: str(x))
|
|
23
|
+
serializer.register_unstructure_hook(Union[UUIDField, None], lambda x: str(x) if x else None)
|
|
24
|
+
|
|
25
|
+
serializer.register_unstructure_hook(DateField, lambda x: x.isoformat())
|
|
26
|
+
serializer.register_unstructure_hook(
|
|
27
|
+
Union[DateField, None], lambda x: x.isoformat() if x else None
|
|
28
|
+
)
|
|
29
|
+
serializer.register_unstructure_hook(DateTimeField, lambda x: x.isoformat())
|
|
30
|
+
serializer.register_unstructure_hook(
|
|
31
|
+
Union[DateTimeField, None], lambda x: x.isoformat() if x else None
|
|
32
|
+
)
|
|
33
|
+
serializer.register_unstructure_hook(DecimalField, decimal_unstructure_str)
|
|
34
|
+
serializer.register_unstructure_hook(Union[DecimalField, None], decimal_unstructure_str)
|
|
35
|
+
serializer.register_unstructure_hook(TimeField, time_unstructure_str)
|
|
36
|
+
serializer.register_unstructure_hook(Union[TimeField, None], time_unstructure_str)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
__all__ = ("serializer",)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from cattrs.preconf.msgspec import make_converter
|
|
2
|
+
|
|
3
|
+
from .register_hooks import (
|
|
4
|
+
register_structure_hooks,
|
|
5
|
+
register_all_unstructure_hooks,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
serializer = make_converter()
|
|
9
|
+
|
|
10
|
+
register_structure_hooks(serializer)
|
|
11
|
+
register_all_unstructure_hooks(serializer)
|
|
12
|
+
|
|
13
|
+
__all__ = ("serializer",)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from cattrs.preconf.orjson import make_converter
|
|
4
|
+
from django.conf import settings
|
|
5
|
+
|
|
6
|
+
from django_cattrs_fields.fields import DecimalField
|
|
7
|
+
from django_cattrs_fields.hooks.number_hooks import decimal_unstructure_str
|
|
8
|
+
|
|
9
|
+
from .register_hooks import (
|
|
10
|
+
register_date_unstructure_hooks,
|
|
11
|
+
register_datetime_unstructure_hooks,
|
|
12
|
+
register_structure_hooks,
|
|
13
|
+
register_time_unstructure_hooks,
|
|
14
|
+
register_unstructure_hooks,
|
|
15
|
+
register_uuid_unstructure_hooks,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
serializer = make_converter()
|
|
19
|
+
|
|
20
|
+
register_structure_hooks(serializer)
|
|
21
|
+
|
|
22
|
+
register_unstructure_hooks(serializer)
|
|
23
|
+
register_uuid_unstructure_hooks(serializer)
|
|
24
|
+
register_date_unstructure_hooks(serializer)
|
|
25
|
+
register_datetime_unstructure_hooks(serializer)
|
|
26
|
+
register_time_unstructure_hooks(serializer)
|
|
27
|
+
|
|
28
|
+
if getattr(settings, "DCF_SERIALIZER_HOOKS", True):
|
|
29
|
+
serializer.register_unstructure_hook(DecimalField, decimal_unstructure_str)
|
|
30
|
+
serializer.register_unstructure_hook(Union[DecimalField, None], decimal_unstructure_str)
|
|
31
|
+
|
|
32
|
+
__all__ = ("serializer",)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from django.conf import settings
|
|
4
|
+
|
|
5
|
+
from cattrs.preconf.pyyaml import make_converter
|
|
6
|
+
|
|
7
|
+
from django_cattrs_fields.fields import DecimalField, TimeField, UUIDField
|
|
8
|
+
from django_cattrs_fields.hooks.date_hooks import time_unstructure_str
|
|
9
|
+
from django_cattrs_fields.hooks.number_hooks import decimal_unstructure_str
|
|
10
|
+
|
|
11
|
+
from .register_hooks import (
|
|
12
|
+
register_structure_hooks,
|
|
13
|
+
register_unstructure_hooks,
|
|
14
|
+
register_datetime_unstructure_hooks,
|
|
15
|
+
register_date_unstructure_hooks,
|
|
16
|
+
register_time_unstructure_hooks,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
serializer = make_converter()
|
|
20
|
+
|
|
21
|
+
register_structure_hooks(serializer)
|
|
22
|
+
|
|
23
|
+
register_unstructure_hooks(serializer)
|
|
24
|
+
register_date_unstructure_hooks(serializer)
|
|
25
|
+
register_datetime_unstructure_hooks(serializer)
|
|
26
|
+
register_time_unstructure_hooks(serializer)
|
|
27
|
+
|
|
28
|
+
if getattr(settings, "DCF_SERIALIZER_HOOKS", True):
|
|
29
|
+
serializer.register_unstructure_hook(UUIDField, lambda x: str(x))
|
|
30
|
+
serializer.register_unstructure_hook(Union[UUIDField, None], lambda x: str(x) if x else None)
|
|
31
|
+
serializer.register_unstructure_hook(DecimalField, decimal_unstructure_str)
|
|
32
|
+
serializer.register_unstructure_hook(Union[DecimalField, None], decimal_unstructure_str)
|
|
33
|
+
serializer.register_unstructure_hook(TimeField, time_unstructure_str)
|
|
34
|
+
serializer.register_unstructure_hook(Union[TimeField, None], time_unstructure_str)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
__all__ = ("serializer",)
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
from typing import Union, get_args
|
|
2
|
+
|
|
3
|
+
from attrs import has
|
|
4
|
+
from cattrs._compat import is_annotated
|
|
5
|
+
from cattrs.converters import Converter
|
|
6
|
+
from django.conf import settings
|
|
7
|
+
|
|
8
|
+
from django_cattrs_fields.fields import (
|
|
9
|
+
BooleanField,
|
|
10
|
+
CharField,
|
|
11
|
+
DateField,
|
|
12
|
+
DateTimeField,
|
|
13
|
+
DecimalField,
|
|
14
|
+
EmailField,
|
|
15
|
+
EmptyField,
|
|
16
|
+
FloatField,
|
|
17
|
+
IntegerField,
|
|
18
|
+
SlugField,
|
|
19
|
+
TimeField,
|
|
20
|
+
URLField,
|
|
21
|
+
UUIDField,
|
|
22
|
+
)
|
|
23
|
+
from django_cattrs_fields.fields.files import FileField
|
|
24
|
+
from django_cattrs_fields.hooks import (
|
|
25
|
+
boolean_structure,
|
|
26
|
+
boolean_structure_nullable,
|
|
27
|
+
boolean_unstructure,
|
|
28
|
+
char_structure,
|
|
29
|
+
char_structure_nullable,
|
|
30
|
+
char_unstructure,
|
|
31
|
+
date_structure,
|
|
32
|
+
date_structure_nullable,
|
|
33
|
+
date_unstructure,
|
|
34
|
+
datetime_structure,
|
|
35
|
+
datetime_structure_nullable,
|
|
36
|
+
datetime_unstructure,
|
|
37
|
+
decimal_structure,
|
|
38
|
+
decimal_structure_annotated,
|
|
39
|
+
decimal_structure_nullable,
|
|
40
|
+
decimal_unstructure,
|
|
41
|
+
email_structure,
|
|
42
|
+
email_structure_nullable,
|
|
43
|
+
email_unstructure,
|
|
44
|
+
file_structure,
|
|
45
|
+
file_structure_nullable,
|
|
46
|
+
file_unstructure,
|
|
47
|
+
float_structure,
|
|
48
|
+
float_structure_nullable,
|
|
49
|
+
float_unstructure,
|
|
50
|
+
integer_structure,
|
|
51
|
+
integer_structure_nullable,
|
|
52
|
+
integer_unstructure,
|
|
53
|
+
slug_structure,
|
|
54
|
+
slug_structure_nullable,
|
|
55
|
+
slug_unstructure,
|
|
56
|
+
time_structure,
|
|
57
|
+
time_structure_nullable,
|
|
58
|
+
time_unstructure,
|
|
59
|
+
url_structure,
|
|
60
|
+
url_structure_nullable,
|
|
61
|
+
url_unstructure,
|
|
62
|
+
uuid_structure,
|
|
63
|
+
uuid_structure_nullable,
|
|
64
|
+
uuid_unstructure,
|
|
65
|
+
)
|
|
66
|
+
from django_cattrs_fields.hooks.empty_hooks import (
|
|
67
|
+
empty_bool_structure,
|
|
68
|
+
empty_bool_structure_nullable,
|
|
69
|
+
empty_bool_unstructure,
|
|
70
|
+
empty_char_structure,
|
|
71
|
+
empty_char_structure_nullable,
|
|
72
|
+
empty_char_unstructure,
|
|
73
|
+
empty_date_structure,
|
|
74
|
+
empty_date_structure_nullable,
|
|
75
|
+
empty_date_unstructure,
|
|
76
|
+
empty_datetime_structure,
|
|
77
|
+
empty_datetime_structure_nullable,
|
|
78
|
+
empty_datetime_unstructure,
|
|
79
|
+
empty_decimal_structure,
|
|
80
|
+
empty_decimal_structure_nullable,
|
|
81
|
+
empty_decimal_unstructure,
|
|
82
|
+
empty_email_structure,
|
|
83
|
+
empty_email_structure_nullable,
|
|
84
|
+
empty_email_unstructure,
|
|
85
|
+
empty_file_structure,
|
|
86
|
+
empty_file_structure_nullable,
|
|
87
|
+
empty_file_unstructure,
|
|
88
|
+
empty_float_structure,
|
|
89
|
+
empty_float_structure_nullable,
|
|
90
|
+
empty_float_unstructure,
|
|
91
|
+
empty_integer_structure,
|
|
92
|
+
empty_integer_structure_nullable,
|
|
93
|
+
empty_integer_unstructure,
|
|
94
|
+
empty_slug_structure,
|
|
95
|
+
empty_slug_structure_nullable,
|
|
96
|
+
empty_slug_unstructure,
|
|
97
|
+
empty_structure,
|
|
98
|
+
empty_time_structure,
|
|
99
|
+
empty_time_structure_nullable,
|
|
100
|
+
empty_time_unstructure,
|
|
101
|
+
empty_unstructure,
|
|
102
|
+
empty_url_structure,
|
|
103
|
+
empty_url_structure_nullable,
|
|
104
|
+
empty_url_unstructure,
|
|
105
|
+
empty_uuid_structure,
|
|
106
|
+
empty_uuid_structure_nullable,
|
|
107
|
+
empty_uuid_unstructure,
|
|
108
|
+
skip_empty,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def register_structure_hooks(converter: Converter):
|
|
113
|
+
converter.register_structure_hook(BooleanField, boolean_structure)
|
|
114
|
+
converter.register_structure_hook(CharField, char_structure)
|
|
115
|
+
converter.register_structure_hook(DateField, date_structure)
|
|
116
|
+
converter.register_structure_hook(DateTimeField, datetime_structure)
|
|
117
|
+
converter.register_structure_hook_func(
|
|
118
|
+
lambda t: is_annotated(t) and get_args(t)[0] is DecimalField, decimal_structure_annotated
|
|
119
|
+
)
|
|
120
|
+
converter.register_structure_hook(DecimalField, decimal_structure)
|
|
121
|
+
converter.register_structure_hook(EmailField, email_structure)
|
|
122
|
+
converter.register_structure_hook(EmptyField, empty_structure)
|
|
123
|
+
converter.register_structure_hook(FloatField, float_structure)
|
|
124
|
+
converter.register_structure_hook(IntegerField, integer_structure)
|
|
125
|
+
converter.register_structure_hook(SlugField, slug_structure)
|
|
126
|
+
converter.register_structure_hook(TimeField, time_structure)
|
|
127
|
+
converter.register_structure_hook(URLField, url_structure)
|
|
128
|
+
converter.register_structure_hook(UUIDField, uuid_structure)
|
|
129
|
+
|
|
130
|
+
# Union types
|
|
131
|
+
converter.register_structure_hook(Union[BooleanField, None], boolean_structure_nullable)
|
|
132
|
+
converter.register_structure_hook(Union[CharField, None], char_structure_nullable)
|
|
133
|
+
converter.register_structure_hook(Union[DateField, None], date_structure_nullable)
|
|
134
|
+
converter.register_structure_hook(Union[DecimalField, None], decimal_structure_nullable)
|
|
135
|
+
converter.register_structure_hook(Union[DateTimeField, None], datetime_structure_nullable)
|
|
136
|
+
converter.register_structure_hook(Union[EmailField, None], email_structure_nullable)
|
|
137
|
+
converter.register_structure_hook(Union[FloatField, None], float_structure_nullable)
|
|
138
|
+
converter.register_structure_hook(Union[IntegerField, None], integer_structure_nullable)
|
|
139
|
+
converter.register_structure_hook(Union[SlugField, None], slug_structure_nullable)
|
|
140
|
+
converter.register_structure_hook(Union[TimeField, None], time_structure_nullable)
|
|
141
|
+
converter.register_structure_hook(Union[URLField, None], url_structure_nullable)
|
|
142
|
+
converter.register_structure_hook(Union[UUIDField, None], uuid_structure_nullable)
|
|
143
|
+
|
|
144
|
+
# Empty Unions
|
|
145
|
+
converter.register_structure_hook(Union[BooleanField, EmptyField], empty_bool_structure)
|
|
146
|
+
converter.register_structure_hook(Union[CharField, EmptyField], empty_char_structure)
|
|
147
|
+
converter.register_structure_hook(Union[EmptyField, EmptyField], empty_email_structure)
|
|
148
|
+
converter.register_structure_hook(Union[SlugField, EmptyField], empty_slug_structure)
|
|
149
|
+
converter.register_structure_hook(Union[URLField, EmptyField], empty_url_structure)
|
|
150
|
+
converter.register_structure_hook(Union[UUIDField, EmptyField], empty_uuid_structure)
|
|
151
|
+
converter.register_structure_hook(Union[IntegerField, EmptyField], empty_integer_structure)
|
|
152
|
+
converter.register_structure_hook(Union[DecimalField, EmptyField], empty_decimal_structure)
|
|
153
|
+
converter.register_structure_hook(Union[FloatField, EmptyField], empty_float_structure)
|
|
154
|
+
converter.register_structure_hook(Union[DateField, EmptyField], empty_date_structure)
|
|
155
|
+
converter.register_structure_hook(Union[DateTimeField, EmptyField], empty_datetime_structure)
|
|
156
|
+
converter.register_structure_hook(Union[TimeField, EmptyField], empty_time_structure)
|
|
157
|
+
|
|
158
|
+
converter.register_structure_hook(
|
|
159
|
+
Union[BooleanField, EmptyField], empty_bool_structure_nullable
|
|
160
|
+
)
|
|
161
|
+
converter.register_structure_hook(
|
|
162
|
+
Union[CharField, EmptyField, None], empty_char_structure_nullable
|
|
163
|
+
)
|
|
164
|
+
converter.register_structure_hook(
|
|
165
|
+
Union[EmptyField, EmptyField, None], empty_email_structure_nullable
|
|
166
|
+
)
|
|
167
|
+
converter.register_structure_hook(
|
|
168
|
+
Union[SlugField, EmptyField, None], empty_slug_structure_nullable
|
|
169
|
+
)
|
|
170
|
+
converter.register_structure_hook(
|
|
171
|
+
Union[URLField, EmptyField, None], empty_url_structure_nullable
|
|
172
|
+
)
|
|
173
|
+
converter.register_structure_hook(
|
|
174
|
+
Union[UUIDField, EmptyField, None], empty_uuid_structure_nullable
|
|
175
|
+
)
|
|
176
|
+
converter.register_structure_hook(
|
|
177
|
+
Union[IntegerField, EmptyField, None], empty_integer_structure_nullable
|
|
178
|
+
)
|
|
179
|
+
converter.register_structure_hook(
|
|
180
|
+
Union[DecimalField, EmptyField, None], empty_decimal_structure_nullable
|
|
181
|
+
)
|
|
182
|
+
converter.register_structure_hook(
|
|
183
|
+
Union[FloatField, EmptyField, None], empty_float_structure_nullable
|
|
184
|
+
)
|
|
185
|
+
converter.register_structure_hook(
|
|
186
|
+
Union[DateField, EmptyField, None], empty_date_structure_nullable
|
|
187
|
+
)
|
|
188
|
+
converter.register_structure_hook(
|
|
189
|
+
Union[DateTimeField, EmptyField, None], empty_datetime_structure_nullable
|
|
190
|
+
)
|
|
191
|
+
converter.register_structure_hook(
|
|
192
|
+
Union[TimeField, EmptyField, None], empty_time_structure_nullable
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
# File
|
|
196
|
+
|
|
197
|
+
if getattr(settings, "DCF_FILE_HOOKS", True):
|
|
198
|
+
converter.register_structure_hook(FileField, file_structure)
|
|
199
|
+
converter.register_structure_hook(Union[FileField, None], file_structure_nullable)
|
|
200
|
+
converter.register_structure_hook(Union[FileField, EmptyField], empty_file_structure)
|
|
201
|
+
converter.register_structure_hook(
|
|
202
|
+
Union[FileField, EmptyField, None], empty_file_structure_nullable
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def register_unstructure_hooks(converter: Converter):
|
|
207
|
+
converter.register_unstructure_hook_factory(has, skip_empty)
|
|
208
|
+
|
|
209
|
+
converter.register_unstructure_hook(BooleanField, boolean_unstructure)
|
|
210
|
+
converter.register_unstructure_hook(CharField, char_unstructure)
|
|
211
|
+
converter.register_unstructure_hook(EmailField, email_unstructure)
|
|
212
|
+
converter.register_unstructure_hook(EmptyField, empty_unstructure)
|
|
213
|
+
converter.register_unstructure_hook(FloatField, float_unstructure)
|
|
214
|
+
converter.register_unstructure_hook(IntegerField, integer_unstructure)
|
|
215
|
+
converter.register_unstructure_hook(SlugField, slug_unstructure)
|
|
216
|
+
converter.register_unstructure_hook(URLField, url_unstructure)
|
|
217
|
+
|
|
218
|
+
# Union types
|
|
219
|
+
converter.register_unstructure_hook(Union[BooleanField, None], boolean_unstructure)
|
|
220
|
+
converter.register_unstructure_hook(Union[CharField, None], char_unstructure)
|
|
221
|
+
converter.register_unstructure_hook(Union[EmailField, None], email_unstructure)
|
|
222
|
+
converter.register_unstructure_hook(Union[FloatField, None], float_unstructure)
|
|
223
|
+
converter.register_unstructure_hook(Union[IntegerField, None], integer_unstructure)
|
|
224
|
+
converter.register_unstructure_hook(Union[SlugField, None], slug_unstructure)
|
|
225
|
+
converter.register_unstructure_hook(Union[URLField, None], url_unstructure)
|
|
226
|
+
|
|
227
|
+
# Empty Unions
|
|
228
|
+
converter.register_unstructure_hook(Union[BooleanField, EmptyField], empty_bool_unstructure)
|
|
229
|
+
converter.register_unstructure_hook(Union[CharField, EmptyField], empty_char_unstructure)
|
|
230
|
+
converter.register_unstructure_hook(Union[EmailField, EmptyField], empty_email_unstructure)
|
|
231
|
+
converter.register_unstructure_hook(Union[SlugField, EmptyField], empty_slug_unstructure)
|
|
232
|
+
converter.register_unstructure_hook(Union[URLField, EmptyField], empty_url_unstructure)
|
|
233
|
+
converter.register_unstructure_hook(Union[UUIDField, EmptyField], empty_uuid_unstructure)
|
|
234
|
+
converter.register_unstructure_hook(Union[IntegerField, EmptyField], empty_integer_unstructure)
|
|
235
|
+
converter.register_unstructure_hook(Union[DecimalField, EmptyField], empty_decimal_unstructure)
|
|
236
|
+
converter.register_unstructure_hook(Union[FloatField, EmptyField], empty_float_unstructure)
|
|
237
|
+
converter.register_unstructure_hook(Union[DateField, EmptyField], empty_date_unstructure)
|
|
238
|
+
converter.register_unstructure_hook(
|
|
239
|
+
Union[DateTimeField, EmptyField], empty_datetime_unstructure
|
|
240
|
+
)
|
|
241
|
+
converter.register_unstructure_hook(Union[TimeField, EmptyField], empty_time_unstructure)
|
|
242
|
+
|
|
243
|
+
converter.register_unstructure_hook(
|
|
244
|
+
Union[BooleanField, EmptyField, None], empty_bool_unstructure
|
|
245
|
+
)
|
|
246
|
+
converter.register_unstructure_hook(Union[CharField, EmptyField, None], empty_char_unstructure)
|
|
247
|
+
converter.register_unstructure_hook(
|
|
248
|
+
Union[EmailField, EmptyField, None], empty_email_unstructure
|
|
249
|
+
)
|
|
250
|
+
converter.register_unstructure_hook(Union[SlugField, EmptyField, None], empty_slug_unstructure)
|
|
251
|
+
converter.register_unstructure_hook(Union[URLField, EmptyField, None], empty_url_unstructure)
|
|
252
|
+
converter.register_unstructure_hook(Union[UUIDField, EmptyField, None], empty_uuid_unstructure)
|
|
253
|
+
converter.register_unstructure_hook(
|
|
254
|
+
Union[IntegerField, EmptyField, None], empty_integer_unstructure
|
|
255
|
+
)
|
|
256
|
+
converter.register_unstructure_hook(
|
|
257
|
+
Union[DecimalField, EmptyField, None], empty_decimal_unstructure
|
|
258
|
+
)
|
|
259
|
+
converter.register_unstructure_hook(
|
|
260
|
+
Union[FloatField, EmptyField, None], empty_float_unstructure
|
|
261
|
+
)
|
|
262
|
+
converter.register_unstructure_hook(Union[DateField, EmptyField, None], empty_date_unstructure)
|
|
263
|
+
converter.register_unstructure_hook(
|
|
264
|
+
Union[DateTimeField, EmptyField], empty_datetime_unstructure
|
|
265
|
+
)
|
|
266
|
+
converter.register_unstructure_hook(Union[TimeField, EmptyField], empty_time_unstructure)
|
|
267
|
+
|
|
268
|
+
# File
|
|
269
|
+
|
|
270
|
+
if getattr(settings, "DCF_FILE_HOOKS", True):
|
|
271
|
+
converter.register_unstructure_hook(FileField, file_unstructure)
|
|
272
|
+
converter.register_unstructure_hook(Union[FileField, None], file_unstructure)
|
|
273
|
+
converter.register_unstructure_hook(Union[FileField, EmptyField], empty_file_unstructure)
|
|
274
|
+
converter.register_unstructure_hook(
|
|
275
|
+
Union[FileField, EmptyField, None], empty_file_unstructure
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def register_uuid_unstructure_hooks(converter: Converter):
|
|
280
|
+
converter.register_unstructure_hook(UUIDField, uuid_unstructure)
|
|
281
|
+
converter.register_unstructure_hook(Union[UUIDField, None], uuid_unstructure)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def register_date_unstructure_hooks(converter: Converter):
|
|
285
|
+
converter.register_unstructure_hook(DateField, date_unstructure)
|
|
286
|
+
converter.register_unstructure_hook(Union[DateField, None], date_unstructure)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def register_datetime_unstructure_hooks(converter: Converter):
|
|
290
|
+
converter.register_unstructure_hook(DateTimeField, datetime_unstructure)
|
|
291
|
+
converter.register_unstructure_hook(Union[DateTimeField, None], datetime_unstructure)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def register_decimal_unstructure_hooks(converter: Converter):
|
|
295
|
+
converter.register_unstructure_hook(DecimalField, decimal_unstructure)
|
|
296
|
+
converter.register_unstructure_hook(Union[DecimalField, None], decimal_unstructure)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def register_time_unstructure_hooks(converter: Converter):
|
|
300
|
+
converter.register_unstructure_hook(TimeField, time_unstructure)
|
|
301
|
+
converter.register_unstructure_hook(Union[TimeField, None], time_unstructure)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def register_all_unstructure_hooks(converter: Converter):
|
|
305
|
+
register_unstructure_hooks(converter)
|
|
306
|
+
register_uuid_unstructure_hooks(converter)
|
|
307
|
+
register_date_unstructure_hooks(converter)
|
|
308
|
+
register_datetime_unstructure_hooks(converter)
|
|
309
|
+
register_decimal_unstructure_hooks(converter)
|
|
310
|
+
register_time_unstructure_hooks(converter)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
|
|
3
|
+
from cattrs.preconf.tomlkit import make_converter
|
|
4
|
+
|
|
5
|
+
from django_cattrs_fields.fields import DecimalField, UUIDField
|
|
6
|
+
from django_cattrs_fields.hooks.number_hooks import decimal_unstructure_str
|
|
7
|
+
|
|
8
|
+
from .register_hooks import (
|
|
9
|
+
register_structure_hooks,
|
|
10
|
+
register_unstructure_hooks,
|
|
11
|
+
register_datetime_unstructure_hooks,
|
|
12
|
+
register_date_unstructure_hooks,
|
|
13
|
+
register_time_unstructure_hooks,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
serializer = make_converter()
|
|
17
|
+
|
|
18
|
+
register_structure_hooks(serializer)
|
|
19
|
+
|
|
20
|
+
register_unstructure_hooks(serializer)
|
|
21
|
+
register_date_unstructure_hooks(serializer)
|
|
22
|
+
register_datetime_unstructure_hooks(serializer)
|
|
23
|
+
register_time_unstructure_hooks(serializer)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if getattr(settings, "DCF_SERIALIZER_HOOKS", True):
|
|
27
|
+
serializer.register_unstructure_hook(UUIDField, lambda x: str(x))
|
|
28
|
+
serializer.register_unstructure_hook(DecimalField, decimal_unstructure_str)
|
|
29
|
+
|
|
30
|
+
__all__ = ("serializer",)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from django.conf import settings
|
|
4
|
+
|
|
5
|
+
from cattrs.preconf.ujson import make_converter
|
|
6
|
+
|
|
7
|
+
from django_cattrs_fields.fields import DateField, DateTimeField, DecimalField, TimeField, UUIDField
|
|
8
|
+
from django_cattrs_fields.hooks.date_hooks import time_unstructure_str
|
|
9
|
+
from django_cattrs_fields.hooks.number_hooks import decimal_unstructure_str
|
|
10
|
+
|
|
11
|
+
from .register_hooks import (
|
|
12
|
+
register_structure_hooks,
|
|
13
|
+
register_unstructure_hooks,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
serializer = make_converter()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
register_structure_hooks(serializer)
|
|
20
|
+
register_unstructure_hooks(serializer)
|
|
21
|
+
|
|
22
|
+
if getattr(settings, "DCF_SERIALIZER_HOOKS", True):
|
|
23
|
+
serializer.register_unstructure_hook(UUIDField, lambda x: str(x))
|
|
24
|
+
serializer.register_unstructure_hook(Union[UUIDField, None], lambda x: str(x) if x else None)
|
|
25
|
+
serializer.register_unstructure_hook(DateField, lambda x: x.isoformat())
|
|
26
|
+
serializer.register_unstructure_hook(
|
|
27
|
+
Union[DateField, None], lambda x: x.isoformat() if x else None
|
|
28
|
+
)
|
|
29
|
+
serializer.register_unstructure_hook(DateTimeField, lambda x: x.isoformat())
|
|
30
|
+
serializer.register_unstructure_hook(
|
|
31
|
+
Union[DateTimeField, None], lambda x: x.isoformat() if x else None
|
|
32
|
+
)
|
|
33
|
+
serializer.register_unstructure_hook(DecimalField, decimal_unstructure_str)
|
|
34
|
+
serializer.register_unstructure_hook(Union[DecimalField, None], decimal_unstructure_str)
|
|
35
|
+
serializer.register_unstructure_hook(TimeField, time_unstructure_str)
|
|
36
|
+
serializer.register_unstructure_hook(Union[TimeField, None], time_unstructure_str)
|
|
37
|
+
|
|
38
|
+
__all__ = ("serializer",)
|