-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from cmu-delphi/development
Merge pull request #20 from cmu-delphi/main
- Loading branch information
Showing
13 changed files
with
517 additions
and
63 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class RestApiConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'rest_api' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from rest_framework.serializers import ModelSerializer, SlugRelatedField | ||
|
||
from signals.models import Signal, Geography | ||
|
||
|
||
class SignalSerializer(ModelSerializer): | ||
""" | ||
Serializer for the Signal model. | ||
""" | ||
|
||
source = SlugRelatedField(read_only=True, slug_field="name") | ||
signal_type = SlugRelatedField(read_only=True, slug_field="name") | ||
available_geography = SlugRelatedField(many=True, read_only=True, slug_field="name") | ||
|
||
class Meta: | ||
model = Signal | ||
fields = ["name", "source", "signal_type", "available_geography", "time_type"] | ||
|
||
|
||
class AvailableGeographySerializer(ModelSerializer): | ||
""" | ||
Serializer for the AvailableGeography model. | ||
""" | ||
|
||
class Meta: | ||
model = Geography | ||
fields = ["name", "display_name"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.urls import path | ||
from django.urls.resolvers import URLPattern | ||
|
||
from rest_api.views import api_signal_detail_view, api_available_geography_view | ||
|
||
|
||
urlpatterns: list[URLPattern] = [ | ||
path("rest_api/signal/<int:pk>", api_signal_detail_view, name="api_signal_detail"), | ||
path("rest_api/signal/", api_signal_detail_view, name="api_signal_detail"), | ||
path("rest_api/geo_level/<int:pk>", api_available_geography_view, name="api_available_geography_detail"), | ||
path("rest_api/geo_level/", api_available_geography_view, name="api_available_geography_detail") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
|
||
from rest_api.serializers import SignalSerializer, AvailableGeographySerializer | ||
from signals.models import Signal, Geography | ||
|
||
|
||
@api_view( | ||
[ | ||
"GET", | ||
] | ||
) | ||
def api_signal_detail_view(request, pk): | ||
""" | ||
API view for getting a single Signal object. | ||
Args: | ||
request: The request object. | ||
pk: The primary key of the Signal object. | ||
Returns: | ||
Response: The response object. | ||
""" | ||
|
||
try: | ||
signal = Signal.objects.get(pk=pk) | ||
except Signal.DoesNotExist: | ||
return Response(status=404) | ||
|
||
if request.method == "GET": | ||
serializer = SignalSerializer(signal) | ||
return Response(serializer.data) | ||
|
||
|
||
@api_view( | ||
[ | ||
"GET", | ||
] | ||
) | ||
def api_available_geography_view(request, pk): | ||
""" | ||
API view for getting a single Geography. | ||
Args: | ||
request: The request object. | ||
pk: The primary key of the Geography object. | ||
Returns: | ||
Response: The response object. | ||
""" | ||
|
||
try: | ||
geography = Geography.objects.get(pk=pk) | ||
except Geography.DoesNotExist: | ||
return Response(status=404) | ||
|
||
if request.method == "GET": | ||
serializer = AvailableGeographySerializer(geography) | ||
return Response(serializer.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.