Making PasswordChangeSerializer compatible with DRF3

- validate_old_password - works with field value and not with dictionary of all values
 - validate - should not return None but raise ValidationError
This commit is contained in:
ante 2015-01-03 14:55:33 +01:00
parent 7984cb5f00
commit 59f482fd65

View File

@ -138,19 +138,20 @@ class PasswordChangeSerializer(serializers.Serializer):
self.request = self.context.get('request') self.request = self.context.get('request')
self.user = getattr(self.request, 'user', None) self.user = getattr(self.request, 'user', None)
def validate_old_password(self, attrs, source): def validate_old_password(self, value):
if self.old_password_field_enabled and self.user and \ if self.old_password_field_enabled and self.user and \
not self.user.check_password(attrs.get(source, '')): not self.user.check_password(value):
raise serializers.ValidationError('Invalid password') raise serializers.ValidationError('Invalid password')
return attrs return value
def validate(self, attrs): def validate(self, attrs):
self.set_password_form = self.set_password_form_class(user=self.user, self.set_password_form = self.set_password_form_class(user=self.user,
data=attrs) data=attrs)
if not self.set_password_form.is_valid(): if not self.set_password_form.is_valid():
self._errors = self.set_password_form.errors #self._errors = self.set_password_form.errors
return None #return None
raise serializers.ValidationError(self.set_password_form.errors)
return attrs return attrs
def save(self): def save(self):