More robust way to retrieve an object's attribute on schemas

This commit is contained in:
Fabio Manganiello 2022-07-23 17:32:14 +02:00
parent 3edb8352b4
commit 32be4df11c
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -6,7 +6,7 @@ from dateutil.tz import tzutc
from marshmallow import fields from marshmallow import fields
class StrippedString(fields.Function): # lgtm [py/missing-call-to-init] class StrippedString(fields.Function): # lgtm [py/missing-call-to-init]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
kwargs['serialize'] = self._strip kwargs['serialize'] = self._strip
kwargs['deserialize'] = self._strip kwargs['deserialize'] = self._strip
@ -21,7 +21,15 @@ class StrippedString(fields.Function): # lgtm [py/missing-call-to-init]
return value.strip() return value.strip()
class DateTime(fields.Function): # lgtm [py/missing-call-to-init] class Function(fields.Function): # lgtm [py/missing-call-to-init]
def _get_attr(self, obj, attr: str):
if hasattr(obj, attr):
return getattr(obj, attr)
elif hasattr(obj, 'get'):
return obj.get(attr)
class DateTime(Function): # lgtm [py/missing-call-to-init]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.metadata = { self.metadata = {
@ -30,7 +38,7 @@ class DateTime(fields.Function): # lgtm [py/missing-call-to-init]
} }
def _serialize(self, value, attr, obj, **kwargs) -> Optional[str]: def _serialize(self, value, attr, obj, **kwargs) -> Optional[str]:
value = normalize_datetime(obj.get(attr)) value = normalize_datetime(self._get_attr(obj, attr))
if value: if value:
return value.isoformat() return value.isoformat()
@ -38,7 +46,7 @@ class DateTime(fields.Function): # lgtm [py/missing-call-to-init]
return normalize_datetime(value) return normalize_datetime(value)
class Date(fields.Function): # lgtm [py/missing-call-to-init] class Date(Function): # lgtm [py/missing-call-to-init]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.metadata = { self.metadata = {
@ -47,7 +55,7 @@ class Date(fields.Function): # lgtm [py/missing-call-to-init]
} }
def _serialize(self, value, attr, obj, **kwargs) -> Optional[str]: def _serialize(self, value, attr, obj, **kwargs) -> Optional[str]:
value = normalize_datetime(obj.get(attr)) value = normalize_datetime(self._get_attr(obj, attr))
if value: if value:
return date(value.year, value.month, value.day).isoformat() return date(value.year, value.month, value.day).isoformat()
@ -56,10 +64,12 @@ class Date(fields.Function): # lgtm [py/missing-call-to-init]
return date.fromtimestamp(dt.timestamp()) return date.fromtimestamp(dt.timestamp())
def normalize_datetime(dt: Union[str, date, datetime]) -> Optional[Union[date, datetime]]: def normalize_datetime(
dt: Optional[Union[str, date, datetime]]
) -> Optional[Union[date, datetime]]:
if not dt: if not dt:
return return
if isinstance(dt, datetime) or isinstance(dt, date): if isinstance(dt, (datetime, date)):
return dt return dt
try: try: