From 39f4171127b0d38aef967607e34744b40f49fead Mon Sep 17 00:00:00 2001
From: Ernesto Rico Schmidt <ernesto@helsinki.at>
Date: Thu, 13 Jan 2022 15:35:30 -0400
Subject: [PATCH] Make profile not required and modify logic of create/update

---
 program/serializers.py | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/program/serializers.py b/program/serializers.py
index c157b9d3..e08726fc 100644
--- a/program/serializers.py
+++ b/program/serializers.py
@@ -32,7 +32,7 @@ from steering.settings import THUMBNAIL_SIZES
 
 class UserSerializer(serializers.ModelSerializer):
     # Add profile fields to JSON
-    profile = ProfileSerializer()
+    profile = ProfileSerializer(required=False)
 
     class Meta:
         model = User
@@ -43,16 +43,18 @@ class UserSerializer(serializers.ModelSerializer):
         Create and return a new User instance, given the validated data.
         """
 
-        profile_data = validated_data.pop('profile')
+        profile_data = validated_data.pop('profile') if 'profile' in validated_data else None
 
         user = super(UserSerializer, self).create(validated_data)
         user.date_joined = timezone.now()
         user.set_password(validated_data['password'])
         user.save()
 
-        profile = Profile(user=user, cba_username=profile_data.get('cba_username').strip(),
-                          cba_user_token=profile_data.get('cba_user_token').strip())
-        profile.save()
+        if profile_data:
+            profile = Profile(user=user,
+                              cba_username=profile_data.get('cba_username').strip(),
+                              cba_user_token=profile_data.get('cba_user_token').strip())
+            profile.save()
 
         return user
 
@@ -72,15 +74,18 @@ class UserSerializer(serializers.ModelSerializer):
             instance.is_staff = validated_data.get('is_staff', instance.is_staff)
             instance.is_superuser = validated_data.get('is_superuser', instance.is_superuser)
 
-        # TODO: How to hook into this from ProfileSerializer without having to call it here?
-        try:
-            profile = Profile.objects.get(user=instance.id)
-        except ObjectDoesNotExist:
-            profile = Profile.objects.create(user=instance, **validated_data['profile'])
+        profile_data = validated_data.pop('profile') if 'profile' in validated_data else None
 
-        profile.cba_username = validated_data['profile'].get('cba_username')
-        profile.cba_user_token = validated_data['profile'].get('cba_user_token')
-        profile.save()
+        if profile_data:
+            # TODO: How to hook into this from ProfileSerializer without having to call it here?
+            try:
+                profile = Profile.objects.get(user=instance.id)
+            except ObjectDoesNotExist:
+                profile = Profile.objects.create(user=instance, **profile_data)
+
+            profile.cba_username = profile_data.get('cba_username')
+            profile.cba_user_token = profile_data.get('cba_user_token')
+            profile.save()
 
         instance.save()
         return instance
-- 
GitLab