Skip to content

Commit 541d1cb

Browse files
committed
Extract init callbacks
1 parent a0b69c6 commit 541d1cb

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

sqlalchemy_serializer/serializer.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def get_tzinfo(self):
5252
"""
5353
Callback to make serializer aware of user's timezone. Should be redefined if needed
5454
Example:
55-
return pytz.timezone('Asia/Krasnoyarsk')
55+
return pytz.timezone('Africa/Abidjan')
5656
5757
:return: datetime.tzinfo
5858
"""
@@ -114,10 +114,32 @@ class Serializer:
114114
)
115115

116116
def __init__(self, **kwargs):
117-
self.serialization_depth = 0
118-
self.opts = Options(**kwargs)
117+
self.set_serialization_depth(0)
118+
self.set_options(Options(**kwargs))
119119
self.schema = Schema()
120120

121+
def __call__(self, value, only=(), extend=()):
122+
"""
123+
Serialization starts here
124+
:param value: Value to serialize
125+
:param only: Exclusive schema of serialization
126+
:param extend: Rules that extend default schema
127+
:return: object: JSON-compatible object
128+
"""
129+
self.schema.update(only=only, extend=extend)
130+
131+
logger.debug("Call serializer for type:%s", get_type(value))
132+
return self.serialize(value)
133+
134+
def set_serialization_depth(self, value: int):
135+
self.serialization_depth = value
136+
137+
def set_options(self, opts: Options):
138+
self.opts = opts
139+
self.apply_opts()
140+
141+
def apply_opts(self):
142+
"""Apply current Options to callbacks"""
121143
self.serialize_types = (
122144
*(self.opts.serialize_types or ()),
123145
(self.atomic_types, lambda x: x), # Should be checked before any other type
@@ -141,22 +163,6 @@ def __init__(self, **kwargs):
141163
(SerializerMixin, self.serialize_model),
142164
)
143165

144-
def __call__(self, value, only=(), extend=()):
145-
"""
146-
Serialization starts here
147-
:param value: Value to serialize
148-
:param only: Exclusive schema of serialization
149-
:param extend: Rules that extend default schema
150-
:return: object: JSON-compatible object
151-
"""
152-
self.schema.update(only=only, extend=extend)
153-
154-
logger.debug("Call serializer for type:%s", get_type(value))
155-
return self.serialize(value)
156-
157-
def set_serialization_depth(self, value: int):
158-
self.serialization_depth = value
159-
160166
@staticmethod
161167
def is_valid_callable(func) -> bool:
162168
"""

0 commit comments

Comments
 (0)