[04/11] parser: Remove matching label from subject prefixes

Message ID 20180415225405.1354-5-stephen@that.guru
State Superseded
Headers show
Series
  • Add labels support
Related show

Commit Message

Stephen Finucane April 15, 2018, 10:53 p.m.
Extend the parser to strip the label from the list of prefixes. This
means we don't duplicate things twice in the UI.

Signed-off-by: Stephen Finucane <stephen@that.guru>
---
I'm not 100% sure about this which is why I've kept it separate. On one
hand, it doesn't make sense to duplicate things like this. On another,
not every label is something we'd want to parse from the subject line
and it makes prefixes in the UI kind of useless. Perhaps we should add a
'Label.parseable' flag?
---
 patchwork/parser.py            |  9 +++++++--
 patchwork/tests/test_parser.py | 19 +++++++++++++------
 2 files changed, 20 insertions(+), 8 deletions(-)

Patch

diff --git a/patchwork/parser.py b/patchwork/parser.py
index 7ac527a2..9ee4eba0 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -505,8 +505,13 @@  def parse_labels(subject_prefixes, project):
     Args:
         subject_prefixes: List of subject prefixes to extract tags from
     """
-    return Label.objects.filter(Q(project=project) | Q(project=None),
-                                name__in=subject_prefixes)
+    labels = Label.objects.filter(Q(project=project) | Q(project=None),
+                                  name__in=subject_prefixes)
+    for label in labels:
+        if label.name in subject_prefixes:
+            subject_prefixes.remove(label.name)
+
+    return labels
 
 
 def _find_content(mail):
diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py
index 50a07523..3615dfd8 100644
--- a/patchwork/tests/test_parser.py
+++ b/patchwork/tests/test_parser.py
@@ -893,16 +893,23 @@  class SubjectTest(TestCase):
     def test_labels(self):
         label = create_label(name='RFC')
 
-        self.assertEqual(list(parse_labels(['RFC'], label.project)), [label])
-        self.assertEqual(list(parse_labels(['rfcx'], label.project)), [])
+        prefixes = ['RFC']
+        self.assertEqual(list(parse_labels(prefixes, label.project)), [label])
+        self.assertEqual(prefixes, [])
 
-        project = create_project()
+        prefixes = ['rfcx']
+        self.assertEqual(list(parse_labels(prefixes, label.project)), [])
+        self.assertEqual(prefixes, ['rfcx'])
 
-        self.assertEqual(list(parse_labels(['RFC'], project)), [])
+        project = create_project()
+        prefixes = ['RFC']
+        self.assertEqual(list(parse_labels(prefixes, project)), [])
+        self.assertEqual(prefixes, ['RFC'])
 
         label = create_label(name='stuff', project=None)
-
-        self.assertEqual(list(parse_labels(['stuff'], project)), [label])
+        prefixes = ['stuff']
+        self.assertEqual(list(parse_labels(prefixes, project)), [label])
+        self.assertEqual(prefixes, []])
 
 
 class SubjectMatchTest(TestCase):