[PATCH v2 04/10] parser: Remove matching label from subject prefixes

Stephen Finucane stephen at that.guru
Sun Oct 14 23:45:35 AEDT 2018


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 at 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(-)

diff --git a/patchwork/parser.py b/patchwork/parser.py
index 8c5106c9..9393a062 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -498,8 +498,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 9a4066fe..39f40d56 100644
--- a/patchwork/tests/test_parser.py
+++ b/patchwork/tests/test_parser.py
@@ -905,16 +905,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):
-- 
2.17.1



More information about the Patchwork mailing list