Browse Source

Add filter condition to label serializer and fix #162

pull/163/head
Hironsan 5 years ago
parent
commit
eb854b1929
2 changed files with 15 additions and 1 deletions
  1. 5
      app/server/serializers.py
  2. 11
      app/server/tests/test_api.py

5
app/server/serializers.py

@ -34,8 +34,11 @@ class LabelSerializer(serializers.ModelSerializer):
raise ValidationError('Shortcut key may not have a suffix key.')
# Don't allow to save same shortcut key when prefix_key is null.
context = self.context['request'].parser_context
project_id = context['kwargs'].get('project_id')
if Label.objects.filter(suffix_key=suffix_key,
prefix_key__isnull=True).exists():
prefix_key__isnull=True,
project=project_id).exists():
raise ValidationError('Duplicate key.')
return super().validate(attrs)

11
app/server/tests/test_api.py

@ -152,8 +152,10 @@ class TestLabelListAPI(APITestCase):
cls.main_project_label = mommy.make('server.Label', project=cls.main_project)
sub_project = mommy.make('server.Project', users=[non_project_member])
other_project = mommy.make('server.Project', users=[super_user])
mommy.make('server.Label', project=sub_project)
cls.url = reverse(viewname='label_list', args=[cls.main_project.id])
cls.other_url = reverse(viewname='label_list', args=[other_project.id])
cls.data = {'text': 'example'}
def test_returns_labels_to_project_member(self):
@ -194,6 +196,15 @@ class TestLabelListAPI(APITestCase):
response = self.client.post(self.url, format='json', data=label)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_can_create_same_label_in_multiple_projects(self):
self.client.login(username=self.super_user_name,
password=self.super_user_pass)
label = {'text': 'LOC', 'prefix_key': None, 'suffix_key': 'l'}
response = self.client.post(self.url, format='json', data=label)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
response = self.client.post(self.other_url, format='json', data=label)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_disallows_project_member_to_create_label(self):
self.client.login(username=self.project_member_name,
password=self.project_member_pass)

Loading…
Cancel
Save