Skip to content

Commit

Permalink
Merge pull request #23 from cmu-delphi/development
Browse files Browse the repository at this point in the history
 Merge pull request #20 from cmu-delphi/main
  • Loading branch information
dmytrotsko authored Nov 4, 2024
2 parents f5ef5f0 + 45d4bc9 commit 8c75032
Show file tree
Hide file tree
Showing 13 changed files with 517 additions and 63 deletions.
Empty file added src/rest_api/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions src/rest_api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions src/rest_api/apps.py
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.
3 changes: 3 additions & 0 deletions src/rest_api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
27 changes: 27 additions & 0 deletions src/rest_api/serializers.py
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"]
12 changes: 12 additions & 0 deletions src/rest_api/urls.py
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")
]
59 changes: 59 additions & 0 deletions src/rest_api/views.py
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)
1 change: 1 addition & 0 deletions src/signal_documentation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
'datasources',
'signals',
'signal_sets',
'rest_api',
]

INSTALLED_APPS: list[str] = DEFAULT_APPS + EXTERNAL_APPS + LOCAL_APPS
Expand Down
7 changes: 6 additions & 1 deletion src/signal_documentation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@
),
# datasources
path(
f"{settings.MAIN_PAGE}/datasources/" if settings.MAIN_PAGE else "datasources",
f"{settings.MAIN_PAGE}/datasources/" if settings.MAIN_PAGE else "datasources/",
include("datasources.urls"),
),
# rest_api
path(
f"{settings.MAIN_PAGE}/rest_api/" if settings.MAIN_PAGE else "rest_api/",
include("rest_api.urls"),
),
path("__debug__/", include("debug_toolbar.urls")),
]

Expand Down
25 changes: 24 additions & 1 deletion src/signal_sets/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
from typing import Any, Dict

from django.conf import settings
from django.db.models.query import QuerySet
from django.views.generic import ListView, DetailView


from signals.models import Geography
from signal_sets.models import SignalSet
from signal_sets.filters import SignalSetFilter
from signal_sets.forms import SignalSetFilterForm
Expand Down Expand Up @@ -40,7 +43,9 @@ def get_url_params(self):
else None
),
"data_source": [el for el in self.request.GET.getlist("data_source")],
"temporal_granularity": [el for el in self.request.GET.getlist("temporal_granularity")],
"temporal_granularity": [
el for el in self.request.GET.getlist("temporal_granularity")
],
}
url_params_str = ""
for param_name, param_value in url_params_dict.items():
Expand Down Expand Up @@ -72,4 +77,22 @@ class SignalSetDetailedView(DetailView):

def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context: Dict[str, Any] = super().get_context_data(**kwargs)
context["epivis_url"] = settings.EPIVIS_URL
context["data_export_url"] = settings.DATA_EXPORT_URL
context["covidcast_url"] = settings.COVIDCAST_URL
context["data_source"] = (
self.object.signals.all()
.values_list("source__name", flat=True)
.distinct()
.order_by()
.first()
)
context["available_geographies"] = Geography.objects.filter(
id__in=self.object.signals.all()
.values_list("available_geography")
.distinct()
.order_by()
)
context["time_type"] = self.object.signals.all().values_list("time_type", flat=True).distinct().order_by().first()

return context
Loading

0 comments on commit 8c75032

Please sign in to comment.