diff --git a/api/serializers.py b/api/serializers.py index e78e80c1..70a873ab 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -9,6 +9,7 @@ from django.utils import timezone from taggit.serializers import TagListSerializerField, TaggitSerializer from core import models +from babybuddy import models as babybuddy_models class CoreModelSerializer(serializers.HyperlinkedModelSerializer): @@ -268,3 +269,9 @@ class WeightSerializer(CoreModelSerializer, TaggableSerializer): class Meta: model = models.Weight fields = ("id", "child", "weight", "date", "notes", "tags") + + +class ProfileSerializer(serializers.ModelSerializer): + class Meta: + model = babybuddy_models.Settings + fields = ("user", "language") diff --git a/api/urls.py b/api/urls.py index 10382c71..b71110c1 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from typing import NamedTuple, Optional, Any + from django.urls import include, path from rest_framework import routers from rest_framework.schemas import get_schema_view @@ -21,13 +23,14 @@ router.register(r"timers", views.TimerViewSet) router.register(r"tummy-times", views.TummyTimeViewSet) router.register(r"weight", views.WeightViewSet) -app_name = "api" +class ExtraUrl(NamedTuple): + view: Any + name: Optional[str] = None -urlpatterns = [ - path("api/", include(router.urls)), - path("api/auth/", include("rest_framework.urls", namespace="rest_framework")), +extra_api_urls = [ + path("api/profile", views.ProfileView.as_view()), path( - "api/schema", + "api/schema", get_schema_view( title="Baby Buddy API", version=1, @@ -36,3 +39,11 @@ urlpatterns = [ name="openapi-schema", ), ] + +app_name = "api" + + +urlpatterns = [ + path("api/", include(router.urls + list(extra_api_urls))), + path("api/auth/", include("rest_framework.urls", namespace="rest_framework")), +] + extra_api_urls diff --git a/api/views.py b/api/views.py index 3a7964cf..44892353 100644 --- a/api/views.py +++ b/api/views.py @@ -1,9 +1,12 @@ # -*- coding: utf-8 -*- -from rest_framework import viewsets +from django.shortcuts import get_object_or_404 + +from rest_framework import viewsets, views from rest_framework.decorators import action from rest_framework.response import Response from core import models +from babybuddy import models as babybuddy_models from . import serializers, filters @@ -113,3 +116,17 @@ class WeightViewSet(viewsets.ModelViewSet): queryset = models.Weight.objects.all() serializer_class = serializers.WeightSerializer filterset_fields = ("child", "date") + + +class ProfileView(views.APIView): + basename = "profile" + queryset = babybuddy_models.Settings.objects.all() + serializer_class = serializers.ProfileSerializer + + def get(self, request): + settings = get_object_or_404( + babybuddy_models.Settings.objects, + user=request.user + ) + serializer = self.serializer_class(settings) + return Response(serializer.data)