Skip to content

Commit 34dfe21

Browse files
authored
Merge pull request doccano#396 from CatalystCode/bugfix/edit-label-with-shortcut
Bugfix/Enable editing of labels with shortcut keys
2 parents 6f217bd + 8222ea6 commit 34dfe21

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

app/api/serializers.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ class Meta:
2121
class LabelSerializer(serializers.ModelSerializer):
2222

2323
def validate(self, attrs):
24-
if 'prefix_key' not in attrs and 'suffix_key' not in attrs:
25-
return super().validate(attrs)
26-
27-
prefix_key = attrs['prefix_key']
28-
suffix_key = attrs['suffix_key']
24+
prefix_key = attrs.get('prefix_key')
25+
suffix_key = attrs.get('suffix_key')
2926

3027
# In the case of user don't set any shortcut key.
3128
if prefix_key is None and suffix_key is None:
@@ -39,13 +36,22 @@ def validate(self, attrs):
3936
try:
4037
context = self.context['request'].parser_context
4138
project_id = context['kwargs']['project_id']
39+
label_id = context['kwargs'].get('label_id')
4240
except (AttributeError, KeyError):
4341
pass # unit tests don't always have the correct context set up
4442
else:
45-
if Label.objects.filter(suffix_key=suffix_key,
46-
prefix_key=prefix_key,
47-
project=project_id).exists():
48-
raise ValidationError('Duplicate key.')
43+
conflicting_labels = Label.objects.filter(
44+
suffix_key=suffix_key,
45+
prefix_key=prefix_key,
46+
project=project_id,
47+
)
48+
49+
if label_id is not None:
50+
conflicting_labels = conflicting_labels.exclude(id=label_id)
51+
52+
if conflicting_labels.exists():
53+
raise ValidationError('Duplicate shortcut key.')
54+
4955
return super().validate(attrs)
5056

5157
class Meta:

app/api/tests/test_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ def setUpTestData(cls):
308308
309309
project = mommy.make('Project', users=[project_member, super_user])
310310
cls.label = mommy.make('Label', project=project)
311+
cls.label_with_shortcut = mommy.make('Label', suffix_key='l', project=project)
311312
cls.url = reverse(viewname='label_detail', args=[project.id, cls.label.id])
313+
cls.url_with_shortcut = reverse(viewname='label_detail', args=[project.id, cls.label_with_shortcut.id])
312314
cls.data = {'text': 'example'}
313315
create_default_roles()
314316
assign_user_to_role(project_member=project_member, project=project,
@@ -332,6 +334,12 @@ def test_allows_superuser_to_update_label(self):
332334
response = self.client.patch(self.url, format='json', data=self.data)
333335
self.assertEqual(response.data['text'], self.data['text'])
334336

337+
def test_allows_superuser_to_update_label_with_shortcut(self):
338+
self.client.login(username=self.super_user_name,
339+
password=self.super_user_pass)
340+
response = self.client.patch(self.url_with_shortcut, format='json', data={'suffix_key': 's'})
341+
self.assertEqual(response.data['suffix_key'], 's')
342+
335343
def test_disallows_project_member_to_update_label(self):
336344
self.client.login(username=self.project_member_name,
337345
password=self.project_member_pass)

0 commit comments

Comments
 (0)