From b045ebd034ddb241ef28477f05af039af57dc01a Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 27 Jan 2024 15:28:26 +0100 Subject: [PATCH] Add API --- sncf/api/__init__.py | 0 sncf/api/apps.py | 6 +++++ sncf/api/serializers.py | 58 +++++++++++++++++++++++++++++++++++++++++ sncf/api/tests.py | 3 +++ sncf/api/views.py | 52 ++++++++++++++++++++++++++++++++++++ sncf/settings.py | 8 ++++++ sncf/urls.py | 17 ++++++++++++ 7 files changed, 144 insertions(+) create mode 100644 sncf/api/__init__.py create mode 100644 sncf/api/apps.py create mode 100644 sncf/api/serializers.py create mode 100644 sncf/api/tests.py create mode 100644 sncf/api/views.py diff --git a/sncf/api/__init__.py b/sncf/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sncf/api/apps.py b/sncf/api/apps.py new file mode 100644 index 0000000..55b034e --- /dev/null +++ b/sncf/api/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "sncf.api" diff --git a/sncf/api/serializers.py b/sncf/api/serializers.py new file mode 100644 index 0000000..9dfd9c2 --- /dev/null +++ b/sncf/api/serializers.py @@ -0,0 +1,58 @@ +from rest_framework import serializers + +from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, \ + Transfer, FeedInfo + + +class AgencySerializer(serializers.ModelSerializer): + class Meta: + model = Agency + fields = '__all__' + + +class StopSerializer(serializers.ModelSerializer): + class Meta: + model = Stop + fields = '__all__' + + +class RouteSerializer(serializers.ModelSerializer): + class Meta: + model = Route + fields = '__all__' + + +class TripSerializer(serializers.ModelSerializer): + class Meta: + model = Trip + fields = '__all__' + + +class StopTimeSerializer(serializers.ModelSerializer): + class Meta: + model = StopTime + fields = '__all__' + + +class CalendarSerializer(serializers.ModelSerializer): + class Meta: + model = Calendar + fields = '__all__' + + +class CalendarDateSerializer(serializers.ModelSerializer): + class Meta: + model = CalendarDate + fields = '__all__' + + +class TransferSerializer(serializers.ModelSerializer): + class Meta: + model = Transfer + fields = '__all__' + + +class FeedInfoSerializer(serializers.ModelSerializer): + class Meta: + model = FeedInfo + fields = '__all__' diff --git a/sncf/api/tests.py b/sncf/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/sncf/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/sncf/api/views.py b/sncf/api/views.py new file mode 100644 index 0000000..64a18f6 --- /dev/null +++ b/sncf/api/views.py @@ -0,0 +1,52 @@ +from rest_framework import viewsets + +from sncf.api.serializers import AgencySerializer, StopSerializer, RouteSerializer, TripSerializer, \ + StopTimeSerializer, CalendarSerializer, CalendarDateSerializer, TransferSerializer, \ + FeedInfoSerializer +from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, \ + Transfer, FeedInfo + + +class AgencyViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Agency.objects.all() + serializer_class = AgencySerializer + + +class StopViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Stop.objects.all() + serializer_class = StopSerializer + + +class RouteViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Route.objects.all() + serializer_class = RouteSerializer + + +class TripViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Trip.objects.all() + serializer_class = TripSerializer + + +class StopTimeViewSet(viewsets.ReadOnlyModelViewSet): + queryset = StopTime.objects.order_by('id').all() + serializer_class = StopTimeSerializer + + +class CalendarViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Calendar.objects.all() + serializer_class = CalendarSerializer + + +class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet): + queryset = CalendarDate.objects.all() + serializer_class = CalendarDateSerializer + + +class TransferViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Transfer.objects.all() + serializer_class = TransferSerializer + + +class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet): + queryset = FeedInfo.objects.all() + serializer_class = FeedInfoSerializer diff --git a/sncf/settings.py b/sncf/settings.py index 6c7cc9f..739ee31 100644 --- a/sncf/settings.py +++ b/sncf/settings.py @@ -38,6 +38,9 @@ INSTALLED_APPS = [ "django.contrib.messages", "django.contrib.staticfiles", + "rest_framework", + + "sncf.api", "sncfgtfs", ] @@ -123,3 +126,8 @@ STATIC_URL = "static/" # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +REST_FRAMEWORK = { + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 20 +} diff --git a/sncf/urls.py b/sncf/urls.py index 1032eab..4032ff6 100644 --- a/sncf/urls.py +++ b/sncf/urls.py @@ -16,8 +16,25 @@ Including another URLconf """ from django.contrib import admin from django.urls import path, include +from rest_framework import routers + +from sncf.api.views import AgencyViewSet, StopViewSet, RouteViewSet, TripViewSet, StopTimeViewSet, \ + CalendarViewSet, CalendarDateViewSet, TransferViewSet, FeedInfoViewSet + +router = routers.DefaultRouter() +router.register("gtfs/agency", AgencyViewSet) +router.register("gtfs/stop", StopViewSet) +router.register("gtfs/route", RouteViewSet) +router.register("gtfs/trip", TripViewSet) +router.register("gtfs/stop_time", StopTimeViewSet) +router.register("gtfs/calendar", CalendarViewSet) +router.register("gtfs/calendar_date", CalendarDateViewSet) +router.register("gtfs/transfer", TransferViewSet) +router.register("gtfs/feed_info", FeedInfoViewSet) urlpatterns = [ path("admin/", admin.site.urls, name="admin"), + path("api/", include(router.urls)), + path("api-auth/", include('rest_framework.urls', namespace='rest_framework')), path("gtfs/", include("sncfgtfs.urls"), name="gtfs"), ]