2017-08-13 21:06:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-25 09:17:30 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from typing import NamedTuple, List, Any
|
2022-08-25 08:15:09 +00:00
|
|
|
|
2017-12-13 23:12:32 +00:00
|
|
|
from django.urls import include, path
|
2017-08-13 13:44:48 +00:00
|
|
|
from rest_framework import routers
|
2020-06-19 21:56:05 +00:00
|
|
|
from rest_framework.schemas import get_schema_view
|
2017-08-13 13:44:48 +00:00
|
|
|
|
2017-11-10 02:15:09 +00:00
|
|
|
from . import views
|
|
|
|
|
2022-08-25 09:17:30 +00:00
|
|
|
|
|
|
|
class ExtraPath(NamedTuple):
|
|
|
|
path: str
|
|
|
|
reverese_name: str
|
|
|
|
route: Any
|
|
|
|
|
|
|
|
|
|
|
|
class CustomRouterWithExtraPaths(routers.DefaultRouter):
|
|
|
|
extra_api_urls: List[ExtraPath]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.extra_api_urls = []
|
|
|
|
|
|
|
|
def add_detail_path(self, url_path, reverese_name, *args, **kwargs):
|
|
|
|
self.extra_api_urls = self.extra_api_urls or []
|
|
|
|
kwargs["name"] = reverese_name
|
|
|
|
self.extra_api_urls.append(
|
|
|
|
ExtraPath(url_path, reverese_name, path(url_path, *args, **kwargs))
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_api_root_view(self, api_urls=None):
|
|
|
|
api_root_dict = OrderedDict()
|
|
|
|
list_name = self.routes[0].name
|
|
|
|
for prefix, viewset, basename in self.registry:
|
|
|
|
api_root_dict[prefix] = list_name.format(basename=basename)
|
|
|
|
for extra_path in self.extra_api_urls:
|
|
|
|
api_root_dict[extra_path.path] = extra_path.reverese_name
|
|
|
|
|
|
|
|
return self.APIRootView.as_view(api_root_dict=api_root_dict)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def urls(self):
|
|
|
|
return super().urls + [e.route for e in self.extra_api_urls]
|
|
|
|
|
|
|
|
|
|
|
|
router = CustomRouterWithExtraPaths()
|
2022-05-27 22:39:25 +00:00
|
|
|
router.register(r"bmi", views.BMIViewSet)
|
2022-02-10 00:00:30 +00:00
|
|
|
router.register(r"changes", views.DiaperChangeViewSet)
|
2022-05-27 22:39:25 +00:00
|
|
|
router.register(r"children", views.ChildViewSet)
|
2022-02-10 00:00:30 +00:00
|
|
|
router.register(r"feedings", views.FeedingViewSet)
|
2022-05-27 22:39:25 +00:00
|
|
|
router.register(r"head-circumference", views.HeadCircumferenceViewSet)
|
|
|
|
router.register(r"height", views.HeightViewSet)
|
2022-02-10 00:00:30 +00:00
|
|
|
router.register(r"notes", views.NoteViewSet)
|
2022-03-04 15:39:13 +00:00
|
|
|
router.register(r"pumping", views.PumpingViewSet)
|
2022-05-27 22:39:25 +00:00
|
|
|
router.register(r"sleep", views.SleepViewSet)
|
|
|
|
router.register(r"tags", views.TagViewSet)
|
2022-03-03 03:11:06 +00:00
|
|
|
router.register(r"temperature", views.TemperatureViewSet)
|
2022-02-10 00:00:30 +00:00
|
|
|
router.register(r"timers", views.TimerViewSet)
|
|
|
|
router.register(r"tummy-times", views.TummyTimeViewSet)
|
|
|
|
router.register(r"weight", views.WeightViewSet)
|
2017-08-13 13:44:48 +00:00
|
|
|
|
2022-08-25 09:17:30 +00:00
|
|
|
router.add_detail_path("profile", "profile", views.ProfileView.as_view())
|
|
|
|
router.add_detail_path(
|
|
|
|
"schema",
|
|
|
|
"openapi-schema",
|
|
|
|
get_schema_view(
|
|
|
|
title="Baby Buddy API",
|
|
|
|
version=1,
|
|
|
|
description="API documentation for the Baby Buddy application",
|
2022-02-10 00:00:30 +00:00
|
|
|
),
|
2022-08-25 09:17:30 +00:00
|
|
|
)
|
2022-08-25 08:15:09 +00:00
|
|
|
|
|
|
|
|
2022-08-25 09:17:30 +00:00
|
|
|
app_name = "api"
|
2022-08-25 08:15:09 +00:00
|
|
|
|
|
|
|
urlpatterns = [
|
2022-08-25 09:17:30 +00:00
|
|
|
path("api/", include(router.urls)),
|
2022-08-25 08:15:09 +00:00
|
|
|
path("api/auth/", include("rest_framework.urls", namespace="rest_framework")),
|
2022-08-25 09:17:30 +00:00
|
|
|
]
|