move include and exclude checks

This commit is contained in:
collerek
2020-11-10 11:34:04 +01:00
parent 6cd68294da
commit 0547259050

View File

@ -178,13 +178,22 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
self._orm.remove_parent(self, name) self._orm.remove_parent(self, name)
@classmethod @classmethod
def get_properties(cls) -> List[str]: def get_properties(
return [ cls,
include: Union["AbstractSetIntStr", "MappingIntStrAny"] = None,
exclude: Union["AbstractSetIntStr", "MappingIntStrAny"] = None,
) -> List[str]:
props = [
prop prop
for prop in dir(cls) for prop in dir(cls)
if isinstance(getattr(cls, prop), property) if isinstance(getattr(cls, prop), property)
and prop not in ("__values__", "__fields__", "fields", "pk_column") and prop not in ("__values__", "__fields__", "fields", "pk_column")
] ]
if include:
props = [prop for prop in props if prop in include]
if exclude:
props = [prop for prop in props if prop not in exclude]
return props
def dict( # noqa A003 def dict( # noqa A003
self, self,
@ -226,11 +235,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
dict_instance[field] = None dict_instance[field] = None
# include model properties as fields # include model properties as fields
props = self.get_properties() props = self.get_properties(include=include, exclude=exclude)
if include:
props = [prop for prop in props if prop in include]
if exclude:
props = [prop for prop in props if prop not in exclude]
if props: if props:
dict_instance.update({prop: getattr(self, prop) for prop in props}) dict_instance.update({prop: getattr(self, prop) for prop in props})