Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply decorators to location
  • Loading branch information
antheia-z committed Aug 16, 2021
commit 3278a3b5b832dbd5cb7b08b641e4fce0dd3cac2f
46 changes: 31 additions & 15 deletions app/location/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ def serialize(self):
}


def decoratedSerialize(func, inc_timelines, *args, **kwargs):
serialized = func()
if kwargs.get("timelines") and inc_timelines:
# Whether to include the timelines or not.
timelines = kwargs["timelines"]
if timelines:
serialized.update(
{
"timelines": {
# Serialize all the timelines.
key: value.serialize()
for (key, value) in self.timelines.items()
}
}
)

if kwargs.get("state"):
state = kwargs["state"]
serialized.update({"state": state})

if kwargs.get("county"):
county = kwargs["county"]
serialized.update({"county": county})

return serialized

class TimelinedLocation(Location):
"""
A location with timelines.
Expand All @@ -96,29 +122,19 @@ def __init__(self, id, country, province, coordinates, last_updated, timelines):

# Set timelines.
self.timelines = timelines
self.inc_timelines = false

# pylint: disable=arguments-differ
def serialize(self, timelines=False):
@decoratedSerialize(timelines=self.timelines, inc_timelines=self.inc_timelines)
def serialize(self, inc_timelines=false):
"""
Serializes the location into a dict.

:param timelines: Whether to include the timelines.
:param inc_timelines: Whether to include the timelines.
:returns: The serialized location.
:rtype: dict
"""
serialized = super().serialize()

# Whether to include the timelines or not.
if timelines:
serialized.update(
{
"timelines": {
# Serialize all the timelines.
key: value.serialize()
for (key, value) in self.timelines.items()
}
}
)
self.inc_timelines = inc_timelines

# Return the serialized location.
return serialized
9 changes: 3 additions & 6 deletions app/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@ def __init__(self, id, state, county, coordinates, last_updated, confirmed, deat
self.state = state
self.county = county

def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
@decoratedSerialize(state=self.state, county=self.county)
def serialize(self): # pylint: disable=arguments-differ,unused-argument
"""
Serializes the location into a dict.


:returns: The serialized location.
:rtype: dict
"""
serialized = super().serialize()

# Update with new fields.
serialized.update(
{"state": self.state, "county": self.county,}
)

# Return the serialized location.
return serialized
11 changes: 3 additions & 8 deletions app/location/nyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ def __init__(self, id, state, county, coordinates, last_updated, timelines):
self.state = state
self.county = county

def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
@decoratedSerialize(timelines=self.timelines, state=self.state, county=self.county)
def serialize(self, inc_timelines=False): # pylint: disable=arguments-differ,unused-argument
"""
Serializes the location into a dict.

:returns: The serialized location.
:rtype: dict
"""
serialized = super().serialize(timelines)

# Update with new fields.
serialized.update(
{"state": self.state, "county": self.county,}
)
serialized = super().serialize(inc_timelines)

# Return the serialized location.
return serialized