Skip to content

Commit

Permalink
Add tag as a ChoiceFilter on the ResourceFilterSet #1217
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed May 23, 2024
1 parent 6192657 commit 4d34ece
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
18 changes: 9 additions & 9 deletions scanpipe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ class ResourceFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
dropdown_widget_fields = [
"status",
"type",
"tag",
"compliance_alert",
"in_package",
"relation_map_type",
Expand Down Expand Up @@ -509,6 +510,7 @@ class ResourceFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
)
in_package = InPackageFilter(label="In a package")
status = StatusFilter()
tag = django_filters.ChoiceFilter()
relation_map_type = RelationMapTypeFilter(
label="Relation map type",
field_name="related_from__map_type",
Expand Down Expand Up @@ -556,14 +558,14 @@ class Meta:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if status_filter := self.filters.get("status"):
status_filter.extra.update(
{
"choices": status_filter.get_status_choices(
self.queryset, include_any=True
)
}
status_filter.extra["choices"] = status_filter.get_status_choices(
self.queryset, include_any=True
)

tags = self.queryset.order_by("tag").values_list("tag", flat=True).distinct()
tag_choices = [(tag, tag) for tag in tags if tag]
self.filters["tag"].extra["choices"] = tag_choices

license_expression_filer = self.filters["detected_license_expression"]
license_expression_filer.extra["widget"] = HasValueDropdownWidget()

Expand Down Expand Up @@ -838,6 +840,4 @@ def __init__(self, *args, **kwargs):
if project:
status_filter = self.filters.get("status")
qs = CodebaseResource.objects.filter(project=project)
status_filter.extra.update(
{"choices": status_filter.get_status_choices(qs)}
)
status_filter.extra["choices"] = status_filter.get_status_choices(qs)
5 changes: 4 additions & 1 deletion scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,10 @@ class CodebaseResourceListView(
"extension",
"programming_language",
"mime_type",
"tag",
{
"field_name": "tag",
"filter_fieldname": "tag",
},
{
"field_name": "detected_license_expression",
"filter_fieldname": "detected_license_expression",
Expand Down

0 comments on commit 4d34ece

Please sign in to comment.