Skip to content

Commit ec7cf38

Browse files
authored
Merge pull request doccano#163 from chakki-works/doccano#162
Possible to save the same label in other projects
2 parents 781f943 + eb854b1 commit ec7cf38

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

app/server/serializers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ def validate(self, attrs):
3434
raise ValidationError('Shortcut key may not have a suffix key.')
3535

3636
# Don't allow to save same shortcut key when prefix_key is null.
37+
context = self.context['request'].parser_context
38+
project_id = context['kwargs'].get('project_id')
3739
if Label.objects.filter(suffix_key=suffix_key,
38-
prefix_key__isnull=True).exists():
40+
prefix_key__isnull=True,
41+
project=project_id).exists():
3942
raise ValidationError('Duplicate key.')
4043
return super().validate(attrs)
4144

app/server/tests/test_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ def setUpTestData(cls):
152152
cls.main_project_label = mommy.make('server.Label', project=cls.main_project)
153153

154154
sub_project = mommy.make('server.Project', users=[non_project_member])
155+
other_project = mommy.make('server.Project', users=[super_user])
155156
mommy.make('server.Label', project=sub_project)
156157
cls.url = reverse(viewname='label_list', args=[cls.main_project.id])
158+
cls.other_url = reverse(viewname='label_list', args=[other_project.id])
157159
cls.data = {'text': 'example'}
158160

159161
def test_returns_labels_to_project_member(self):
@@ -194,6 +196,15 @@ def test_can_create_multiple_labels_without_shortcut_key(self):
194196
response = self.client.post(self.url, format='json', data=label)
195197
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
196198

199+
def test_can_create_same_label_in_multiple_projects(self):
200+
self.client.login(username=self.super_user_name,
201+
password=self.super_user_pass)
202+
label = {'text': 'LOC', 'prefix_key': None, 'suffix_key': 'l'}
203+
response = self.client.post(self.url, format='json', data=label)
204+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
205+
response = self.client.post(self.other_url, format='json', data=label)
206+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
207+
197208
def test_disallows_project_member_to_create_label(self):
198209
self.client.login(username=self.project_member_name,
199210
password=self.project_member_pass)

0 commit comments

Comments
 (0)