From eb854b19290359ae97a1028a7c2567789f769c35 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 16 Apr 2019 15:42:00 +0900 Subject: [PATCH] Add filter condition to label serializer and fix #162 --- app/server/serializers.py | 5 ++++- app/server/tests/test_api.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/server/serializers.py b/app/server/serializers.py index 5972dc3d..78b57a75 100644 --- a/app/server/serializers.py +++ b/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) diff --git a/app/server/tests/test_api.py b/app/server/tests/test_api.py index 6cb0a28b..e8d80a51 100644 --- a/app/server/tests/test_api.py +++ b/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)