Add back the UserProfile and use DRF nested relationship for profile page

- Also fix part of the openapi-specs. Item-model for profile page is still not
  rendered out however
This commit is contained in:
Paul Konstantin Gerke 2022-09-05 23:00:03 +02:00
parent 02ea6487d5
commit 5ea7d33ef6
2 changed files with 20 additions and 25 deletions

View File

@ -265,41 +265,31 @@ class WeightSerializer(CoreModelSerializer, TaggableSerializer):
fields = ("id", "child", "weight", "date", "notes", "tags")
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
"id",
"username",
"first_name",
"last_name",
"email",
"is_staff",
)
extra_kwargs = {k: {"read_only": True} for k in fields}
class ProfileSerializer(serializers.ModelSerializer):
user = UserSerializer(many=False)
api_key = serializers.SerializerMethodField("get_api_key")
username = serializers.SerializerMethodField("get_username")
first_name = serializers.SerializerMethodField("get_first_name")
last_name = serializers.SerializerMethodField("get_last_name")
email = serializers.SerializerMethodField("get_email")
staff = serializers.SerializerMethodField("is_staff")
def get_api_key(self, value):
return self.instance.api_key().key
def get_username(self, value):
return self.instance.user.username
def get_first_name(self, value):
return self.instance.user.first_name
def get_last_name(self, value):
return self.instance.user.last_name
def get_email(self, value):
return self.instance.user.email
def is_staff(self, value):
return self.instance.user.is_staff
class Meta:
model = babybuddy_models.Settings
fields = (
"user",
"username",
"first_name",
"last_name",
"email",
"staff",
"language",
"timezone",
"api_key",

View File

@ -4,6 +4,7 @@ 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 rest_framework.schemas.openapi import AutoSchema
from core import models
from babybuddy import models as babybuddy_models
@ -119,7 +120,11 @@ class WeightViewSet(viewsets.ModelViewSet):
class ProfileView(views.APIView):
schema = AutoSchema(operation_id_base="CurrentProfile")
action = "get"
basename = "profile"
queryset = babybuddy_models.Settings.objects.all()
serializer_class = serializers.ProfileSerializer