This commit is contained in:
Emmy D'Anello 2024-01-27 15:28:26 +01:00
parent b6da0c54f7
commit b045ebd034
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
7 changed files with 144 additions and 0 deletions

0
sncf/api/__init__.py Normal file
View File

6
sncf/api/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "sncf.api"

58
sncf/api/serializers.py Normal file
View File

@ -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__'

3
sncf/api/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

52
sncf/api/views.py Normal file
View File

@ -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

View File

@ -38,6 +38,9 @@ INSTALLED_APPS = [
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"rest_framework",
"sncf.api",
"sncfgtfs", "sncfgtfs",
] ]
@ -123,3 +126,8 @@ STATIC_URL = "static/"
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 20
}

View File

@ -16,8 +16,25 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path, include 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 = [ urlpatterns = [
path("admin/", admin.site.urls, name="admin"), 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"), path("gtfs/", include("sncfgtfs.urls"), name="gtfs"),
] ]