-
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathfilters.py
More file actions
877 lines (746 loc) · 26.7 KB
/
Copy pathfilters.py
File metadata and controls
877 lines (746 loc) · 26.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# SPDX-License-Identifier: Apache-2.0
#
# http://nexb.com and https://github.com/nexB/scancode.io
# The ScanCode.io software is licensed under the Apache License version 2.0.
# Data generated with ScanCode.io is provided as-is without warranties.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
#
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.
import shlex
from django import forms
from django.apps import apps
from django.core.exceptions import FieldError
from django.core.validators import EMPTY_VALUES
from django.db import models
from django.db.models import Q
from django.db.models.fields import BLANK_CHOICE_DASH
from django.utils.http import urlencode
from django.utils.translation import gettext as _
import django_filters
from django_filters.widgets import LinkWidget
from packageurl.contrib.django.filters import PackageURLFilter
from scanpipe.models import CodebaseRelation
from scanpipe.models import CodebaseResource
from scanpipe.models import DiscoveredDependency
from scanpipe.models import DiscoveredPackage
from scanpipe.models import Project
from scanpipe.models import ProjectMessage
from scanpipe.models import Run
scanpipe_app = apps.get_app_config("scanpipe")
PAGE_VAR = "page"
EMPTY_VAR = "_EMPTY_"
ANY_VAR = "_ANY_"
OTHER_VAR = "_OTHER_"
class ModelFieldValuesFilter(django_filters.ChoiceFilter):
"""
A filter that provides dynamic choices for a specified model field.
This filter dynamically generates its choices based on the unique values
of a specified CharField in the model's queryset.
"""
def __init__(self, include_empty=False, include_any=False, *args, **kwargs):
self.include_empty = include_empty
self.include_any = include_any
super().__init__(*args, **kwargs)
def filter(self, qs, value):
if value == "any":
return qs
return super().filter(qs, value)
@property
def field(self):
"""
Override the field property to dynamically set the choices for this filter
based on unique values from the parent FilterSet's queryset.
This method retrieves all distinct values for the specified field from the
parent FilterSet's queryset.
"""
qs = self.parent.queryset
field_name = self.field_name
field = qs.model._meta.get_field(field_name)
choices = []
if self.include_empty:
choices.append((EMPTY_VAR, f"No {field.verbose_name}"))
if self.include_any:
choices.append(("any", f"Any {field.verbose_name}"))
# Retrieve distinct values for the specified field.
field_values = (
qs.order_by(field_name).values_list(field_name, flat=True).distinct()
)
value_choices = [
(value, value) for value in field_values if value not in EMPTY_VALUES
]
self.extra["choices"] = choices + value_choices
return super().field
class StrictBooleanFilter(django_filters.ChoiceFilter):
def __init__(self, *args, **kwargs):
kwargs["choices"] = (
(True, _("Yes")),
(False, _("No")),
)
super().__init__(*args, **kwargs)
class BulmaLinkWidget(LinkWidget):
"""Replace LinkWidget rendering with Bulma CSS classes."""
extra_css_class = ""
def render_option(self, name, selected_choices, option_value, option_label):
option_value = str(option_value)
if option_label == BLANK_CHOICE_DASH[0][1]:
option_label = _("All")
data = self.data.copy()
data[name] = option_value
selected = data == self.data or option_value in selected_choices
# Do not include the pagination in the filter query string.
data.pop(PAGE_VAR, None)
css_class = str(self.extra_css_class)
if selected:
css_class += " is-active"
try:
url = data.urlencode()
except AttributeError:
url = urlencode(data, doseq=True)
return self.option_string().format(
css_class=css_class,
query_string=url,
label=str(option_label),
)
def option_string(self):
return '<li><a href="?{query_string}" class="{css_class}">{label}</a></li>'
class BulmaDropdownWidget(BulmaLinkWidget):
extra_css_class = "dropdown-item"
class HasValueDropdownWidget(BulmaDropdownWidget):
def __init__(self, attrs=None, choices=()):
super().__init__(attrs)
self.choices = (
("", "All"),
(EMPTY_VAR, "None"),
(ANY_VAR, "Any"),
)
class FilterSetUtilsMixin:
empty_value = EMPTY_VAR
any_value = ANY_VAR
other_value = OTHER_VAR
dropdown_widget_class = BulmaDropdownWidget
dropdown_widget_fields = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Set the widget class for defined ``dropdown_widget_fields``.
for field_name in self.dropdown_widget_fields:
self.filters[field_name].extra["widget"] = self.dropdown_widget_class
@staticmethod
def remove_field_from_query_dict(query_dict, field_name, remove_value=None):
"""
For given `field_name`, returns an encoded URL without the value.
For multi-value filters, a single value can be removed using `remove_value`.
This URL can be used to remove a filter value from active filters.
"""
if not query_dict:
return ""
data = query_dict.copy()
field_data = data.pop(field_name, [])
if remove_value and len(field_data) > 1 and remove_value in field_data:
for item in field_data:
if item != remove_value:
data.update({field_name: item})
return data.urlencode()
def is_active(self):
"""Return True if any of the filters is active except for the 'sort' filter."""
return bool(
[
field_name
for field_name in self.form.changed_data
if field_name not in ["sort"]
]
)
def get_query_no_sort(self):
return self.remove_field_from_query_dict(self.data, "sort")
def get_filters_breadcrumb(self):
return [
{
"label": self.filters[field_name].label,
"value": value,
"remove_url": self.remove_field_from_query_dict(
self.data, field_name, value
),
}
for field_name in self.form.changed_data
for value in self.data.getlist(field_name)
]
@classmethod
def verbose_name_plural(cls):
return cls.Meta.model._meta.verbose_name_plural
@property
def params(self):
return dict(self.data.items())
@property
def params_for_search(self):
"""
Return the current request query parameter used to keep the state
of the filters when using the search form.
The pagination and the search value is removed from those parameters.
"""
params = self.params
params.pop(PAGE_VAR, None)
params.pop("search", None)
return params
def filter_queryset(self, queryset):
"""
Add the ability to filter by empty and none values providing the "magic"
`empty_value` to any filters.
"""
for name, value in self.form.cleaned_data.items():
field_name = self.filters[name].field_name
if value == self.empty_value:
queryset = queryset.filter(**{f"{field_name}__in": EMPTY_VALUES})
elif value == self.any_value:
queryset = queryset.filter(~Q(**{f"{field_name}__in": EMPTY_VALUES}))
elif value == self.other_value and hasattr(queryset, "less_common"):
return queryset.less_common(name)
else:
queryset = self.filters[name].filter(queryset, value)
return queryset
def parse_query_string_to_lookups(query_string, default_lookup_expr, default_field):
"""Parse a query string and convert it into queryset lookups using Q objects."""
lookups = Q()
terms = shlex.split(query_string)
lookup_types = {
"=": "iexact",
"^": "istartswith",
"$": "iendswith",
"~": "icontains",
">": "gt",
"<": "lt",
}
for term in terms:
lookup_expr = default_lookup_expr
negated = False
if ":" in term:
field_name, search_value = term.split(":", maxsplit=1)
if field_name.endswith(tuple(lookup_types.keys())):
lookup_symbol = field_name[-1]
lookup_expr = lookup_types.get(lookup_symbol)
field_name = field_name[:-1]
if field_name.startswith("-"):
field_name = field_name[1:]
negated = True
else:
search_value = term
field_name = default_field
lookups &= Q(**{f"{field_name}__{lookup_expr}": search_value}, _negated=negated)
return lookups
class QuerySearchField(forms.CharField):
"""Add value validation for the search complex query syntax."""
def validate(self, value):
super().validate(value)
try:
shlex.split(value)
except ValueError as error:
raise forms.ValidationError(
f"The provided search value is invalid: {error}", code="invalid"
)
class QuerySearchFilter(django_filters.CharFilter):
"""Add support for complex query syntax in search filter."""
field_class = QuerySearchField
def filter(self, qs, value):
if not value:
return qs
lookups = parse_query_string_to_lookups(
query_string=value,
default_lookup_expr=self.lookup_expr,
default_field=self.field_name,
)
try:
return qs.filter(lookups)
except FieldError:
return qs.none()
class ProjectFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
dropdown_widget_fields = [
"sort",
"pipeline",
"status",
]
search = QuerySearchFilter(
label="Search", field_name="name", lookup_expr="icontains"
)
sort = django_filters.OrderingFilter(
label="Sort",
fields=[
"created_date",
"name",
"discoveredpackages_count",
"discovereddependencies_count",
"codebaseresources_count",
"projectmessages_count",
],
empty_label="Newest",
choices=(
("created_date", "Oldest"),
("name", "Name (A-z)"),
("-name", "Name (z-A)"),
("-discoveredpackages_count", "Packages (+)"),
("discoveredpackages_count", "Packages (-)"),
("-discovereddependencies_count", "Dependencies (+)"),
("discovereddependencies_count", "Dependencies (-)"),
("-codebaseresources_count", "Resources (+)"),
("codebaseresources_count", "Resources (-)"),
("-projectmessages_count", "Messages (+)"),
("projectmessages_count", "Messages (-)"),
),
)
pipeline = django_filters.ChoiceFilter(
label="Pipeline",
field_name="runs__pipeline_name",
choices=scanpipe_app.get_pipeline_choices(include_blank=False),
distinct=True,
)
status = django_filters.ChoiceFilter(
label="Status",
method="filter_run_status",
choices=[
("not_started", "Not started"),
("queued", "Queued"),
("running", "Running"),
("succeed", "Success"),
("failed", "Failure"),
],
distinct=True,
)
label = django_filters.CharFilter(
label="Label",
field_name="labels__slug",
distinct=True,
)
class Meta:
model = Project
fields = ["is_archived"]
exclude = ["page"]
def __init__(self, data=None, *args, **kwargs):
"""Filter out the archived projects by default."""
super().__init__(data, *args, **kwargs)
# Default filtering by "Active" projects.
if not data or data.get("is_archived", "") == "":
self.queryset = self.queryset.filter(is_archived=False)
active_count = Project.objects.filter(is_archived=False).count()
archived_count = Project.objects.filter(is_archived=True).count()
self.filters["is_archived"].extra["widget"] = BulmaLinkWidget(
choices=[
("", f'<i class="fa-solid fa-seedling"></i> {active_count} Active'),
(
"true",
f'<i class="fa-solid fa-dice-d6"></i> {archived_count} Archived',
),
]
)
def filter_run_status(self, queryset, name, value):
"""Filter by Run status using the `RunQuerySet` methods."""
run_queryset_method = value
run_queryset = getattr(Run.objects, run_queryset_method)()
return queryset.filter(runs__in=run_queryset)
class JSONContainsFilter(django_filters.CharFilter):
"""
Allows "contains" lookup on a JSONField converted to text.
This is useful for data structures stored as a list of dictionaries, where
Django's default lookups are not available.
Requires the implementation of "json_field_contains" method on the QuerySet.
"""
def filter(self, qs, value):
if value:
return qs.json_field_contains(self.field_name, value)
return qs
class InPackageFilter(django_filters.ChoiceFilter):
def __init__(self, *args, **kwargs):
kwargs["choices"] = (
("true", "In a package"),
("false", "Not in a package"),
)
super().__init__(*args, **kwargs)
def filter(self, qs, value):
if value == "true":
return qs.in_package()
elif value == "false":
return qs.not_in_package()
return qs
MAP_TYPE_CHOICES = (
("about_file", "about file"),
("java_to_class", "java to class"),
("jar_to_source", "jar to source"),
("js_compiled", "js compiled"),
("js_colocation", "js colocation"),
("js_path", "js path"),
("path", "path"),
("sha1", "sha1"),
("dwarf_included_paths", "dwarf_included_paths"),
("dwarf_compiled_paths", "dwarf_compiled_paths"),
("go_file_paths", "go_file_paths"),
)
class RelationMapTypeFilter(django_filters.ChoiceFilter):
def __init__(self, *args, **kwargs):
kwargs["choices"] = (
("none", "No map"),
("any", "Any map"),
("many", "Many map"),
*MAP_TYPE_CHOICES,
)
super().__init__(*args, **kwargs)
def filter(self, qs, value):
if value == "none":
return qs.has_no_relation()
elif value == "any":
return qs.has_relation()
elif value == "many":
return qs.has_many_relation()
return super().filter(qs, value)
class ResourceFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
dropdown_widget_fields = [
"status",
"type",
"tag",
"compliance_alert",
"in_package",
"relation_map_type",
]
search = QuerySearchFilter(
label="Search",
field_name="path",
lookup_expr="icontains",
)
sort = django_filters.OrderingFilter(
label="Sort",
fields=[
"path",
"status",
"type",
"size",
"name",
"detected_license_expression",
"extension",
"programming_language",
"mime_type",
"tag",
"compliance_alert",
"related_from__map_type",
"related_from__from_resource__path",
],
)
compliance_alert = django_filters.ChoiceFilter(
choices=[(EMPTY_VAR, "None")] + CodebaseResource.Compliance.choices,
)
in_package = InPackageFilter(label="In a package")
status = ModelFieldValuesFilter(include_empty=True, include_any=True)
tag = ModelFieldValuesFilter(include_empty=True, include_any=True)
relation_map_type = RelationMapTypeFilter(
label="Relation map type",
field_name="related_from__map_type",
)
class Meta:
model = CodebaseResource
fields = [
"search",
"path",
"rootfs_path",
"md5",
"sha1",
"sha256",
"sha512",
"size",
"status",
"tag",
"type",
"name",
"extension",
"programming_language",
"mime_type",
"file_type",
"compliance_alert",
"copyrights",
"holders",
"authors",
"detected_license_expression",
"detected_license_expression_spdx",
"license_detections",
"license_clues",
"percentage_of_license_text",
"emails",
"urls",
"in_package",
"relation_map_type",
"is_binary",
"is_text",
"is_archive",
"is_key_file",
"is_media",
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
license_expression_filer = self.filters["detected_license_expression"]
license_expression_filer.extra["widget"] = HasValueDropdownWidget()
@classmethod
def filter_for_lookup(cls, field, lookup_type):
"""Add support for JSONField storing "list" using the JSONListFilter."""
if isinstance(field, models.JSONField) and field.default == list:
return JSONContainsFilter, {}
return super().filter_for_lookup(field, lookup_type)
class IsVulnerable(django_filters.ChoiceFilter):
def __init__(self, *args, **kwargs):
kwargs["choices"] = (
("yes", "Affected by vulnerabilities"),
("no", "No vulnerabilities found"),
)
super().__init__(*args, **kwargs)
def filter(self, qs, value):
if value == "yes":
return qs.filter(~Q(**{f"{self.field_name}__in": EMPTY_VALUES}))
elif value == "no":
return qs.filter(**{f"{self.field_name}__in": EMPTY_VALUES})
return qs
class DiscoveredPackageSearchFilter(QuerySearchFilter):
def filter(self, qs, value):
if not value:
return qs
if value.startswith("pkg:"):
return qs.for_package_url(value)
if ":" in value:
return super().filter(qs, value)
search_fields = ["type", "namespace", "name", "version"]
lookups = Q()
for field_names in search_fields:
lookups |= Q(**{f"{field_names}__{self.lookup_expr}": value})
return qs.filter(lookups)
class GroupOrderingFilter(django_filters.OrderingFilter):
"""Add the ability to provide a group a fields to order by."""
def __init__(self, *args, **kwargs):
"""grouped_fields = {"group_name": ["field1", "field2", "field3"]}"""
self.grouped_fields = kwargs.pop("grouped_fields", {})
super().__init__(*args, **kwargs)
def get_ordering_value(self, param):
descending = param.startswith("-")
param = param[1:] if descending else param
group = self.grouped_fields.get(param)
if not group:
group = [self.param_map.get(param, param)]
return [f"-{field_name}" if descending else field_name for field_name in group]
def filter(self, qs, value):
if value in EMPTY_VALUES:
return qs
ordering = []
for param in value:
ordering.extend(self.get_ordering_value(param))
return qs.order_by(*ordering)
class PackageFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
dropdown_widget_fields = [
"is_vulnerable",
"compliance_alert",
]
search = DiscoveredPackageSearchFilter(
label="Search", field_name="name", lookup_expr="icontains"
)
sort = GroupOrderingFilter(
label="Sort",
fields=[
"package_url",
"type",
"namespace",
"name",
"version",
"declared_license_expression",
"other_license_expression",
"compliance_alert",
"copyright",
"primary_language",
"tag",
"resources_count",
],
grouped_fields={"package_url": ["type", "namespace", "name", "version"]},
)
purl = PackageURLFilter(label="Package URL")
is_vulnerable = IsVulnerable(field_name="affected_by_vulnerabilities")
compliance_alert = django_filters.ChoiceFilter(
choices=[(EMPTY_VAR, "None")] + CodebaseResource.Compliance.choices,
)
copyright = django_filters.filters.CharFilter(widget=HasValueDropdownWidget)
declared_license_expression = django_filters.filters.CharFilter(
widget=HasValueDropdownWidget
)
is_private = StrictBooleanFilter()
is_virtual = StrictBooleanFilter()
class Meta:
model = DiscoveredPackage
fields = [
"search",
"purl",
"type",
"namespace",
"name",
"version",
"qualifiers",
"subpath",
"filename",
"primary_language",
"release_date",
"homepage_url",
"download_url",
"size",
"md5",
"sha1",
"bug_tracking_url",
"code_view_url",
"vcs_url",
"type",
"declared_license_expression",
"declared_license_expression_spdx",
"other_license_expression",
"other_license_expression_spdx",
"extracted_license_statement",
"copyright",
"is_vulnerable",
"compliance_alert",
"tag",
"is_private",
"is_virtual",
]
class DependencyFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
dropdown_widget_fields = [
"type",
"scope",
"is_runtime",
"is_optional",
"is_resolved",
"is_direct",
"datasource_id",
"is_vulnerable",
]
search = QuerySearchFilter(
label="Search", field_name="name", lookup_expr="icontains"
)
sort = GroupOrderingFilter(
label="Sort",
fields=[
"package_url",
"type",
"namespace",
"name",
"version",
"extracted_requirement",
"scope",
"is_runtime",
"is_optional",
"is_resolved",
"is_direct",
"for_package",
"resolved_to_package",
"datafile_resource",
"datasource_id",
],
grouped_fields={"package_url": ["type", "namespace", "name", "version"]},
)
purl = PackageURLFilter(label="Package URL")
type = ModelFieldValuesFilter()
scope = ModelFieldValuesFilter()
datasource_id = ModelFieldValuesFilter()
is_runtime = StrictBooleanFilter()
is_optional = StrictBooleanFilter()
is_resolved = StrictBooleanFilter()
is_direct = StrictBooleanFilter()
is_vulnerable = IsVulnerable(field_name="affected_by_vulnerabilities")
class Meta:
model = DiscoveredDependency
fields = [
"search",
"purl",
"dependency_uid",
"type",
"namespace",
"name",
"version",
"qualifiers",
"subpath",
"scope",
"is_runtime",
"is_optional",
"is_resolved",
"is_direct",
"datasource_id",
"is_vulnerable",
]
class ProjectMessageFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
search = QuerySearchFilter(
label="Search", field_name="description", lookup_expr="icontains"
)
sort = django_filters.OrderingFilter(
label="Sort",
fields=[
"severity",
"model",
],
)
class Meta:
model = ProjectMessage
fields = [
"search",
"severity",
"model",
"description",
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.filters["severity"].extra["widget"] = BulmaDropdownWidget()
class StatusFilter(django_filters.ChoiceFilter):
def filter(self, qs, value):
if value == "any":
return qs.status()
return super().filter(qs, value)
@staticmethod
def get_status_choices(qs, include_any=False):
"""Return the list of unique status for resources in ``project``."""
default_choices = [(EMPTY_VAR, "No status")]
if include_any:
default_choices.append(("any", "Any status"))
status_values = (
qs.order_by("status").values_list("status", flat=True).distinct()
)
value_choices = [(status, status) for status in status_values if status]
return default_choices + value_choices
class RelationFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
dropdown_widget_fields = [
"status",
"map_type",
]
search = QuerySearchFilter(
label="Search",
field_name="to_resource__path",
lookup_expr="icontains",
)
sort = django_filters.OrderingFilter(
label="Sort",
fields=[
"from_resource",
"to_resource",
"map_type",
],
)
map_type = django_filters.ChoiceFilter(choices=MAP_TYPE_CHOICES)
status = StatusFilter(field_name="to_resource__status")
class Meta:
model = CodebaseRelation
fields = [
"search",
"map_type",
"status",
]
def __init__(self, *args, **kwargs):
project = kwargs.pop("project")
super().__init__(*args, **kwargs)
if project:
qs = CodebaseResource.objects.filter(project=project)
status_filter = self.filters["status"]
status_filter.extra["choices"] = status_filter.get_status_choices(qs)