Skip to content
Snippets Groups Projects
Commit 39f41711 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

Make profile not required and modify logic of create/update

parent 6a7c47c3
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment