Merge pull request #2875 from matakuka/removespeakers

delete list of speaker error messages (closes #2237)
This commit is contained in:
Norman Jäckel 2017-01-19 13:22:28 +01:00 committed by GitHub
commit a724898ba3
2 changed files with 19 additions and 7 deletions

View File

@ -127,15 +127,22 @@ class ItemViewSet(ListModelMixin, RetrieveModelMixin, UpdateModelMixin, GenericV
self.permission_denied(request)
if type(speaker_ids) is int:
speaker_ids = [speaker_ids]
deleted_speaker_count = 0
for speaker_id in speaker_ids:
try:
speaker = Speaker.objects.get(pk=int(speaker_id))
except (ValueError, Speaker.DoesNotExist):
raise ValidationError({'detail': _('Speaker does not exist.')})
# Delete the speaker.
speaker.delete()
message = _('Speaker %s was successfully removed from the list of speakers.') % speaker
pass
else:
speaker.delete()
deleted_speaker_name = speaker
deleted_speaker_count += 1
if deleted_speaker_count > 1:
message = str(deleted_speaker_count) + ' ' + _('speakers have been removed from the list of speakers.')
elif deleted_speaker_count == 1:
message = _('User %s has been removed from the list of speakers.') % deleted_speaker_name
else:
message = _('No speakers have been removed from the list of speakers.')
# Initiate response.
return Response({'detail': message})

View File

@ -1,5 +1,6 @@
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext
from rest_framework import status
from rest_framework.test import APIClient
@ -203,13 +204,17 @@ class ManageSpeaker(TestCase):
response = self.client.delete(
reverse('item-manage-speaker', args=[self.item.pk]),
{'speaker': '1'})
self.assertEqual(response.status_code, 400)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.get('detail'),
ugettext('No speakers have been removed from the list of speakers.'))
def test_remove_someone_else_invalid_data(self):
response = self.client.delete(
reverse('item-manage-speaker', args=[self.item.pk]),
{'speaker': 'invalid'})
self.assertEqual(response.status_code, 400)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.get('detail'),
ugettext('No speakers have been removed from the list of speakers.'))
def test_remove_someone_else_non_admin(self):
admin = get_user_model().objects.get(username='admin')