|
19 | 19 | logger.setLevel(level="WARN") |
20 | 20 |
|
21 | 21 |
|
22 | | -class SerializerMixin: |
23 | | - """ |
24 | | - Mixin for retrieving public fields of sqlAlchemy-model in json-compatible format |
25 | | - with no pain |
26 | | - It can be inherited to redefine get_tzinfo callback, datetime formats or to add |
27 | | - some extra serialization logic |
28 | | - """ |
29 | | - |
30 | | - # Default exclusive schema. |
31 | | - # If left blank, serializer becomes greedy and takes all SQLAlchemy-model's attributes |
32 | | - serialize_only: tuple = () |
33 | | - |
34 | | - # Additions to default schema. Can include negative rules |
35 | | - serialize_rules: tuple = () |
36 | | - |
37 | | - # Extra serialising functions |
38 | | - serialize_types: tuple = () |
39 | | - |
40 | | - # Custom list of fields to serialize in this model |
41 | | - serializable_keys: tuple = () |
42 | | - |
43 | | - date_format = "%Y-%m-%d" |
44 | | - datetime_format = "%Y-%m-%d %H:%M:%S" |
45 | | - time_format = "%H:%M" |
46 | | - decimal_format = "{}" |
47 | | - |
48 | | - # Serialize fields of the model defined as @property automatically |
49 | | - auto_serialize_properties: bool = False |
50 | | - |
51 | | - def get_tzinfo(self): |
52 | | - """ |
53 | | - Callback to make serializer aware of user's timezone. Should be redefined if needed |
54 | | - Example: |
55 | | - return pytz.timezone('Africa/Abidjan') |
56 | | -
|
57 | | - :return: datetime.tzinfo |
58 | | - """ |
59 | | - return None |
60 | | - |
61 | | - def to_dict( |
62 | | - self, |
63 | | - only=(), |
64 | | - rules=(), |
65 | | - date_format=None, |
66 | | - datetime_format=None, |
67 | | - time_format=None, |
68 | | - tzinfo=None, |
69 | | - decimal_format=None, |
70 | | - serialize_types=None, |
71 | | - ): |
72 | | - """ |
73 | | - Returns SQLAlchemy model's data in JSON compatible format |
74 | | -
|
75 | | - For details about datetime formats follow: |
76 | | - https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior |
77 | | -
|
78 | | - :param only: exclusive schema to replace the default one |
79 | | - always have higher priority than rules |
80 | | - :param rules: schema to extend default one or schema defined in "only" |
81 | | - :param date_format: str |
82 | | - :param datetime_format: str |
83 | | - :param time_format: str |
84 | | - :param decimal_format: str |
85 | | - :param serialize_types: |
86 | | - :param tzinfo: datetime.tzinfo converts datetimes to local user timezone |
87 | | - :return: data: dict |
88 | | - """ |
89 | | - s = Serializer( |
90 | | - date_format=date_format or self.date_format, |
91 | | - datetime_format=datetime_format or self.datetime_format, |
92 | | - time_format=time_format or self.time_format, |
93 | | - decimal_format=decimal_format or self.decimal_format, |
94 | | - tzinfo=tzinfo or self.get_tzinfo(), |
95 | | - serialize_types=serialize_types or self.serialize_types, |
96 | | - ) |
97 | | - return s(self, only=only, extend=rules) |
98 | | - |
99 | | - |
100 | 22 | Options = namedtuple( |
101 | 23 | "Options", |
102 | 24 | "date_format datetime_format time_format decimal_format tzinfo serialize_types", |
103 | 25 | ) |
104 | 26 |
|
105 | 27 |
|
| 28 | +class IsNotSerializable(Exception): |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +def get_type(value) -> str: |
| 33 | + return type(value).__name__ |
| 34 | + |
| 35 | + |
106 | 36 | class Serializer: |
107 | 37 | # Types that do nod need any serialization logic |
108 | 38 | atomic_types = ( |
@@ -291,13 +221,83 @@ def serialize_model(self, value) -> dict: |
291 | 221 | return res |
292 | 222 |
|
293 | 223 |
|
294 | | -class IsNotSerializable(Exception): |
295 | | - pass |
| 224 | +def serialize_collection(iterable: t.Iterable, *args, **kwargs) -> list: |
| 225 | + return [item.to_dict(*args, **kwargs) for item in iterable] |
296 | 226 |
|
297 | 227 |
|
298 | | -def get_type(value) -> str: |
299 | | - return type(value).__name__ |
| 228 | +class SerializerMixin: |
| 229 | + """ |
| 230 | + Mixin for retrieving public fields of sqlAlchemy-model in json-compatible format |
| 231 | + with no pain |
| 232 | + It can be inherited to redefine get_tzinfo callback, datetime formats or to add |
| 233 | + some extra serialization logic |
| 234 | + """ |
300 | 235 |
|
| 236 | + # Default exclusive schema. |
| 237 | + # If left blank, serializer becomes greedy and takes all SQLAlchemy-model's attributes |
| 238 | + serialize_only: tuple = () |
301 | 239 |
|
302 | | -def serialize_collection(iterable: t.Iterable, *args, **kwargs) -> list: |
303 | | - return [item.to_dict(*args, **kwargs) for item in iterable] |
| 240 | + # Additions to default schema. Can include negative rules |
| 241 | + serialize_rules: tuple = () |
| 242 | + |
| 243 | + # Extra serialising functions |
| 244 | + serialize_types: tuple = () |
| 245 | + |
| 246 | + # Custom list of fields to serialize in this model |
| 247 | + serializable_keys: tuple = () |
| 248 | + |
| 249 | + date_format = "%Y-%m-%d" |
| 250 | + datetime_format = "%Y-%m-%d %H:%M:%S" |
| 251 | + time_format = "%H:%M" |
| 252 | + decimal_format = "{}" |
| 253 | + |
| 254 | + # Serialize fields of the model defined as @property automatically |
| 255 | + auto_serialize_properties: bool = False |
| 256 | + |
| 257 | + def get_tzinfo(self): |
| 258 | + """ |
| 259 | + Callback to make serializer aware of user's timezone. Should be redefined if needed |
| 260 | + Example: |
| 261 | + return pytz.timezone('Africa/Abidjan') |
| 262 | +
|
| 263 | + :return: datetime.tzinfo |
| 264 | + """ |
| 265 | + return None |
| 266 | + |
| 267 | + def to_dict( |
| 268 | + self, |
| 269 | + only=(), |
| 270 | + rules=(), |
| 271 | + date_format=None, |
| 272 | + datetime_format=None, |
| 273 | + time_format=None, |
| 274 | + tzinfo=None, |
| 275 | + decimal_format=None, |
| 276 | + serialize_types=None, |
| 277 | + ): |
| 278 | + """ |
| 279 | + Returns SQLAlchemy model's data in JSON compatible format |
| 280 | +
|
| 281 | + For details about datetime formats follow: |
| 282 | + https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior |
| 283 | +
|
| 284 | + :param only: exclusive schema to replace the default one |
| 285 | + always have higher priority than rules |
| 286 | + :param rules: schema to extend default one or schema defined in "only" |
| 287 | + :param date_format: str |
| 288 | + :param datetime_format: str |
| 289 | + :param time_format: str |
| 290 | + :param decimal_format: str |
| 291 | + :param serialize_types: |
| 292 | + :param tzinfo: datetime.tzinfo converts datetimes to local user timezone |
| 293 | + :return: data: dict |
| 294 | + """ |
| 295 | + s = Serializer( |
| 296 | + date_format=date_format or self.date_format, |
| 297 | + datetime_format=datetime_format or self.datetime_format, |
| 298 | + time_format=time_format or self.time_format, |
| 299 | + decimal_format=decimal_format or self.decimal_format, |
| 300 | + tzinfo=tzinfo or self.get_tzinfo(), |
| 301 | + serialize_types=serialize_types or self.serialize_types, |
| 302 | + ) |
| 303 | + return s(self, only=only, extend=rules) |
0 commit comments