Handle missing url attribute on write requests

This commit is contained in:
Santiago L 2021-07-08 12:25:29 +02:00
parent b365580165
commit 7d6a2474ab
1 changed files with 11 additions and 8 deletions

View File

@ -17,7 +17,7 @@ class SetPasswordSerializer(serializers.Serializer):
class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer): class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
""" support for postonly_fields, fields whose value can only be set on post """ """ support for postonly_fields, fields whose value can only be set on post """
def validate(self, attrs): def validate(self, attrs):
""" calls model.clean() """ """ calls model.clean() """
attrs = super(HyperlinkedModelSerializer, self).validate(attrs) attrs = super(HyperlinkedModelSerializer, self).validate(attrs)
@ -39,7 +39,7 @@ class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
instance = ModelClass(**validated_data) instance = ModelClass(**validated_data)
instance.clean() instance.clean()
return attrs return attrs
def post_only_cleanning(self, instance, validated_data): def post_only_cleanning(self, instance, validated_data):
""" removes postonly_fields from attrs """ """ removes postonly_fields from attrs """
model_attrs = dict(**validated_data) model_attrs = dict(**validated_data)
@ -49,12 +49,12 @@ class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
if attr in post_only_fields: if attr in post_only_fields:
model_attrs.pop(attr) model_attrs.pop(attr)
return model_attrs return model_attrs
def update(self, instance, validated_data): def update(self, instance, validated_data):
""" removes postonly_fields from attrs when not posting """ """ removes postonly_fields from attrs when not posting """
model_attrs = self.post_only_cleanning(instance, validated_data) model_attrs = self.post_only_cleanning(instance, validated_data)
return super(HyperlinkedModelSerializer, self).update(instance, model_attrs) return super(HyperlinkedModelSerializer, self).update(instance, model_attrs)
def partial_update(self, instance, validated_data): def partial_update(self, instance, validated_data):
""" removes postonly_fields from attrs when not posting """ """ removes postonly_fields from attrs when not posting """
model_attrs = self.post_only_cleanning(instance, validated_data) model_attrs = self.post_only_cleanning(instance, validated_data)
@ -64,7 +64,10 @@ class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
class RelatedHyperlinkedModelSerializer(HyperlinkedModelSerializer): class RelatedHyperlinkedModelSerializer(HyperlinkedModelSerializer):
""" returns object on to_internal_value based on URL """ """ returns object on to_internal_value based on URL """
def to_internal_value(self, data): def to_internal_value(self, data):
url = data.get('url') try:
url = data.get('url')
except AttributeError:
url = None
if not url: if not url:
raise ValidationError({ raise ValidationError({
'url': "URL is required." 'url': "URL is required."
@ -80,7 +83,7 @@ class SetPasswordHyperlinkedSerializer(HyperlinkedModelSerializer):
password = serializers.CharField(max_length=128, label=_('Password'), password = serializers.CharField(max_length=128, label=_('Password'),
validators=[validate_password], write_only=True, required=False, validators=[validate_password], write_only=True, required=False,
style={'widget': widgets.PasswordInput}) style={'widget': widgets.PasswordInput})
def validate_password(self, attrs, source): def validate_password(self, attrs, source):
""" POST only password """ """ POST only password """
if self.instance: if self.instance:
@ -89,7 +92,7 @@ class SetPasswordHyperlinkedSerializer(HyperlinkedModelSerializer):
elif 'password' not in attrs: elif 'password' not in attrs:
raise serializers.ValidationError(_("Password required")) raise serializers.ValidationError(_("Password required"))
return attrs return attrs
def validate(self, attrs): def validate(self, attrs):
""" remove password in case is not a real model field """ """ remove password in case is not a real model field """
try: try:
@ -102,7 +105,7 @@ class SetPasswordHyperlinkedSerializer(HyperlinkedModelSerializer):
if password is not None: if password is not None:
attrs['password'] = password attrs['password'] = password
return attrs return attrs
def create(self, validated_data): def create(self, validated_data):
password = validated_data.pop('password') password = validated_data.pop('password')
instance = self.Meta.model(**validated_data) instance = self.Meta.model(**validated_data)