Skip to content

Commit c254f91

Browse files
Xavier MedranoXavier Medrano
authored andcommitted
add api to fetch data
1 parent 3c95112 commit c254f91

5 files changed

Lines changed: 30 additions & 2 deletions

File tree

map/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<meta content="TK" name="description" />
1717
<meta
18-
content="{{ cookiecutter.project_verbose_name }}"
18+
content="Code Challenge v2"
1919
name="author"
2020
/>
2121
<meta property="og:site_name" content="Code Challenge v2" />

map/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
urlpatterns = [
88
path("", views.Home.as_view(), name="home"),
9+
path("map-data/", views.MapDataView.as_view(), name="map_data"),
910
path("admin/", admin.site.urls),
1011
path("robots.txt/", views.robots_txt),
1112
]

map/views.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
import os
2+
import csv
3+
import json
24

35
from django.shortcuts import render
4-
56
from django.views.generic import TemplateView
67

8+
from rest_framework.views import APIView
9+
from rest_framework.response import Response
10+
from rest_framework import status
11+
712

813
class Home(TemplateView):
914
template_name = "map/home_page.html"
1015

1116

17+
class MapDataView(APIView):
18+
def get(self, request):
19+
DATA_DIR = os.path.join(os.getcwd(), 'data/raw')
20+
response = {"restaurants": [], "community_areas": {}}
21+
22+
# Load in the csv of restaurants
23+
with open(f"{DATA_DIR}/chicago-restaurants.csv", "r") as data_infile:
24+
reader = csv.DictReader(data_infile)
25+
response["restaurants"] = [row for row in reader]
26+
27+
# Load in the geojson of community areas
28+
with open(f"{DATA_DIR}/community-areas.geojson", "r") as geojson_infile:
29+
response["community_areas"] = json.load(geojson_infile)
30+
31+
return Response(response, status=status.HTTP_200_OK)
32+
33+
1234
def robots_txt(request):
1335
return render(
1436
request,

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pytest==9.0.2
99
sentry-sdk==2.54.0
1010
setuptools==82.0.0
1111
whitenoise==6.12.0
12+
djangorestframework==3.16.1

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ const config = {
9393
},
9494
],
9595
},
96+
{
97+
test: /\.(png)$/,
98+
type: 'asset/resource',
99+
},
96100
],
97101
},
98102
}

0 commit comments

Comments
 (0)