mirror of https://github.com/snachodog/mybuddy.git
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:
parent
02ea6487d5
commit
5ea7d33ef6
|
@ -265,41 +265,31 @@ class WeightSerializer(CoreModelSerializer, TaggableSerializer):
|
||||||
fields = ("id", "child", "weight", "date", "notes", "tags")
|
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):
|
class ProfileSerializer(serializers.ModelSerializer):
|
||||||
|
user = UserSerializer(many=False)
|
||||||
api_key = serializers.SerializerMethodField("get_api_key")
|
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):
|
def get_api_key(self, value):
|
||||||
return self.instance.api_key().key
|
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:
|
class Meta:
|
||||||
model = babybuddy_models.Settings
|
model = babybuddy_models.Settings
|
||||||
fields = (
|
fields = (
|
||||||
"user",
|
"user",
|
||||||
"username",
|
|
||||||
"first_name",
|
|
||||||
"last_name",
|
|
||||||
"email",
|
|
||||||
"staff",
|
|
||||||
"language",
|
"language",
|
||||||
"timezone",
|
"timezone",
|
||||||
"api_key",
|
"api_key",
|
||||||
|
|
|
@ -4,6 +4,7 @@ from django.shortcuts import get_object_or_404
|
||||||
from rest_framework import viewsets, views
|
from rest_framework import viewsets, views
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.schemas.openapi import AutoSchema
|
||||||
|
|
||||||
from core import models
|
from core import models
|
||||||
from babybuddy import models as babybuddy_models
|
from babybuddy import models as babybuddy_models
|
||||||
|
@ -119,7 +120,11 @@ class WeightViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
|
|
||||||
class ProfileView(views.APIView):
|
class ProfileView(views.APIView):
|
||||||
|
schema = AutoSchema(operation_id_base="CurrentProfile")
|
||||||
|
|
||||||
|
action = "get"
|
||||||
basename = "profile"
|
basename = "profile"
|
||||||
|
|
||||||
queryset = babybuddy_models.Settings.objects.all()
|
queryset = babybuddy_models.Settings.objects.all()
|
||||||
serializer_class = serializers.ProfileSerializer
|
serializer_class = serializers.ProfileSerializer
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue