Skip to content

Commit 8222ea6

Browse files
committed
Enable editing of labels with shortcut keys
1 parent 843ba42 commit 8222ea6

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
@@ -19,11 +19,8 @@ class Meta:
1919
class LabelSerializer(serializers.ModelSerializer):
2020

2121
def validate(self, attrs):
22-
if 'prefix_key' not in attrs and 'suffix_key' not in attrs:
23-
return super().validate(attrs)
24-
25-
prefix_key = attrs['prefix_key']
26-
suffix_key = attrs['suffix_key']
22+
prefix_key = attrs.get('prefix_key')
23+
suffix_key = attrs.get('suffix_key')
2724

2825
# In the case of user don't set any shortcut key.
2926
if prefix_key is None and suffix_key is None:
@@ -37,13 +34,22 @@ def validate(self, attrs):
3734
try:
3835
context = self.context['request'].parser_context
3936
project_id = context['kwargs']['project_id']
37+
label_id = context['kwargs'].get('label_id')
4038
except (AttributeError, KeyError):
4139
pass # unit tests don't always have the correct context set up
4240
else:
43-
if Label.objects.filter(suffix_key=suffix_key,
44-
prefix_key=prefix_key,
45-
project=project_id).exists():
46-
raise ValidationError('Duplicate key.')
41+
conflicting_labels = Label.objects.filter(
42+
suffix_key=suffix_key,
43+
prefix_key=prefix_key,
44+
project=project_id,
45+
)
46+
47+
if label_id is not None:
48+
conflicting_labels = conflicting_labels.exclude(id=label_id)
49+
50+
if conflicting_labels.exists():
51+
raise ValidationError('Duplicate shortcut key.')
52+
4753
return super().validate(attrs)
4854

4955
class Meta:

app/api/tests/test_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ def setUpTestData(cls):
256256
257257
project = mommy.make('Project', users=[project_member, super_user])
258258
cls.label = mommy.make('Label', project=project)
259+
cls.label_with_shortcut = mommy.make('Label', suffix_key='l', project=project)
259260
cls.url = reverse(viewname='label_detail', args=[project.id, cls.label.id])
261+
cls.url_with_shortcut = reverse(viewname='label_detail', args=[project.id, cls.label_with_shortcut.id])
260262
cls.data = {'text': 'example'}
261263

262264
def test_returns_label_to_project_member(self):
@@ -277,6 +279,12 @@ def test_allows_superuser_to_update_label(self):
277279
response = self.client.patch(self.url, format='json', data=self.data)
278280
self.assertEqual(response.data['text'], self.data['text'])
279281

282+
def test_allows_superuser_to_update_label_with_shortcut(self):
283+
self.client.login(username=self.super_user_name,
284+
password=self.super_user_pass)
285+
response = self.client.patch(self.url_with_shortcut, format='json', data={'suffix_key': 's'})
286+
self.assertEqual(response.data['suffix_key'], 's')
287+
280288
def test_disallows_project_member_to_update_label(self):
281289
self.client.login(username=self.project_member_name,
282290
password=self.project_member_pass)

0 commit comments

Comments
 (0)