Used 'detail' for all ValidationError messages. See #1946.

This commit is contained in:
Norman Jäckel 2016-02-06 00:02:22 +01:00
parent 378218e8a5
commit 87a97edd69
5 changed files with 19 additions and 19 deletions

View File

@ -13,12 +13,12 @@ class JSONSerializerField(Field):
value is a dictionary with must have a key 'name'.
"""
if type(data) is not dict:
raise ValidationError('Data must be a dictionary.')
raise ValidationError({'detail': 'Data must be a dictionary.'})
for element in data.values():
if type(element) is not dict:
raise ValidationError('Data must be a dictionary.')
raise ValidationError({'detail': 'Data must be a dictionary.'})
elif element.get('name') is None:
raise ValidationError("Every dictionary must have a key 'name'.")
raise ValidationError({'detail': "Every dictionary must have a key 'name'."})
return data

View File

@ -177,13 +177,13 @@ class ProjectorViewSet(ReadOnlyModelViewSet):
of dictionaries to be appended to the projector config entry.
"""
if not isinstance(request.data, list):
raise ValidationError({'data': 'Data must be a list.'})
raise ValidationError({'detail': 'Data must be a list.'})
projector_instance = self.get_object()
projector_config = projector_instance.config
for element in request.data:
if element.get('name') is None:
raise ValidationError({'data': 'Invalid projector element. Name is missing.'})
raise ValidationError({'detail': 'Invalid projector element. Name is missing.'})
projector_config[uuid.uuid4().hex] = element
serializer = self.get_serializer(projector_instance, data={'config': projector_config}, partial=False)
@ -200,7 +200,7 @@ class ProjectorViewSet(ReadOnlyModelViewSet):
entries are deleted but not entries with stable == True.
"""
if not isinstance(request.data, list):
raise ValidationError({'data': 'Data must be a list.'})
raise ValidationError({'detail': 'Data must be a list.'})
projector_instance = self.get_object()
projector_config = {}
@ -209,7 +209,7 @@ class ProjectorViewSet(ReadOnlyModelViewSet):
projector_config[key] = value
for element in request.data:
if element.get('name') is None:
raise ValidationError({'data': 'Invalid projector element. Name is missing.'})
raise ValidationError({'detail': 'Invalid projector element. Name is missing.'})
projector_config[uuid.uuid4().hex] = element
serializer = self.get_serializer(projector_instance, data={'config': projector_config}, partial=False)
@ -239,8 +239,8 @@ class ProjectorViewSet(ReadOnlyModelViewSet):
}
"""
if not isinstance(request.data, dict):
raise ValidationError({'data': 'Data must be a dictionary.'})
error = {'data': 'Data must be a dictionary with UUIDs as keys and dictionaries as values.'}
raise ValidationError({'detail': 'Data must be a dictionary.'})
error = {'detail': 'Data must be a dictionary with UUIDs as keys and dictionaries as values.'}
for key, value in request.data.items():
try:
uuid.UUID(hex=str(key))
@ -253,7 +253,7 @@ class ProjectorViewSet(ReadOnlyModelViewSet):
projector_config = projector_instance.config
for key, value in request.data.items():
if key not in projector_config:
raise ValidationError({'data': 'Invalid projector element. Wrong UUID.'})
raise ValidationError({'detail': 'Invalid projector element. Wrong UUID.'})
projector_config[key].update(request.data[key])
serializer = self.get_serializer(projector_instance, data={'config': projector_config}, partial=False)
@ -270,12 +270,12 @@ class ProjectorViewSet(ReadOnlyModelViewSet):
that should be deleted.
"""
if not isinstance(request.data, list):
raise ValidationError({'data': 'Data must be a list of hex UUIDs.'})
raise ValidationError({'detail': 'Data must be a list of hex UUIDs.'})
for item in request.data:
try:
uuid.UUID(hex=str(item))
except ValueError:
raise ValidationError({'data': 'Data must be a list of hex UUIDs.'})
raise ValidationError({'detail': 'Data must be a list of hex UUIDs.'})
projector_instance = self.get_object()
projector_config = projector_instance.config
@ -283,7 +283,7 @@ class ProjectorViewSet(ReadOnlyModelViewSet):
try:
del projector_config[key]
except KeyError:
raise ValidationError({'data': 'Invalid UUID.'})
raise ValidationError({'detail': 'Invalid UUID.'})
serializer = self.get_serializer(projector_instance, data={'config': projector_config}, partial=False)
serializer.is_valid(raise_exception=True)
@ -327,11 +327,11 @@ class ProjectorViewSet(ReadOnlyModelViewSet):
}
"""
if not isinstance(request.data, dict):
raise ValidationError({'data': 'Data must be a dictionary.'})
raise ValidationError({'detail': 'Data must be a dictionary.'})
if (request.data.get('action') not in ('scale', 'scroll') or
request.data.get('direction') not in ('up', 'down', 'reset')):
raise ValidationError({'data': "Data must be a dictionary with an action ('scale' or 'scroll') "
"and a direction ('up', 'down' or 'reset')."})
raise ValidationError({'detail': "Data must be a dictionary with an action ('scale' or 'scroll') "
"and a direction ('up', 'down' or 'reset')."})
projector_instance = self.get_object()
if request.data['action'] == 'scale':

View File

@ -46,5 +46,5 @@ class MediafileViewSet(ModelViewSet):
str(self.request.user.pk) != str(uploader_id)):
self.permission_denied(request)
if not self.request.data.get('mediafile'):
raise ValidationError({'details': 'You forgot to provide a file.'})
raise ValidationError({'detail': 'You forgot to provide a file.'})
return super().create(request, *args, **kwargs)

View File

@ -27,7 +27,7 @@ def validate_workflow_field(value):
Validator to ensure that the workflow with the given id exists.
"""
if not Workflow.objects.filter(pk=value).exists():
raise ValidationError(_('Workflow %(pk)d does not exist.') % {'pk': value})
raise ValidationError({'detail': _('Workflow %(pk)d does not exist.') % {'pk': value}})
class CategorySerializer(ModelSerializer):

View File

@ -69,7 +69,7 @@ class UserFullSerializer(ModelSerializer):
Generates the username if it is empty.
"""
if not (data.get('username') or data.get('first_name') or data.get('last_name')):
raise ValidationError(_('Username, first name and last name can not all be empty.'))
raise ValidationError({'detail': _('Username, first name and last name can not all be empty.')})
# Generate username. But only if it is not set and the serializer is not
# called in a PATCH context (partial_update).