[System] Models: Do not export optional, not set, parameters.

This commit is contained in:
Ferdinand Thiessen 2021-02-14 19:11:39 +01:00
parent a6a1de19de
commit 24418f5bcb
1 changed files with 12 additions and 1 deletions

View File

@ -1,4 +1,6 @@
import sys
import datetime
from sqlalchemy.types import DateTime, TypeDecorator
@ -7,6 +9,15 @@ class ModelSerializeMixin:
Ignores private and protected members as well as members marked as not to publish (name ends with _)
"""
def __is_optional(self, param):
if sys.version_info < (3, 8):
return False
import typing
if typing.get_origin(self.__class__.__annotations__[param]) is typing.Union and \
typing.get_args(self.__class__.__annotations__[param])[1] is None:
return getattr(self, param) is None
def serialize(self):
"""Serialize class to dict
Returns:
@ -15,7 +26,7 @@ class ModelSerializeMixin:
d = {
param: getattr(self, param)
for param in self.__class__.__annotations__
if not param.startswith("_") and not param.endswith("_")
if not param.startswith("_") and not param.endswith("_") and not self.__is_optional(param)
}
if len(d) == 1:
key, value = d.popitem()