Experimental changes

This commit is contained in:
Paul Konstantin Gerke 2022-08-25 10:15:09 +02:00
parent d5bbbd4ee4
commit a6433732b6
3 changed files with 41 additions and 6 deletions

View File

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

View File

@ -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,11 +23,12 @@ 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",
get_schema_view(
@ -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

View File

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