Skip to content

Commit a154f42

Browse files
committed
filters: Add label query filter
The filter allows filtering by project-specific and global labels. It also supports negative filtering. Prepending a label name with a minus sign excludes patches with that label from search results.
1 parent 62791e8 commit a154f42

4 files changed

Lines changed: 266 additions & 3 deletions

File tree

‎patchwork/filters.py‎

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,71 @@ def set_status(self, *args, **kwargs):
519519
self.forced = True
520520

521521

522+
class LabelsFilter(Filter):
523+
name = 'Labels'
524+
param = 'labels'
525+
526+
def __init__(self, filters):
527+
super(LabelsFilter, self).__init__(filters)
528+
self.labels = None
529+
530+
@property
531+
def condition(self):
532+
return self.labels
533+
534+
@property
535+
def key(self):
536+
return self.labels
537+
538+
@key.setter
539+
def key(self, key):
540+
key = key.strip()
541+
if not key:
542+
return
543+
544+
self.labels = key
545+
self.applied = True
546+
547+
@property
548+
def kwargs(self):
549+
return None
550+
551+
def q(self, queryset):
552+
label_names = self.labels.split(' ')
553+
if len(label_names) == 0:
554+
return queryset
555+
556+
labels_pos, labels_neg = [], []
557+
for label in label_names:
558+
if not label.startswith('-'):
559+
labels_pos.append(label)
560+
else:
561+
labels_neg.append(label[1:])
562+
563+
queryset = queryset.exclude_labels(labels_neg)
564+
queryset = queryset.include_labels(labels_pos)
565+
return queryset
566+
567+
@property
568+
def form(self):
569+
value = ''
570+
if self.labels:
571+
value = escape(self.labels)
572+
# labels value escaped above
573+
return mark_safe( # noqa: S308
574+
'<input type="text" id="labels_input" name="%s" '
575+
'class="form-control" value="%s">' % (self.param, value)
576+
)
577+
578+
522579
FILTERS = [
523580
SeriesFilter,
524581
SubmitterFilter,
525582
StateFilter,
526583
SearchFilter,
527584
ArchiveFilter,
528585
DelegateFilter,
586+
LabelsFilter,
529587
]
530588

531589

@@ -555,14 +613,22 @@ def available_filters(self):
555613

556614
def apply(self, queryset):
557615
kwargs = collections.OrderedDict()
616+
q_filters = []
558617
for f in self._filters:
559618
if f.applied:
560-
kwargs.update(f.kwargs)
619+
if f.kwargs is not None:
620+
kwargs.update(f.kwargs)
621+
else:
622+
q_filters.append(f)
561623

562-
if not kwargs:
624+
if not kwargs and not q_filters:
563625
return queryset
564626

565-
return queryset.filter(**kwargs)
627+
queryset = queryset.filter(**kwargs)
628+
for f in q_filters:
629+
queryset = f.q(queryset)
630+
631+
return queryset
566632

567633
def querystring(self, remove=None):
568634
params = self.params

‎patchwork/models.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@ def get_project_labels(project):
310310
)
311311
)
312312

313+
@staticmethod
314+
def filter_by_names(labels):
315+
labels = [label.lower() for label in labels]
316+
result = {}
317+
label_q = Label.objects.annotate(lname=Lower('name')).filter(
318+
lname__in=labels
319+
)
320+
321+
for label in label_q:
322+
if label.name not in result:
323+
result[label.name] = [label.id]
324+
else:
325+
result[label.name].append(label.id)
326+
327+
return result
328+
313329
class Meta:
314330
ordering = ['name']
315331
constraints = [
@@ -400,6 +416,37 @@ def with_tag_counts(self, project=None):
400416

401417
return qs
402418

419+
def exclude_labels(self, labels):
420+
lids = Label.filter_by_names(labels)
421+
for _, ids_with_name in lids.items():
422+
for lid in ids_with_name:
423+
label_q = Patch.labels.through.objects.filter(
424+
patch_id=OuterRef('pk'), label_id=lid
425+
)
426+
self = self.exclude(Exists(label_q))
427+
428+
return self
429+
430+
def include_labels(self, labels):
431+
if len(labels) == 0:
432+
return self
433+
434+
lids = Label.filter_by_names(labels)
435+
if len(lids) > 0:
436+
for _, ids_with_name in lids.items():
437+
q = Q()
438+
for lid in ids_with_name:
439+
q |= Exists(
440+
Patch.labels.through.objects.filter(
441+
patch_id=OuterRef('pk'), label_id=lid
442+
)
443+
)
444+
self = self.filter(q)
445+
446+
return self
447+
else:
448+
return Patch.objects.none()
449+
403450

404451
class PatchManager(models.Manager):
405452
def get_queryset(self):

‎patchwork/tests/unit/views/test_patch.py‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from patchwork.tests.utils import create_project
2626
from patchwork.tests.utils import create_state
2727
from patchwork.tests.utils import create_user
28+
from patchwork.tests.utils import create_label
2829
from patchwork.tests.utils import read_patch
2930

3031

@@ -203,6 +204,147 @@ def test_utf8_handling(self):
203204
self.assertEqual(response.status_code, 200)
204205

205206

207+
class PatchListLabelFilteringTest(TestCase):
208+
def setUp(self):
209+
self.project = create_project()
210+
self.label1 = create_label(project=self.project)
211+
self.label2 = create_label(project=self.project)
212+
self.label_RFC = create_label(project=self.project, name='RFC')
213+
self.label_RFC_global = create_label(project=None, name='RFC')
214+
self.global_label = create_label(project=None)
215+
216+
self.person = create_person(name='test', email='test@example.com')
217+
218+
self.p_no_labels = self.create_patch([])
219+
self.p_label1 = self.create_patch([self.label1])
220+
self.p_label1_2 = self.create_patch([self.label1, self.label2])
221+
self.p_label1_global = self.create_patch(
222+
[self.label1, self.global_label]
223+
)
224+
self.p_label_rfc = self.create_patch([self.label_RFC])
225+
self.p_label_rfc_global = self.create_patch([self.label_RFC_global])
226+
227+
project2 = create_project()
228+
self.p2_no_labels = create_patch(
229+
submitter=self.person, project=project2
230+
)
231+
self.p2_global_label = create_patch(
232+
submitter=self.person,
233+
project=project2,
234+
labels=[self.global_label],
235+
)
236+
237+
# NOTE: does not include project2 patches on purpose
238+
self.patches = [
239+
self.p_no_labels,
240+
self.p_label1,
241+
self.p_label1_2,
242+
self.p_label1_global,
243+
self.p_label_rfc,
244+
self.p_label_rfc_global,
245+
]
246+
247+
def _extract_patch_ids(self, response):
248+
"""Extract patch identifiers from patch list HTML."""
249+
id_re = re.compile(r'<tr id="patch-row:(\d+)"')
250+
ids = [
251+
int(m.group(1)) for m in id_re.finditer(response.content.decode())
252+
]
253+
254+
return ids
255+
256+
def create_patch(self, labels):
257+
return create_patch(
258+
submitter=self.person, project=self.project, labels=labels
259+
)
260+
261+
def _extract_patches(self, response):
262+
"""Extract patches from patch list and return Patch models."""
263+
ids = self._extract_patch_ids(response)
264+
if not ids:
265+
return []
266+
return [Patch.objects.get(id=i) for i in ids]
267+
268+
def filter_labels(self, labels, expected):
269+
"""Test if results contain expected patches when filtered by labels."""
270+
url = reverse(
271+
'patch-list', kwargs={'project_id': self.project.linkname}
272+
)
273+
query = '+'.join(labels)
274+
response = self.client.get(url + '?labels=' + query)
275+
276+
patches = self._extract_patches(response)
277+
self.assertEqual(len(patches), len(expected))
278+
for patch in patches:
279+
self.assertIn(patch, expected)
280+
281+
def test_no_labels(self):
282+
"""Test if no filter returns all patches."""
283+
self.filter_labels([], self.patches)
284+
285+
def test_one_label(self):
286+
self.filter_labels(
287+
[self.label1.name],
288+
[
289+
self.p_label1,
290+
self.p_label1_2,
291+
self.p_label1_global,
292+
],
293+
)
294+
295+
def test_two_labels(self):
296+
self.filter_labels(
297+
[self.label1.name, self.label2.name], [self.p_label1_2]
298+
)
299+
300+
def test_global_label(self):
301+
"""Test filtering by global labels."""
302+
self.filter_labels(
303+
[self.label1.name, self.global_label.name], [self.p_label1_global]
304+
)
305+
306+
def test_negative_label(self):
307+
"""Test filtering with a negative term."""
308+
self.filter_labels(
309+
['-' + self.label2.name],
310+
[
311+
self.p_no_labels,
312+
self.p_label1,
313+
self.p_label1_global,
314+
self.p_label_rfc,
315+
self.p_label_rfc_global,
316+
],
317+
)
318+
319+
def test_positive_negative_label(self):
320+
"""Test filtering with positive and negative term."""
321+
self.filter_labels(
322+
['-' + self.label2.name, self.label1.name],
323+
[
324+
self.p_label1,
325+
self.p_label1_global,
326+
],
327+
)
328+
329+
def test_mutually_exclusive_labels(self):
330+
"""Test filtering by same label as negative and positive filter."""
331+
self.filter_labels(['-' + self.label1.name, self.label1.name], [])
332+
333+
def test_label_local_global(self):
334+
"""Test filtering for patches with different labels, one local and
335+
one global (without a project) that have the same name.
336+
"""
337+
self.filter_labels(
338+
['RFC'], [self.p_label_rfc, self.p_label_rfc_global]
339+
)
340+
341+
def test_label_case_insensitive(self):
342+
"""Test if labels filtering is case-insensitive."""
343+
self.filter_labels(
344+
['rfc'], [self.p_label_rfc, self.p_label_rfc_global]
345+
)
346+
347+
206348
class PatchViewTest(TestCase):
207349
def test_redirect(self):
208350
patch = create_patch()

‎patchwork/tests/utils.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,20 @@ def create_patch(**kwargs):
192192
}
193193
values.update(kwargs)
194194

195+
labels = None
196+
if 'labels' in values:
197+
labels = values['labels']
198+
del values['labels']
199+
195200
patch = Patch.objects.create(**values)
196201

197202
if series:
198203
number = number or series.patches.count() + 1
199204
series.add_patch(patch, number)
200205

206+
if labels is not None:
207+
patch.labels.set(labels)
208+
201209
return patch
202210

203211

0 commit comments

Comments
 (0)