forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
92 lines (67 loc) · 1.62 KB
/
models.py
File metadata and controls
92 lines (67 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""app.models.py"""
from typing import Dict, List
from pydantic import BaseModel, validator
class Latest(BaseModel):
"""
Latest model.
"""
confirmed: int
deaths: int
recovered: int
class LatestResponse(BaseModel):
"""
Response for latest.
"""
latest: Latest
class Timeline(BaseModel):
"""
Timeline model.
"""
timeline: Dict[str, int] = {}
@validator("timeline")
@classmethod
def sort_timeline(cls, value):
"""Sort the timeline history before inserting into the model"""
return dict(sorted(value.items()))
@property
def latest(self):
"""Get latest available history value."""
return list(self.timeline.values())[-1] if self.timeline else 0
def serialize(self):
"""
Serialize the model into dict
TODO: override dict() instead of using serialize
"""
return {**self.dict(), "latest": self.latest}
class Timelines(BaseModel):
"""
Timelines model.
"""
confirmed: Timeline
deaths: Timeline
recovered: Timeline
class Location(BaseModel):
"""
Location model.
"""
id: int
country: str
country_code: str
country_population: int = None
province: str = ""
county: str = ""
last_updated: str # TODO use datetime.datetime type.
coordinates: Dict
latest: Latest
timelines: Timelines = {}
class LocationResponse(BaseModel):
"""
Response for location.
"""
location: Location
class LocationsResponse(BaseModel):
"""
Response for locations.
"""
latest: Latest
locations: List[Location] = []