From df48b97ea8aaa68abbb4f24eeb098a4ce4ada1df Mon Sep 17 00:00:00 2001 From: s460930 Date: Mon, 30 Nov 2020 22:54:03 +0100 Subject: [PATCH] SMART-29 created serializers for user model --- rest-app/smartpicasso/app/user/serializers.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 rest-app/smartpicasso/app/user/serializers.py diff --git a/rest-app/smartpicasso/app/user/serializers.py b/rest-app/smartpicasso/app/user/serializers.py new file mode 100644 index 0000000..e55c7bc --- /dev/null +++ b/rest-app/smartpicasso/app/user/serializers.py @@ -0,0 +1,40 @@ +""" +@author p.dolata +""" +from rest_framework import serializers + +from smartpicasso.app.user.models import User +from smartpicasso.app.user_profile.models import UserProfile + + +class UserSerializer(serializers.ModelSerializer): + """ + Class to manage serializing UserProfile + """ + + class Meta: + model = UserProfile + fields = ('first_name', 'last_name', 'phone_number') + + +class UserRegistrationSerializer(serializers.ModelSerializer): + """ + Class to manage serializing user during registration + """ + profile = UserSerializer(required=False) + + class Meta: + model = User + fields = ('email', 'password', 'profile') + extra_kwargs = {'password': {'write_only': True}} + + def create(self, validated_data): + profile_data = validated_data.pop('profile') + user = User.objects.create_user(**validated_data) + UserProfile.objects.create( + user=user, + first_name=profile_data['first_name'], + last_name=profile_data['last_name'], + phone_number=profile_data['phone_number'], + ) + return user