Skip to content

Commit f28c264

Browse files
author
ExpDev07
committed
Core changes + /v2/
1 parent 50d2fcf commit f28c264

File tree

16 files changed

+249
-26
lines changed

16 files changed

+249
-26
lines changed

app/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from flask import Flask
22
from flask_cors import CORS
3-
from . import settings
43

54
def create_app():
65
"""
@@ -12,7 +11,7 @@ def create_app():
1211
CORS(app)
1312

1413
# Set app config from settings.
15-
app.config.from_pyfile('settings.py');
14+
app.config.from_pyfile('config/settings.py');
1615

1716
with app.app_context():
1817
# Import routes.
File renamed without changes.

app/coordinates.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Coordinates:
2+
"""
3+
A position on earth using decimal coordinates (latitude and longitude).
4+
"""
5+
6+
def __init__(self, latitude, longitude):
7+
self.latitude = latitude
8+
self.longitude = longitude
9+
10+
def serialize(self):
11+
"""
12+
Serializes the coordinates into a dict.
13+
"""
14+
return {
15+
'latitude' : self.latitude,
16+
'longitude': self.longitude
17+
}
18+
19+
def __str__(self):
20+
return 'lat: %s, long: %s' % (self.latitude, self.longitude)

app/location.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from .coordinates import Coordinates
2+
from .utils import countrycodes
3+
4+
class Location:
5+
"""
6+
A location in the world affected by the coronavirus.
7+
"""
8+
9+
def __init__(self, id, country, province, coordinates, confirmed, deaths, recovered):
10+
# General info.
11+
self.id = id
12+
self.country = country.strip()
13+
self.province = province.strip()
14+
self.coordinates = coordinates
15+
16+
# Data.
17+
self.confirmed = confirmed
18+
self.deaths = deaths
19+
self.recovered = recovered
20+
21+
22+
@property
23+
def country_code(self):
24+
"""
25+
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
26+
"""
27+
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
28+
29+
def serialize(self):
30+
"""
31+
Serializes the location into a dict.
32+
"""
33+
return {
34+
# General info.
35+
'id' : self.id,
36+
'country' : self.country,
37+
'province' : self.province,
38+
'country_code': self.country_code,
39+
40+
# Coordinates.
41+
'coordinates': self.coordinates.serialize(),
42+
43+
# Latest data.
44+
'latest': {
45+
'confirmed': self.confirmed.latest,
46+
'deaths' : self.deaths.latest,
47+
'recovered': self.recovered.latest
48+
}
49+
}

app/models/location.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

app/routes/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from . import confirmed
2-
from . import deaths
3-
from . import recovered
4-
from . import all
1+
# API version 1.
2+
from .v1 import confirmed, deaths, recovered, all
3+
4+
# API version 2.
5+
from .v2 import locations, latest
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from flask import jsonify
22
from flask import current_app as app
3-
from ..data import get_data
3+
from ...services.location.jhu import get_category
44

55
@app.route('/all')
66
def all():
77
# Get all the categories.
8-
confirmed = get_data('confirmed')
9-
deaths = get_data('deaths')
10-
recovered = get_data('recovered')
8+
confirmed = get_category('confirmed')
9+
deaths = get_category('deaths')
10+
recovered = get_category('recovered')
1111

1212
return jsonify({
1313
# Data.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask import jsonify
22
from flask import current_app as app
3-
from ..data import get_data
3+
from ...services.location.jhu import get_category
44

55
@app.route('/confirmed')
66
def confirmed():
7-
return jsonify(get_data('confirmed'))
7+
return jsonify(get_category('confirmed'))
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask import jsonify
22
from flask import current_app as app
3-
from ..data import get_data
3+
from ...services.location.jhu import get_category
44

55
@app.route('/deaths')
66
def deaths():
7-
return jsonify(get_data('deaths'))
7+
return jsonify(get_category('deaths'))
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask import jsonify
22
from flask import current_app as app
3-
from ..data import get_data
3+
from ...services.location.jhu import get_category
44

55
@app.route('/recovered')
66
def recovered():
7-
return jsonify(get_data('recovered'))
7+
return jsonify(get_category('recovered'))

0 commit comments

Comments
 (0)