Browse Source

Merge pull request #163 from chakki-works/#162

Possible to save the same label in other projects
pull/167/head
Hiroki Nakayama 5 years ago
committed by GitHub
parent
commit
ec7cf38017
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
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.') raise ValidationError('Shortcut key may not have a suffix key.')
# Don't allow to save same shortcut key when prefix_key is null. # 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, 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.') raise ValidationError('Duplicate key.')
return super().validate(attrs) 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) cls.main_project_label = mommy.make('server.Label', project=cls.main_project)
sub_project = mommy.make('server.Project', users=[non_project_member]) 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) mommy.make('server.Label', project=sub_project)
cls.url = reverse(viewname='label_list', args=[cls.main_project.id]) 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'} cls.data = {'text': 'example'}
def test_returns_labels_to_project_member(self): 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) response = self.client.post(self.url, format='json', data=label)
self.assertEqual(response.status_code, status.HTTP_201_CREATED) 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): def test_disallows_project_member_to_create_label(self):
self.client.login(username=self.project_member_name, self.client.login(username=self.project_member_name,
password=self.project_member_pass) password=self.project_member_pass)

Loading…
Cancel
Save