def update(self, instance, validated_data): """ Update the profile, including nested fields. Raises: errors.AccountValidationError: the update was not attempted because validation errors were found with the supplied update """ language_proficiencies = validated_data.pop("language_proficiencies", None) # Update all fields on the user profile that are writeable, # except for "language_proficiencies" and "social_links", which we'll update separately update_fields = set(self.get_writeable_fields()) - set(["language_proficiencies"]) - set(["social_links"]) for field_name in update_fields: default = getattr(instance, field_name) field_value = validated_data.get(field_name, default) setattr(instance, field_name, field_value) # Update the related language proficiency if language_proficiencies is not None: instance.language_proficiencies.all().delete() instance.language_proficiencies.bulk_create([ LanguageProficiency(user_profile=instance, code=language["code"]) for language in language_proficiencies ]) # Update the user's social links social_link_data = self._kwargs['data']['social_links'] if 'social_links' in self._kwargs['data'] else None if social_link_data and len(social_link_data) > 0: new_social_link = social_link_data[0] current_social_links = list(instance.social_links.all()) instance.social_links.all().delete() try: # Add the new social link with correct formatting validate_social_link(new_social_link['platform'], new_social_link['social_link']) formatted_link = format_social_link(new_social_link['platform'], new_social_link['social_link']) instance.social_links.bulk_create([ SocialLink(user_profile=instance, platform=new_social_link['platform'], social_link=formatted_link) ]) except ValueError as err: # If we have encountered any validation errors, return them to the user. raise errors.AccountValidationError({ 'social_links': { "developer_message": u"Error thrown from adding new social link: '{}'".format(text_type(err)), "user_message": text_type(err) } }) # Add back old links unless overridden by new link for current_social_link in current_social_links: if current_social_link.platform != new_social_link['platform']: instance.social_links.bulk_create([ SocialLink(user_profile=instance, platform=current_social_link.platform, social_link=current_social_link.social_link) ]) instance.save() return instance
def update(self, instance, validated_data): """ Update the profile, including nested fields. Raises: errors.AccountValidationError: the update was not attempted because validation errors were found with the supplied update """ language_proficiencies = validated_data.pop("language_proficiencies", None) # Update all fields on the user profile that are writeable, # except for "language_proficiencies" and "social_links", which we'll update separately update_fields = set(self.get_writeable_fields()) - set(["language_proficiencies"]) - set(["social_links"]) for field_name in update_fields: default = getattr(instance, field_name) field_value = validated_data.get(field_name, default) setattr(instance, field_name, field_value) # Update the related language proficiency if language_proficiencies is not None: instance.language_proficiencies.all().delete() instance.language_proficiencies.bulk_create([ LanguageProficiency(user_profile=instance, code=language["code"]) for language in language_proficiencies ]) # Update the user's social links requested_social_links = self._kwargs['data'].get('social_links') if requested_social_links: self._update_social_links(instance, requested_social_links) instance.save() return instance
def update(self, instance, validated_data): """ Update the profile, including nested fields. """ language_proficiencies = validated_data.pop("language_proficiencies", None) # Update all fields on the user profile that are writeable, # except for "language_proficiencies", which we'll update separately update_fields = set(self.get_writeable_fields()) - set( ["language_proficiencies"]) for field_name in update_fields: default = getattr(instance, field_name) field_value = validated_data.get(field_name, default) setattr(instance, field_name, field_value) instance.save() # Now update the related language proficiency if language_proficiencies is not None: instance.language_proficiencies.all().delete() instance.language_proficiencies.bulk_create([ LanguageProficiency(user_profile=instance, code=language["code"]) for language in language_proficiencies ]) return instance
def create_mock_profile(self, user): """ Helper method that creates a mock profile for the specified user :return: """ legacy_profile = UserProfile.objects.get(id=user.id) legacy_profile.country = "US" legacy_profile.level_of_education = "m" legacy_profile.year_of_birth = 2000 legacy_profile.goals = "world peace" legacy_profile.mailing_address = "Park Ave" legacy_profile.gender = "f" legacy_profile.bio = "Tired mother of twins" legacy_profile.profile_image_uploaded_at = TEST_PROFILE_IMAGE_UPLOADED_AT legacy_profile.language_proficiencies.add(LanguageProficiency(code='en')) legacy_profile.save()